mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 16:57:10 +00:00
Store runtime data inside the config entry in Steam (#119881)
This commit is contained in:
parent
9128dc198a
commit
dc388c76f9
@ -6,24 +6,22 @@ from homeassistant.config_entries import ConfigEntry
|
|||||||
from homeassistant.const import Platform
|
from homeassistant.const import Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .const import DOMAIN
|
|
||||||
from .coordinator import SteamDataUpdateCoordinator
|
from .coordinator import SteamDataUpdateCoordinator
|
||||||
|
|
||||||
PLATFORMS = [Platform.SENSOR]
|
PLATFORMS = [Platform.SENSOR]
|
||||||
|
type SteamConfigEntry = ConfigEntry[SteamDataUpdateCoordinator]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: SteamConfigEntry) -> bool:
|
||||||
"""Set up Steam from a config entry."""
|
"""Set up Steam from a config entry."""
|
||||||
coordinator = SteamDataUpdateCoordinator(hass)
|
coordinator = SteamDataUpdateCoordinator(hass)
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
|
entry.runtime_data = coordinator
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: SteamConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
|
||||||
return unload_ok
|
|
||||||
|
@ -10,7 +10,6 @@ import voluptuous as vol
|
|||||||
|
|
||||||
from homeassistant.config_entries import (
|
from homeassistant.config_entries import (
|
||||||
SOURCE_REAUTH,
|
SOURCE_REAUTH,
|
||||||
ConfigEntry,
|
|
||||||
ConfigFlow,
|
ConfigFlow,
|
||||||
ConfigFlowResult,
|
ConfigFlowResult,
|
||||||
OptionsFlow,
|
OptionsFlow,
|
||||||
@ -19,6 +18,7 @@ from homeassistant.const import CONF_API_KEY, Platform
|
|||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.helpers import config_validation as cv, entity_registry as er
|
from homeassistant.helpers import config_validation as cv, entity_registry as er
|
||||||
|
|
||||||
|
from . import SteamConfigEntry
|
||||||
from .const import CONF_ACCOUNT, CONF_ACCOUNTS, DOMAIN, LOGGER, PLACEHOLDERS
|
from .const import CONF_ACCOUNT, CONF_ACCOUNTS, DOMAIN, LOGGER, PLACEHOLDERS
|
||||||
|
|
||||||
# To avoid too long request URIs, the amount of ids to request is limited
|
# To avoid too long request URIs, the amount of ids to request is limited
|
||||||
@ -38,12 +38,12 @@ class SteamFlowHandler(ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
"""Initialize the flow."""
|
"""Initialize the flow."""
|
||||||
self.entry: ConfigEntry | None = None
|
self.entry: SteamConfigEntry | None = None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@callback
|
@callback
|
||||||
def async_get_options_flow(
|
def async_get_options_flow(
|
||||||
config_entry: ConfigEntry,
|
config_entry: SteamConfigEntry,
|
||||||
) -> OptionsFlow:
|
) -> OptionsFlow:
|
||||||
"""Get the options flow for this handler."""
|
"""Get the options flow for this handler."""
|
||||||
return SteamOptionsFlowHandler(config_entry)
|
return SteamOptionsFlowHandler(config_entry)
|
||||||
@ -127,7 +127,7 @@ def _batch_ids(ids: list[str]) -> Iterator[list[str]]:
|
|||||||
class SteamOptionsFlowHandler(OptionsFlow):
|
class SteamOptionsFlowHandler(OptionsFlow):
|
||||||
"""Handle Steam client options."""
|
"""Handle Steam client options."""
|
||||||
|
|
||||||
def __init__(self, entry: ConfigEntry) -> None:
|
def __init__(self, entry: SteamConfigEntry) -> None:
|
||||||
"""Initialize options flow."""
|
"""Initialize options flow."""
|
||||||
self.entry = entry
|
self.entry = entry
|
||||||
self.options = dict(entry.options)
|
self.options = dict(entry.options)
|
||||||
|
@ -3,11 +3,11 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
import steam
|
import steam
|
||||||
from steam.api import _interface_method as INTMethod
|
from steam.api import _interface_method as INTMethod
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_API_KEY
|
from homeassistant.const import CONF_API_KEY
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||||
@ -15,13 +15,16 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda
|
|||||||
|
|
||||||
from .const import CONF_ACCOUNTS, DOMAIN, LOGGER
|
from .const import CONF_ACCOUNTS, DOMAIN, LOGGER
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from . import SteamConfigEntry
|
||||||
|
|
||||||
|
|
||||||
class SteamDataUpdateCoordinator(
|
class SteamDataUpdateCoordinator(
|
||||||
DataUpdateCoordinator[dict[str, dict[str, str | int]]]
|
DataUpdateCoordinator[dict[str, dict[str, str | int]]]
|
||||||
):
|
):
|
||||||
"""Data update coordinator for the Steam integration."""
|
"""Data update coordinator for the Steam integration."""
|
||||||
|
|
||||||
config_entry: ConfigEntry
|
config_entry: SteamConfigEntry
|
||||||
|
|
||||||
def __init__(self, hass: HomeAssistant) -> None:
|
def __init__(self, hass: HomeAssistant) -> None:
|
||||||
"""Initialize the coordinator."""
|
"""Initialize the coordinator."""
|
||||||
|
@ -7,15 +7,14 @@ from time import localtime, mktime
|
|||||||
from typing import cast
|
from typing import cast
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import StateType
|
from homeassistant.helpers.typing import StateType
|
||||||
from homeassistant.util.dt import utc_from_timestamp
|
from homeassistant.util.dt import utc_from_timestamp
|
||||||
|
|
||||||
|
from . import SteamConfigEntry
|
||||||
from .const import (
|
from .const import (
|
||||||
CONF_ACCOUNTS,
|
CONF_ACCOUNTS,
|
||||||
DOMAIN,
|
|
||||||
STEAM_API_URL,
|
STEAM_API_URL,
|
||||||
STEAM_HEADER_IMAGE_FILE,
|
STEAM_HEADER_IMAGE_FILE,
|
||||||
STEAM_ICON_URL,
|
STEAM_ICON_URL,
|
||||||
@ -30,12 +29,12 @@ PARALLEL_UPDATES = 1
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: SteamConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Steam platform."""
|
"""Set up the Steam platform."""
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
SteamSensor(hass.data[DOMAIN][entry.entry_id], account)
|
SteamSensor(entry.runtime_data, account)
|
||||||
for account in entry.options[CONF_ACCOUNTS]
|
for account in entry.options[CONF_ACCOUNTS]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -7,8 +7,11 @@ import urllib.parse
|
|||||||
|
|
||||||
import steam
|
import steam
|
||||||
|
|
||||||
from homeassistant.components.steam_online import DOMAIN
|
from homeassistant.components.steam_online.const import (
|
||||||
from homeassistant.components.steam_online.const import CONF_ACCOUNT, CONF_ACCOUNTS
|
CONF_ACCOUNT,
|
||||||
|
CONF_ACCOUNTS,
|
||||||
|
DOMAIN,
|
||||||
|
)
|
||||||
from homeassistant.const import CONF_API_KEY
|
from homeassistant.const import CONF_API_KEY
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user