Use runtime_data for airthings_ble (#116623)

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Marc Mueller 2024-05-02 16:38:01 +02:00 committed by GitHub
parent adfd63be8c
commit 0977ad017d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 19 deletions

View File

@ -22,8 +22,13 @@ PLATFORMS: list[Platform] = [Platform.SENSOR]
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
AirthingsBLEDataUpdateCoordinator = DataUpdateCoordinator[AirthingsDevice]
AirthingsBLEConfigEntry = ConfigEntry[AirthingsBLEDataUpdateCoordinator]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(
hass: HomeAssistant, entry: AirthingsBLEConfigEntry
) -> bool:
"""Set up Airthings BLE device from a config entry.""" """Set up Airthings BLE device from a config entry."""
hass.data.setdefault(DOMAIN, {}) hass.data.setdefault(DOMAIN, {})
address = entry.unique_id address = entry.unique_id
@ -51,7 +56,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return data return data
coordinator = DataUpdateCoordinator( coordinator: AirthingsBLEDataUpdateCoordinator = DataUpdateCoordinator(
hass, hass,
_LOGGER, _LOGGER,
name=DOMAIN, name=DOMAIN,
@ -61,16 +66,15 @@ 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: AirthingsBLEConfigEntry
) -> bool:
"""Unload a config entry.""" """Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok

View File

@ -13,7 +13,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_PARTS_PER_BILLION, CONCENTRATION_PARTS_PER_BILLION,
CONCENTRATION_PARTS_PER_MILLION, CONCENTRATION_PARTS_PER_MILLION,
@ -36,12 +35,10 @@ from homeassistant.helpers.entity_registry import (
async_get as entity_async_get, async_get as entity_async_get,
) )
from homeassistant.helpers.typing import StateType from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import ( from homeassistant.helpers.update_coordinator import CoordinatorEntity
CoordinatorEntity,
DataUpdateCoordinator,
)
from homeassistant.util.unit_system import METRIC_SYSTEM from homeassistant.util.unit_system import METRIC_SYSTEM
from . import AirthingsBLEConfigEntry, AirthingsBLEDataUpdateCoordinator
from .const import DOMAIN, VOLUME_BECQUEREL, VOLUME_PICOCURIE from .const import DOMAIN, VOLUME_BECQUEREL, VOLUME_PICOCURIE
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -152,15 +149,13 @@ def async_migrate(hass: HomeAssistant, address: str, sensor_name: str) -> None:
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: AirthingsBLEConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up the Airthings BLE sensors.""" """Set up the Airthings BLE sensors."""
is_metric = hass.config.units is METRIC_SYSTEM is_metric = hass.config.units is METRIC_SYSTEM
coordinator: DataUpdateCoordinator[AirthingsDevice] = hass.data[DOMAIN][ coordinator = entry.runtime_data
entry.entry_id
]
# we need to change some units # we need to change some units
sensors_mapping = SENSORS_MAPPING_TEMPLATE.copy() sensors_mapping = SENSORS_MAPPING_TEMPLATE.copy()
@ -193,7 +188,7 @@ async def async_setup_entry(
class AirthingsSensor( class AirthingsSensor(
CoordinatorEntity[DataUpdateCoordinator[AirthingsDevice]], SensorEntity CoordinatorEntity[AirthingsBLEDataUpdateCoordinator], SensorEntity
): ):
"""Airthings BLE sensors for the device.""" """Airthings BLE sensors for the device."""
@ -201,7 +196,7 @@ class AirthingsSensor(
def __init__( def __init__(
self, self,
coordinator: DataUpdateCoordinator[AirthingsDevice], coordinator: AirthingsBLEDataUpdateCoordinator,
airthings_device: AirthingsDevice, airthings_device: AirthingsDevice,
entity_description: SensorEntityDescription, entity_description: SensorEntityDescription,
) -> None: ) -> None: