Store runtime data inside the config entry in Steam (#119881)

This commit is contained in:
Robert Hillis 2024-06-18 06:28:43 -04:00 committed by GitHub
parent 9128dc198a
commit dc388c76f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 22 additions and 19 deletions

View File

@ -6,24 +6,22 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from .const import DOMAIN
from .coordinator import SteamDataUpdateCoordinator
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."""
coordinator = SteamDataUpdateCoordinator(hass)
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)
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."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

View File

@ -10,7 +10,6 @@ import voluptuous as vol
from homeassistant.config_entries import (
SOURCE_REAUTH,
ConfigEntry,
ConfigFlow,
ConfigFlowResult,
OptionsFlow,
@ -19,6 +18,7 @@ from homeassistant.const import CONF_API_KEY, Platform
from homeassistant.core import callback
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
# 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:
"""Initialize the flow."""
self.entry: ConfigEntry | None = None
self.entry: SteamConfigEntry | None = None
@staticmethod
@callback
def async_get_options_flow(
config_entry: ConfigEntry,
config_entry: SteamConfigEntry,
) -> OptionsFlow:
"""Get the options flow for this handler."""
return SteamOptionsFlowHandler(config_entry)
@ -127,7 +127,7 @@ def _batch_ids(ids: list[str]) -> Iterator[list[str]]:
class SteamOptionsFlowHandler(OptionsFlow):
"""Handle Steam client options."""
def __init__(self, entry: ConfigEntry) -> None:
def __init__(self, entry: SteamConfigEntry) -> None:
"""Initialize options flow."""
self.entry = entry
self.options = dict(entry.options)

View File

@ -3,11 +3,11 @@
from __future__ import annotations
from datetime import timedelta
from typing import TYPE_CHECKING
import steam
from steam.api import _interface_method as INTMethod
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_API_KEY
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed
@ -15,13 +15,16 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda
from .const import CONF_ACCOUNTS, DOMAIN, LOGGER
if TYPE_CHECKING:
from . import SteamConfigEntry
class SteamDataUpdateCoordinator(
DataUpdateCoordinator[dict[str, dict[str, str | int]]]
):
"""Data update coordinator for the Steam integration."""
config_entry: ConfigEntry
config_entry: SteamConfigEntry
def __init__(self, hass: HomeAssistant) -> None:
"""Initialize the coordinator."""

View File

@ -7,15 +7,14 @@ from time import localtime, mktime
from typing import cast
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from homeassistant.util.dt import utc_from_timestamp
from . import SteamConfigEntry
from .const import (
CONF_ACCOUNTS,
DOMAIN,
STEAM_API_URL,
STEAM_HEADER_IMAGE_FILE,
STEAM_ICON_URL,
@ -30,12 +29,12 @@ PARALLEL_UPDATES = 1
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
entry: SteamConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Steam platform."""
async_add_entities(
SteamSensor(hass.data[DOMAIN][entry.entry_id], account)
SteamSensor(entry.runtime_data, account)
for account in entry.options[CONF_ACCOUNTS]
)

View File

@ -7,8 +7,11 @@ import urllib.parse
import steam
from homeassistant.components.steam_online import DOMAIN
from homeassistant.components.steam_online.const import CONF_ACCOUNT, CONF_ACCOUNTS
from homeassistant.components.steam_online.const import (
CONF_ACCOUNT,
CONF_ACCOUNTS,
DOMAIN,
)
from homeassistant.const import CONF_API_KEY
from homeassistant.core import HomeAssistant