Use runtime_data in hko (#144368)

This commit is contained in:
epenet 2025-05-07 10:47:36 +02:00 committed by GitHub
parent 65ad39f5be
commit 9a332f19c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 17 deletions

View File

@ -4,18 +4,17 @@ from __future__ import annotations
from hko import LOCATIONS
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_LOCATION, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import DEFAULT_DISTRICT, DOMAIN, KEY_DISTRICT, KEY_LOCATION
from .coordinator import HKOUpdateCoordinator
from .const import DEFAULT_DISTRICT, KEY_DISTRICT, KEY_LOCATION
from .coordinator import HKOConfigEntry, HKOUpdateCoordinator
PLATFORMS: list[Platform] = [Platform.WEATHER]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: HKOConfigEntry) -> bool:
"""Set up Hong Kong Observatory from a config entry."""
location = entry.data[CONF_LOCATION]
@ -27,16 +26,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
coordinator = HKOUpdateCoordinator(hass, entry, websession, district, location)
await coordinator.async_config_entry_first_refresh()
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
entry.runtime_data = 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: HKOConfigEntry) -> 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)

View File

@ -65,16 +65,18 @@ from .const import (
_LOGGER = logging.getLogger(__name__)
type HKOConfigEntry = ConfigEntry[HKOUpdateCoordinator]
class HKOUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]):
"""HKO Update Coordinator."""
config_entry: ConfigEntry
config_entry: HKOConfigEntry
def __init__(
self,
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: HKOConfigEntry,
session: ClientSession,
district: str,
location: str,

View File

@ -5,7 +5,6 @@ from homeassistant.components.weather import (
WeatherEntity,
WeatherEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
@ -22,19 +21,18 @@ from .const import (
DOMAIN,
MANUFACTURER,
)
from .coordinator import HKOUpdateCoordinator
from .coordinator import HKOConfigEntry, HKOUpdateCoordinator
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: HKOConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Add a HKO weather entity from a config_entry."""
assert config_entry.unique_id is not None
unique_id = config_entry.unique_id
coordinator = hass.data[DOMAIN][config_entry.entry_id]
async_add_entities([HKOEntity(unique_id, coordinator)], False)
async_add_entities([HKOEntity(unique_id, config_entry.runtime_data)], False)
class HKOEntity(CoordinatorEntity[HKOUpdateCoordinator], WeatherEntity):