mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 13:57:10 +00:00
Use config entry runtime_data in awair (#127073)
This commit is contained in:
parent
064bbab3f5
commit
dec03d4d25
@ -2,14 +2,13 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_HOST, Platform
|
from homeassistant.const import CONF_HOST, Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
|
|
||||||
from .const import DOMAIN
|
|
||||||
from .coordinator import (
|
from .coordinator import (
|
||||||
AwairCloudDataUpdateCoordinator,
|
AwairCloudDataUpdateCoordinator,
|
||||||
|
AwairConfigEntry,
|
||||||
AwairDataUpdateCoordinator,
|
AwairDataUpdateCoordinator,
|
||||||
AwairLocalDataUpdateCoordinator,
|
AwairLocalDataUpdateCoordinator,
|
||||||
)
|
)
|
||||||
@ -17,7 +16,9 @@ from .coordinator import (
|
|||||||
PLATFORMS = [Platform.SENSOR]
|
PLATFORMS = [Platform.SENSOR]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant, config_entry: AwairConfigEntry
|
||||||
|
) -> bool:
|
||||||
"""Set up Awair integration from a config entry."""
|
"""Set up Awair integration from a config entry."""
|
||||||
session = async_get_clientsession(hass)
|
session = async_get_clientsession(hass)
|
||||||
|
|
||||||
@ -33,28 +34,21 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||||||
|
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
|
|
||||||
hass.data.setdefault(DOMAIN, {})
|
config_entry.runtime_data = coordinator
|
||||||
hass.data[DOMAIN][config_entry.entry_id] = coordinator
|
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
async def _async_update_listener(hass: HomeAssistant, entry: AwairConfigEntry) -> None:
|
||||||
"""Handle options update."""
|
"""Handle options update."""
|
||||||
coordinator: AwairLocalDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
if entry.title != entry.runtime_data.title:
|
||||||
if entry.title != coordinator.title:
|
|
||||||
await hass.config_entries.async_reload(entry.entry_id)
|
await hass.config_entries.async_reload(entry.entry_id)
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
async def async_unload_entry(
|
||||||
|
hass: HomeAssistant, config_entry: AwairConfigEntry
|
||||||
|
) -> bool:
|
||||||
"""Unload Awair configuration."""
|
"""Unload Awair configuration."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(
|
return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
|
||||||
config_entry, PLATFORMS
|
|
||||||
)
|
|
||||||
|
|
||||||
if unload_ok:
|
|
||||||
hass.data[DOMAIN].pop(config_entry.entry_id)
|
|
||||||
|
|
||||||
return unload_ok
|
|
||||||
|
@ -26,6 +26,8 @@ from .const import (
|
|||||||
UPDATE_INTERVAL_LOCAL,
|
UPDATE_INTERVAL_LOCAL,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type AwairConfigEntry = ConfigEntry[AwairDataUpdateCoordinator]
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class AwairResult:
|
class AwairResult:
|
||||||
|
@ -46,7 +46,7 @@ from .const import (
|
|||||||
ATTRIBUTION,
|
ATTRIBUTION,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
)
|
)
|
||||||
from .coordinator import AwairDataUpdateCoordinator, AwairResult
|
from .coordinator import AwairConfigEntry, AwairDataUpdateCoordinator
|
||||||
|
|
||||||
DUST_ALIASES = [API_PM25, API_PM10]
|
DUST_ALIASES = [API_PM25, API_PM10]
|
||||||
|
|
||||||
@ -132,15 +132,14 @@ SENSOR_TYPES_DUST: tuple[AwairSensorEntityDescription, ...] = (
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: AwairConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Awair sensor entity based on a config entry."""
|
"""Set up Awair sensor entity based on a config entry."""
|
||||||
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
coordinator = config_entry.runtime_data
|
||||||
entities = []
|
entities = []
|
||||||
|
|
||||||
data: list[AwairResult] = coordinator.data.values()
|
for result in coordinator.data.values():
|
||||||
for result in data:
|
|
||||||
if result.air_data:
|
if result.air_data:
|
||||||
entities.append(AwairSensor(result.device, coordinator, SENSOR_TYPE_SCORE))
|
entities.append(AwairSensor(result.device, coordinator, SENSOR_TYPE_SCORE))
|
||||||
device_sensors = result.air_data.sensors.keys()
|
device_sensors = result.air_data.sensors.keys()
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from homeassistant.components.awair import DOMAIN
|
from homeassistant.components.awair.const import DOMAIN
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user