Store GIOS runtime data in entry (#116510)

Use entry.runtime_data

Co-authored-by: Maciej Bieniek <478555+bieniu@users.noreply.github.com>
This commit is contained in:
Maciej Bieniek 2024-05-01 17:28:20 +02:00 committed by GitHub
parent 2ad6353bf8
commit 9e8f7b5618
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 20 deletions

View File

@ -3,6 +3,7 @@
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
from dataclasses import dataclass
import logging import logging
from aiohttp import ClientSession from aiohttp import ClientSession
@ -25,8 +26,17 @@ _LOGGER = logging.getLogger(__name__)
PLATFORMS = [Platform.SENSOR] 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.""" """Set up GIOS as config entry."""
station_id: int = entry.data[CONF_STATION_ID] station_id: int = entry.data[CONF_STATION_ID]
_LOGGER.debug("Using station_id: %d", 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) coordinator = GiosDataUpdateCoordinator(hass, websession, station_id)
await coordinator.async_config_entry_first_refresh() await coordinator.async_config_entry_first_refresh()
hass.data.setdefault(DOMAIN, {}) entry.runtime_data = GiosData(coordinator)
hass.data[DOMAIN][entry.entry_id] = coordinator
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) 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 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 a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
class GiosDataUpdateCoordinator(DataUpdateCoordinator[GiosSensors]): # pylint: disable=hass-enforce-coordinator-module class GiosDataUpdateCoordinator(DataUpdateCoordinator[GiosSensors]): # pylint: disable=hass-enforce-coordinator-module

View File

@ -5,18 +5,16 @@ from __future__ import annotations
from dataclasses import asdict from dataclasses import asdict
from typing import Any from typing import Any
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from . import GiosDataUpdateCoordinator from . import GiosConfigEntry
from .const import DOMAIN
async def async_get_config_entry_diagnostics( async def async_get_config_entry_diagnostics(
hass: HomeAssistant, config_entry: ConfigEntry hass: HomeAssistant, config_entry: GiosConfigEntry
) -> dict[str, Any]: ) -> dict[str, Any]:
"""Return diagnostics for a config entry.""" """Return diagnostics for a config entry."""
coordinator: GiosDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id] coordinator = config_entry.runtime_data.coordinator
return { return {
"config_entry": config_entry.as_dict(), "config_entry": config_entry.as_dict(),

View File

@ -15,7 +15,6 @@ from homeassistant.components.sensor import (
SensorEntityDescription, SensorEntityDescription,
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, CONF_NAME from homeassistant.const import CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, CONF_NAME
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er 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.typing import StateType
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import GiosDataUpdateCoordinator from . import GiosConfigEntry, GiosDataUpdateCoordinator
from .const import ( from .const import (
ATTR_AQI, ATTR_AQI,
ATTR_C6H6, ATTR_C6H6,
@ -159,13 +158,12 @@ SENSOR_TYPES: tuple[GiosSensorEntityDescription, ...] = (
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant, entry: GiosConfigEntry, async_add_entities: AddEntitiesCallback
) -> None: ) -> None:
"""Add a GIOS entities from a config_entry.""" """Add a GIOS entities from a config_entry."""
name = entry.data[CONF_NAME] 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 # Due to the change of the attribute name of one sensor, it is necessary to migrate
# the unique_id to the new name. # the unique_id to the new name.
entity_registry = er.async_get(hass) entity_registry = er.async_get(hass)