Use runtime_data in hvv_departures (#144951)

This commit is contained in:
epenet 2025-05-19 10:58:01 +02:00 committed by GitHub
parent 177afea5ad
commit f50afae1c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 20 additions and 22 deletions

View File

@ -1,17 +1,15 @@
"""The HVV integration.""" """The HVV integration."""
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME, Platform from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME, Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import aiohttp_client from homeassistant.helpers import aiohttp_client
from .const import DOMAIN from .hub import GTIHub, HVVConfigEntry
from .hub import GTIHub
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR] 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.""" """Set up HVV from a config entry."""
hub = GTIHub( hub = GTIHub(
@ -21,14 +19,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
aiohttp_client.async_get_clientsession(hass), aiohttp_client.async_get_clientsession(hass),
) )
hass.data.setdefault(DOMAIN, {}) entry.runtime_data = hub
hass.data[DOMAIN][entry.entry_id] = hub
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: HVVConfigEntry) -> bool:
"""Unload a config entry.""" """Unload a config entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

View File

@ -14,7 +14,6 @@ from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass, BinarySensorDeviceClass,
BinarySensorEntity, BinarySensorEntity,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback 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 .const import ATTRIBUTION, CONF_STATION, DOMAIN, MANUFACTURER
from .hub import HVVConfigEntry
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: HVVConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback, async_add_entities: AddConfigEntryEntitiesCallback,
) -> None: ) -> None:
"""Set up the binary_sensor platform.""" """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_name = entry.data[CONF_STATION]["name"]
station = entry.data[CONF_STATION] station = entry.data[CONF_STATION]

View File

@ -9,18 +9,13 @@ from pygti.auth import GTI_DEFAULT_HOST
from pygti.exceptions import CannotConnect, InvalidAuth from pygti.exceptions import CannotConnect, InvalidAuth
import voluptuous as vol import voluptuous as vol
from homeassistant.config_entries import ( from homeassistant.config_entries import ConfigFlow, ConfigFlowResult, OptionsFlow
ConfigEntry,
ConfigFlow,
ConfigFlowResult,
OptionsFlow,
)
from homeassistant.const import CONF_HOST, CONF_OFFSET, CONF_PASSWORD, CONF_USERNAME from homeassistant.const import CONF_HOST, CONF_OFFSET, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.helpers import aiohttp_client, config_validation as cv from homeassistant.helpers import aiohttp_client, config_validation as cv
from .const import CONF_FILTER, CONF_REAL_TIME, CONF_STATION, DOMAIN from .const import CONF_FILTER, CONF_REAL_TIME, CONF_STATION, DOMAIN
from .hub import GTIHub from .hub import GTIHub, HVVConfigEntry
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -137,7 +132,7 @@ class HVVDeparturesConfigFlow(ConfigFlow, domain=DOMAIN):
@staticmethod @staticmethod
@callback @callback
def async_get_options_flow( def async_get_options_flow(
config_entry: ConfigEntry, config_entry: HVVConfigEntry,
) -> OptionsFlowHandler: ) -> OptionsFlowHandler:
"""Get options flow.""" """Get options flow."""
return OptionsFlowHandler() return OptionsFlowHandler()
@ -146,6 +141,8 @@ class HVVDeparturesConfigFlow(ConfigFlow, domain=DOMAIN):
class OptionsFlowHandler(OptionsFlow): class OptionsFlowHandler(OptionsFlow):
"""Options flow handler.""" """Options flow handler."""
config_entry: HVVConfigEntry
def __init__(self) -> None: def __init__(self) -> None:
"""Initialize HVV Departures options flow.""" """Initialize HVV Departures options flow."""
self.departure_filters: dict[str, Any] = {} self.departure_filters: dict[str, Any] = {}
@ -157,7 +154,7 @@ class OptionsFlowHandler(OptionsFlow):
errors = {} errors = {}
if not self.departure_filters: if not self.departure_filters:
departure_list = {} departure_list = {}
hub: GTIHub = self.hass.data[DOMAIN][self.config_entry.entry_id] hub = self.config_entry.runtime_data
try: try:
departure_list = await hub.gti.departureList( departure_list = await hub.gti.departureList(

View File

@ -2,6 +2,10 @@
from pygti.gti import GTI, Auth from pygti.gti import GTI, Auth
from homeassistant.config_entries import ConfigEntry
type HVVConfigEntry = ConfigEntry[GTIHub]
class GTIHub: class GTIHub:
"""GTI Hub.""" """GTI Hub."""

View File

@ -8,7 +8,6 @@ from aiohttp import ClientConnectorError
from pygti.exceptions import InvalidAuth from pygti.exceptions import InvalidAuth
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_ID, CONF_OFFSET from homeassistant.const import ATTR_ID, CONF_OFFSET
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import aiohttp_client 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 homeassistant.util.dt import get_time_zone, utcnow
from .const import ATTRIBUTION, CONF_REAL_TIME, CONF_STATION, DOMAIN, MANUFACTURER from .const import ATTRIBUTION, CONF_REAL_TIME, CONF_STATION, DOMAIN, MANUFACTURER
from .hub import HVVConfigEntry
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1) MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1)
MAX_LIST = 20 MAX_LIST = 20
@ -41,11 +41,11 @@ _LOGGER = logging.getLogger(__name__)
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: HVVConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback, async_add_entities: AddConfigEntryEntitiesCallback,
) -> None: ) -> None:
"""Set up the sensor platform.""" """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) session = aiohttp_client.async_get_clientsession(hass)