Use runtime_data for airthings (#116622)

Co-authored-by: Matthias Alphart <farmio@alphart.net>
This commit is contained in:
Marc Mueller 2024-05-02 20:59:34 +02:00 committed by GitHub
parent 1c65aacde5
commit f2b7733e8c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 14 deletions

View File

@ -22,11 +22,11 @@ SCAN_INTERVAL = timedelta(minutes=6)
AirthingsDataCoordinatorType = DataUpdateCoordinator[dict[str, AirthingsDevice]] AirthingsDataCoordinatorType = DataUpdateCoordinator[dict[str, AirthingsDevice]]
AirthingsConfigEntry = ConfigEntry[AirthingsDataCoordinatorType]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: AirthingsConfigEntry) -> bool:
"""Set up Airthings from a config entry.""" """Set up Airthings from a config entry."""
hass.data.setdefault(DOMAIN, {})
airthings = Airthings( airthings = Airthings(
entry.data[CONF_ID], entry.data[CONF_ID],
entry.data[CONF_SECRET], entry.data[CONF_SECRET],
@ -49,17 +49,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
) )
await coordinator.async_config_entry_first_refresh() await coordinator.async_config_entry_first_refresh()
hass.data[DOMAIN][entry.entry_id] = coordinator entry.runtime_data = coordinator
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: AirthingsConfigEntry) -> 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

View File

@ -10,7 +10,6 @@ from homeassistant.components.sensor import (
SensorEntityDescription, SensorEntityDescription,
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
CONCENTRATION_PARTS_PER_BILLION, CONCENTRATION_PARTS_PER_BILLION,
@ -27,7 +26,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 AirthingsDataCoordinatorType from . import AirthingsConfigEntry, AirthingsDataCoordinatorType
from .const import DOMAIN from .const import DOMAIN
SENSORS: dict[str, SensorEntityDescription] = { SENSORS: dict[str, SensorEntityDescription] = {
@ -102,12 +101,12 @@ SENSORS: dict[str, SensorEntityDescription] = {
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: AirthingsConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up the Airthings sensor.""" """Set up the Airthings sensor."""
coordinator: AirthingsDataCoordinatorType = hass.data[DOMAIN][entry.entry_id] coordinator = entry.runtime_data
entities = [ entities = [
AirthingsHeaterEnergySensor( AirthingsHeaterEnergySensor(
coordinator, coordinator,