From f50afae1c3d6a22dc816a7534da1e1be62dc0ef2 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 19 May 2025 10:58:01 +0200 Subject: [PATCH] Use runtime_data in hvv_departures (#144951) --- .../components/hvv_departures/__init__.py | 11 ++++------- .../components/hvv_departures/binary_sensor.py | 6 +++--- .../components/hvv_departures/config_flow.py | 15 ++++++--------- homeassistant/components/hvv_departures/hub.py | 4 ++++ homeassistant/components/hvv_departures/sensor.py | 6 +++--- 5 files changed, 20 insertions(+), 22 deletions(-) diff --git a/homeassistant/components/hvv_departures/__init__.py b/homeassistant/components/hvv_departures/__init__.py index 1104359111c..cfe76591688 100644 --- a/homeassistant/components/hvv_departures/__init__.py +++ b/homeassistant/components/hvv_departures/__init__.py @@ -1,17 +1,15 @@ """The HVV integration.""" -from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import aiohttp_client -from .const import DOMAIN -from .hub import GTIHub +from .hub import GTIHub, HVVConfigEntry PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR] -async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_setup_entry(hass: HomeAssistant, entry: HVVConfigEntry) -> bool: """Set up HVV from a config entry.""" hub = GTIHub( @@ -21,14 +19,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: aiohttp_client.async_get_clientsession(hass), ) - hass.data.setdefault(DOMAIN, {}) - hass.data[DOMAIN][entry.entry_id] = hub + entry.runtime_data = hub 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: HVVConfigEntry) -> bool: """Unload a config entry.""" return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/hvv_departures/binary_sensor.py b/homeassistant/components/hvv_departures/binary_sensor.py index 622a8436e04..18598dd4c94 100644 --- a/homeassistant/components/hvv_departures/binary_sensor.py +++ b/homeassistant/components/hvv_departures/binary_sensor.py @@ -14,7 +14,6 @@ from homeassistant.components.binary_sensor import ( BinarySensorDeviceClass, BinarySensorEntity, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback @@ -25,17 +24,18 @@ from homeassistant.helpers.update_coordinator import ( ) from .const import ATTRIBUTION, CONF_STATION, DOMAIN, MANUFACTURER +from .hub import HVVConfigEntry _LOGGER = logging.getLogger(__name__) async def async_setup_entry( hass: HomeAssistant, - entry: ConfigEntry, + entry: HVVConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up the binary_sensor platform.""" - hub = hass.data[DOMAIN][entry.entry_id] + hub = entry.runtime_data station_name = entry.data[CONF_STATION]["name"] station = entry.data[CONF_STATION] diff --git a/homeassistant/components/hvv_departures/config_flow.py b/homeassistant/components/hvv_departures/config_flow.py index d76ccef7cab..63d457bf302 100644 --- a/homeassistant/components/hvv_departures/config_flow.py +++ b/homeassistant/components/hvv_departures/config_flow.py @@ -9,18 +9,13 @@ from pygti.auth import GTI_DEFAULT_HOST from pygti.exceptions import CannotConnect, InvalidAuth import voluptuous as vol -from homeassistant.config_entries import ( - ConfigEntry, - ConfigFlow, - ConfigFlowResult, - OptionsFlow, -) +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult, OptionsFlow from homeassistant.const import CONF_HOST, CONF_OFFSET, CONF_PASSWORD, CONF_USERNAME from homeassistant.core import callback from homeassistant.helpers import aiohttp_client, config_validation as cv from .const import CONF_FILTER, CONF_REAL_TIME, CONF_STATION, DOMAIN -from .hub import GTIHub +from .hub import GTIHub, HVVConfigEntry _LOGGER = logging.getLogger(__name__) @@ -137,7 +132,7 @@ class HVVDeparturesConfigFlow(ConfigFlow, domain=DOMAIN): @staticmethod @callback def async_get_options_flow( - config_entry: ConfigEntry, + config_entry: HVVConfigEntry, ) -> OptionsFlowHandler: """Get options flow.""" return OptionsFlowHandler() @@ -146,6 +141,8 @@ class HVVDeparturesConfigFlow(ConfigFlow, domain=DOMAIN): class OptionsFlowHandler(OptionsFlow): """Options flow handler.""" + config_entry: HVVConfigEntry + def __init__(self) -> None: """Initialize HVV Departures options flow.""" self.departure_filters: dict[str, Any] = {} @@ -157,7 +154,7 @@ class OptionsFlowHandler(OptionsFlow): errors = {} if not self.departure_filters: departure_list = {} - hub: GTIHub = self.hass.data[DOMAIN][self.config_entry.entry_id] + hub = self.config_entry.runtime_data try: departure_list = await hub.gti.departureList( diff --git a/homeassistant/components/hvv_departures/hub.py b/homeassistant/components/hvv_departures/hub.py index 7cffbed345c..31523b72ba1 100644 --- a/homeassistant/components/hvv_departures/hub.py +++ b/homeassistant/components/hvv_departures/hub.py @@ -2,6 +2,10 @@ from pygti.gti import GTI, Auth +from homeassistant.config_entries import ConfigEntry + +type HVVConfigEntry = ConfigEntry[GTIHub] + class GTIHub: """GTI Hub.""" diff --git a/homeassistant/components/hvv_departures/sensor.py b/homeassistant/components/hvv_departures/sensor.py index 667893db8f2..1b10451f22d 100644 --- a/homeassistant/components/hvv_departures/sensor.py +++ b/homeassistant/components/hvv_departures/sensor.py @@ -8,7 +8,6 @@ from aiohttp import ClientConnectorError from pygti.exceptions import InvalidAuth from homeassistant.components.sensor import SensorDeviceClass, SensorEntity -from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_ID, CONF_OFFSET from homeassistant.core import HomeAssistant from homeassistant.helpers import aiohttp_client @@ -18,6 +17,7 @@ from homeassistant.util import Throttle from homeassistant.util.dt import get_time_zone, utcnow from .const import ATTRIBUTION, CONF_REAL_TIME, CONF_STATION, DOMAIN, MANUFACTURER +from .hub import HVVConfigEntry MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1) MAX_LIST = 20 @@ -41,11 +41,11 @@ _LOGGER = logging.getLogger(__name__) async def async_setup_entry( hass: HomeAssistant, - config_entry: ConfigEntry, + config_entry: HVVConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up the sensor platform.""" - hub = hass.data[DOMAIN][config_entry.entry_id] + hub = config_entry.runtime_data session = aiohttp_client.async_get_clientsession(hass)