mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 07:37:34 +00:00
Migrate WAQI to runtime data (#148977)
This commit is contained in:
parent
3c87a3e892
commit
37a154b1df
@ -4,18 +4,16 @@ from __future__ import annotations
|
||||
|
||||
from aiowaqi import WAQIClient
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_API_KEY, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import WAQIDataUpdateCoordinator
|
||||
from .coordinator import WAQIConfigEntry, WAQIDataUpdateCoordinator
|
||||
|
||||
PLATFORMS: list[Platform] = [Platform.SENSOR]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: WAQIConfigEntry) -> bool:
|
||||
"""Set up World Air Quality Index (WAQI) from a config entry."""
|
||||
|
||||
client = WAQIClient(session=async_get_clientsession(hass))
|
||||
@ -23,16 +21,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
|
||||
waqi_coordinator = WAQIDataUpdateCoordinator(hass, entry, client)
|
||||
await waqi_coordinator.async_config_entry_first_refresh()
|
||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = waqi_coordinator
|
||||
entry.runtime_data = waqi_coordinator
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: WAQIConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
@ -12,14 +12,16 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda
|
||||
|
||||
from .const import CONF_STATION_NUMBER, DOMAIN, LOGGER
|
||||
|
||||
type WAQIConfigEntry = ConfigEntry[WAQIDataUpdateCoordinator]
|
||||
|
||||
|
||||
class WAQIDataUpdateCoordinator(DataUpdateCoordinator[WAQIAirQuality]):
|
||||
"""The WAQI Data Update Coordinator."""
|
||||
|
||||
config_entry: ConfigEntry
|
||||
config_entry: WAQIConfigEntry
|
||||
|
||||
def __init__(
|
||||
self, hass: HomeAssistant, config_entry: ConfigEntry, client: WAQIClient
|
||||
self, hass: HomeAssistant, config_entry: WAQIConfigEntry, client: WAQIClient
|
||||
) -> None:
|
||||
"""Initialize the WAQI data coordinator."""
|
||||
super().__init__(
|
||||
|
@ -14,7 +14,6 @@ from homeassistant.components.sensor import (
|
||||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import PERCENTAGE, UnitOfPressure, UnitOfTemperature
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||
@ -23,7 +22,7 @@ from homeassistant.helpers.typing import StateType
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import WAQIDataUpdateCoordinator
|
||||
from .coordinator import WAQIConfigEntry, WAQIDataUpdateCoordinator
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
@ -127,11 +126,11 @@ SENSORS: list[WAQISensorEntityDescription] = [
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
entry: WAQIConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the WAQI sensor."""
|
||||
coordinator: WAQIDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
coordinator = entry.runtime_data
|
||||
async_add_entities(
|
||||
WaqiSensor(coordinator, sensor)
|
||||
for sensor in SENSORS
|
||||
|
Loading…
x
Reference in New Issue
Block a user