diff --git a/homeassistant/components/gios/__init__.py b/homeassistant/components/gios/__init__.py index 5810a32f80f..6c49ddd9020 100644 --- a/homeassistant/components/gios/__init__.py +++ b/homeassistant/components/gios/__init__.py @@ -3,6 +3,7 @@ from __future__ import annotations import asyncio +from dataclasses import dataclass import logging from aiohttp import ClientSession @@ -25,8 +26,17 @@ _LOGGER = logging.getLogger(__name__) PLATFORMS = [Platform.SENSOR] +GiosConfigEntry = ConfigEntry["GiosData"] -async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: + +@dataclass +class GiosData: + """Data for GIOS integration.""" + + coordinator: GiosDataUpdateCoordinator + + +async def async_setup_entry(hass: HomeAssistant, entry: GiosConfigEntry) -> bool: """Set up GIOS as config entry.""" station_id: int = entry.data[CONF_STATION_ID] _LOGGER.debug("Using station_id: %d", station_id) @@ -48,8 +58,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: coordinator = GiosDataUpdateCoordinator(hass, websession, station_id) await coordinator.async_config_entry_first_refresh() - hass.data.setdefault(DOMAIN, {}) - hass.data[DOMAIN][entry.entry_id] = coordinator + entry.runtime_data = GiosData(coordinator) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) @@ -65,14 +74,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: return True -async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_unload_entry(hass: HomeAssistant, entry: GiosConfigEntry) -> bool: """Unload a config entry.""" - unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) - - if unload_ok: - hass.data[DOMAIN].pop(entry.entry_id) - - return unload_ok + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) class GiosDataUpdateCoordinator(DataUpdateCoordinator[GiosSensors]): # pylint: disable=hass-enforce-coordinator-module diff --git a/homeassistant/components/gios/diagnostics.py b/homeassistant/components/gios/diagnostics.py index 0bdd8f3a7ef..a94a95254de 100644 --- a/homeassistant/components/gios/diagnostics.py +++ b/homeassistant/components/gios/diagnostics.py @@ -5,18 +5,16 @@ from __future__ import annotations from dataclasses import asdict from typing import Any -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant -from . import GiosDataUpdateCoordinator -from .const import DOMAIN +from . import GiosConfigEntry async def async_get_config_entry_diagnostics( - hass: HomeAssistant, config_entry: ConfigEntry + hass: HomeAssistant, config_entry: GiosConfigEntry ) -> dict[str, Any]: """Return diagnostics for a config entry.""" - coordinator: GiosDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id] + coordinator = config_entry.runtime_data.coordinator return { "config_entry": config_entry.as_dict(), diff --git a/homeassistant/components/gios/sensor.py b/homeassistant/components/gios/sensor.py index c2da9239453..244e741a086 100644 --- a/homeassistant/components/gios/sensor.py +++ b/homeassistant/components/gios/sensor.py @@ -15,7 +15,6 @@ from homeassistant.components.sensor import ( SensorEntityDescription, SensorStateClass, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, CONF_NAME from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er @@ -24,7 +23,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import StateType from homeassistant.helpers.update_coordinator import CoordinatorEntity -from . import GiosDataUpdateCoordinator +from . import GiosConfigEntry, GiosDataUpdateCoordinator from .const import ( ATTR_AQI, ATTR_C6H6, @@ -159,13 +158,12 @@ SENSOR_TYPES: tuple[GiosSensorEntityDescription, ...] = ( async def async_setup_entry( - hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback + hass: HomeAssistant, entry: GiosConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: """Add a GIOS entities from a config_entry.""" name = entry.data[CONF_NAME] - coordinator = hass.data[DOMAIN][entry.entry_id] - + coordinator = entry.runtime_data.coordinator # Due to the change of the attribute name of one sensor, it is necessary to migrate # the unique_id to the new name. entity_registry = er.async_get(hass)