Store runtime data inside the config entry in DWD (#116764)

This commit is contained in:
Michael 2024-05-04 12:36:28 +02:00 committed by GitHub
parent a5df229715
commit e5543e3b95
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 24 additions and 19 deletions

View file

@ -2,27 +2,27 @@
from __future__ import annotations from __future__ import annotations
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from .const import DOMAIN, PLATFORMS from .const import PLATFORMS
from .coordinator import DwdWeatherWarningsCoordinator 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.""" """Set up a config entry."""
coordinator = DwdWeatherWarningsCoordinator(hass) coordinator = DwdWeatherWarningsCoordinator(hass)
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: DwdWeatherWarningsConfigEntry
) -> 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

View file

@ -19,11 +19,13 @@ from .const import (
from .exceptions import EntityNotFoundError from .exceptions import EntityNotFoundError
from .util import get_position_data from .util import get_position_data
DwdWeatherWarningsConfigEntry = ConfigEntry["DwdWeatherWarningsCoordinator"]
class DwdWeatherWarningsCoordinator(DataUpdateCoordinator[None]): class DwdWeatherWarningsCoordinator(DataUpdateCoordinator[None]):
"""Custom coordinator for the dwd_weather_warnings integration.""" """Custom coordinator for the dwd_weather_warnings integration."""
config_entry: ConfigEntry config_entry: DwdWeatherWarningsConfigEntry
api: DwdWeatherWarningsAPI api: DwdWeatherWarningsAPI
def __init__(self, hass: HomeAssistant) -> None: def __init__(self, hass: HomeAssistant) -> None:

View file

@ -14,7 +14,6 @@ from __future__ import annotations
from typing import Any from typing import Any
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.config_entries import ConfigEntry
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
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -40,7 +39,7 @@ from .const import (
DEFAULT_NAME, DEFAULT_NAME,
DOMAIN, DOMAIN,
) )
from .coordinator import DwdWeatherWarningsCoordinator from .coordinator import DwdWeatherWarningsConfigEntry, DwdWeatherWarningsCoordinator
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
@ -55,10 +54,12 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant,
entry: DwdWeatherWarningsConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up entities from config entry.""" """Set up entities from config entry."""
coordinator = hass.data[DOMAIN][entry.entry_id] coordinator = entry.runtime_data
async_add_entities( async_add_entities(
[ [
@ -80,7 +81,7 @@ class DwdWeatherWarningsSensor(
def __init__( def __init__(
self, self,
coordinator: DwdWeatherWarningsCoordinator, coordinator: DwdWeatherWarningsCoordinator,
entry: ConfigEntry, entry: DwdWeatherWarningsConfigEntry,
description: SensorEntityDescription, description: SensorEntityDescription,
) -> None: ) -> None:
"""Initialize a DWD-Weather-Warnings sensor.""" """Initialize a DWD-Weather-Warnings sensor."""

View file

@ -6,6 +6,9 @@ from homeassistant.components.dwd_weather_warnings.const import (
CONF_REGION_DEVICE_TRACKER, CONF_REGION_DEVICE_TRACKER,
DOMAIN, DOMAIN,
) )
from homeassistant.components.dwd_weather_warnings.coordinator import (
DwdWeatherWarningsCoordinator,
)
from homeassistant.config_entries import ConfigEntryState from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE, STATE_HOME from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE, STATE_HOME
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -25,13 +28,12 @@ async def test_load_unload_entry(
entry = await init_integration(hass, mock_identifier_entry) entry = await init_integration(hass, mock_identifier_entry)
assert entry.state is ConfigEntryState.LOADED 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) assert await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
assert entry.state is ConfigEntryState.NOT_LOADED assert entry.state is ConfigEntryState.NOT_LOADED
assert entry.entry_id not in hass.data[DOMAIN]
async def test_load_invalid_registry_entry( async def test_load_invalid_registry_entry(
@ -97,4 +99,4 @@ async def test_load_valid_device_tracker(
await hass.async_block_till_done() await hass.async_block_till_done()
assert mock_tracker_entry.state is ConfigEntryState.LOADED 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)