mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 02:37:08 +00:00
Use runtime_data in hko (#144368)
This commit is contained in:
parent
65ad39f5be
commit
9a332f19c2
@ -4,18 +4,17 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from hko import LOCATIONS
|
from hko import LOCATIONS
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_LOCATION, Platform
|
from homeassistant.const import CONF_LOCATION, 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 DEFAULT_DISTRICT, DOMAIN, KEY_DISTRICT, KEY_LOCATION
|
from .const import DEFAULT_DISTRICT, KEY_DISTRICT, KEY_LOCATION
|
||||||
from .coordinator import HKOUpdateCoordinator
|
from .coordinator import HKOConfigEntry, HKOUpdateCoordinator
|
||||||
|
|
||||||
PLATFORMS: list[Platform] = [Platform.WEATHER]
|
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."""
|
"""Set up Hong Kong Observatory from a config entry."""
|
||||||
|
|
||||||
location = entry.data[CONF_LOCATION]
|
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)
|
coordinator = HKOUpdateCoordinator(hass, entry, websession, district, location)
|
||||||
await coordinator.async_config_entry_first_refresh()
|
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)
|
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: HKOConfigEntry) -> 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
|
|
||||||
|
@ -65,16 +65,18 @@ from .const import (
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
type HKOConfigEntry = ConfigEntry[HKOUpdateCoordinator]
|
||||||
|
|
||||||
|
|
||||||
class HKOUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
class HKOUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
||||||
"""HKO Update Coordinator."""
|
"""HKO Update Coordinator."""
|
||||||
|
|
||||||
config_entry: ConfigEntry
|
config_entry: HKOConfigEntry
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: HKOConfigEntry,
|
||||||
session: ClientSession,
|
session: ClientSession,
|
||||||
district: str,
|
district: str,
|
||||||
location: str,
|
location: str,
|
||||||
|
@ -5,7 +5,6 @@ from homeassistant.components.weather import (
|
|||||||
WeatherEntity,
|
WeatherEntity,
|
||||||
WeatherEntityFeature,
|
WeatherEntityFeature,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import UnitOfTemperature
|
from homeassistant.const import UnitOfTemperature
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||||
@ -22,19 +21,18 @@ from .const import (
|
|||||||
DOMAIN,
|
DOMAIN,
|
||||||
MANUFACTURER,
|
MANUFACTURER,
|
||||||
)
|
)
|
||||||
from .coordinator import HKOUpdateCoordinator
|
from .coordinator import HKOConfigEntry, HKOUpdateCoordinator
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: HKOConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Add a HKO weather entity from a config_entry."""
|
"""Add a HKO weather entity from a config_entry."""
|
||||||
assert config_entry.unique_id is not None
|
assert config_entry.unique_id is not None
|
||||||
unique_id = config_entry.unique_id
|
unique_id = config_entry.unique_id
|
||||||
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
async_add_entities([HKOEntity(unique_id, config_entry.runtime_data)], False)
|
||||||
async_add_entities([HKOEntity(unique_id, coordinator)], False)
|
|
||||||
|
|
||||||
|
|
||||||
class HKOEntity(CoordinatorEntity[HKOUpdateCoordinator], WeatherEntity):
|
class HKOEntity(CoordinatorEntity[HKOUpdateCoordinator], WeatherEntity):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user