Store runtime data inside the config entry in DWD (#116764)
This commit is contained in:
parent
a5df229715
commit
e5543e3b95
4 changed files with 24 additions and 19 deletions
|
@ -2,27 +2,27 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import DOMAIN, PLATFORMS
|
||||
from .coordinator import DwdWeatherWarningsCoordinator
|
||||
from .const import PLATFORMS
|
||||
from .coordinator import DwdWeatherWarningsConfigEntry, DwdWeatherWarningsCoordinator
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: DwdWeatherWarningsConfigEntry
|
||||
) -> bool:
|
||||
"""Set up a config entry."""
|
||||
coordinator = DwdWeatherWarningsCoordinator(hass)
|
||||
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: DwdWeatherWarningsConfigEntry
|
||||
) -> 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)
|
||||
|
|
|
@ -19,11 +19,13 @@ from .const import (
|
|||
from .exceptions import EntityNotFoundError
|
||||
from .util import get_position_data
|
||||
|
||||
DwdWeatherWarningsConfigEntry = ConfigEntry["DwdWeatherWarningsCoordinator"]
|
||||
|
||||
|
||||
class DwdWeatherWarningsCoordinator(DataUpdateCoordinator[None]):
|
||||
"""Custom coordinator for the dwd_weather_warnings integration."""
|
||||
|
||||
config_entry: ConfigEntry
|
||||
config_entry: DwdWeatherWarningsConfigEntry
|
||||
api: DwdWeatherWarningsAPI
|
||||
|
||||
def __init__(self, hass: HomeAssistant) -> None:
|
||||
|
|
|
@ -14,7 +14,6 @@ from __future__ import annotations
|
|||
from typing import Any
|
||||
|
||||
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
@ -40,7 +39,7 @@ from .const import (
|
|||
DEFAULT_NAME,
|
||||
DOMAIN,
|
||||
)
|
||||
from .coordinator import DwdWeatherWarningsCoordinator
|
||||
from .coordinator import DwdWeatherWarningsConfigEntry, DwdWeatherWarningsCoordinator
|
||||
|
||||
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||
SensorEntityDescription(
|
||||
|
@ -55,10 +54,12 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
hass: HomeAssistant,
|
||||
entry: DwdWeatherWarningsConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up entities from config entry."""
|
||||
coordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
async_add_entities(
|
||||
[
|
||||
|
@ -80,7 +81,7 @@ class DwdWeatherWarningsSensor(
|
|||
def __init__(
|
||||
self,
|
||||
coordinator: DwdWeatherWarningsCoordinator,
|
||||
entry: ConfigEntry,
|
||||
entry: DwdWeatherWarningsConfigEntry,
|
||||
description: SensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize a DWD-Weather-Warnings sensor."""
|
||||
|
|
|
@ -6,6 +6,9 @@ from homeassistant.components.dwd_weather_warnings.const import (
|
|||
CONF_REGION_DEVICE_TRACKER,
|
||||
DOMAIN,
|
||||
)
|
||||
from homeassistant.components.dwd_weather_warnings.coordinator import (
|
||||
DwdWeatherWarningsCoordinator,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE, STATE_HOME
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
@ -25,13 +28,12 @@ async def test_load_unload_entry(
|
|||
entry = await init_integration(hass, mock_identifier_entry)
|
||||
|
||||
assert entry.state is ConfigEntryState.LOADED
|
||||
assert entry.entry_id in hass.data[DOMAIN]
|
||||
assert isinstance(entry.runtime_data, DwdWeatherWarningsCoordinator)
|
||||
|
||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert entry.state is ConfigEntryState.NOT_LOADED
|
||||
assert entry.entry_id not in hass.data[DOMAIN]
|
||||
|
||||
|
||||
async def test_load_invalid_registry_entry(
|
||||
|
@ -97,4 +99,4 @@ async def test_load_valid_device_tracker(
|
|||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_tracker_entry.state is ConfigEntryState.LOADED
|
||||
assert mock_tracker_entry.entry_id in hass.data[DOMAIN]
|
||||
assert isinstance(mock_tracker_entry.runtime_data, DwdWeatherWarningsCoordinator)
|
||||
|
|
Loading…
Add table
Reference in a new issue