Store runtime data inside the config entry in Met.no (#116767)

This commit is contained in:
Michael 2024-05-04 18:59:01 +02:00 committed by GitHub
parent 985fd49909
commit 831282c3ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 17 deletions

View File

@ -21,8 +21,12 @@ PLATFORMS = [Platform.WEATHER]
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
MetWeatherConfigEntry = ConfigEntry[MetDataUpdateCoordinator]
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
async def async_setup_entry(
hass: HomeAssistant, config_entry: MetWeatherConfigEntry
) -> bool:
"""Set up Met as config entry.""" """Set up Met as config entry."""
# Don't setup if tracking home location and latitude or longitude isn't set. # Don't setup if tracking home location and latitude or longitude isn't set.
# Also, filters out our onboarding default location. # Also, filters out our onboarding default location.
@ -44,10 +48,10 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
if config_entry.data.get(CONF_TRACK_HOME, False): if config_entry.data.get(CONF_TRACK_HOME, False):
coordinator.track_home() coordinator.track_home()
hass.data.setdefault(DOMAIN, {}) config_entry.runtime_data = coordinator
hass.data[DOMAIN][config_entry.entry_id] = coordinator
config_entry.async_on_unload(config_entry.add_update_listener(async_update_entry)) config_entry.async_on_unload(config_entry.add_update_listener(async_update_entry))
config_entry.async_on_unload(coordinator.untrack_home)
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
@ -56,19 +60,14 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
return True return True
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: async def async_unload_entry(
hass: HomeAssistant, config_entry: MetWeatherConfigEntry
) -> bool:
"""Unload a config entry.""" """Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms( return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
config_entry, PLATFORMS
)
hass.data[DOMAIN][config_entry.entry_id].untrack_home()
hass.data[DOMAIN].pop(config_entry.entry_id)
return unload_ok
async def async_update_entry(hass: HomeAssistant, config_entry: ConfigEntry): async def async_update_entry(hass: HomeAssistant, config_entry: MetWeatherConfigEntry):
"""Reload Met component when options changed.""" """Reload Met component when options changed."""
await hass.config_entries.async_reload(config_entry.entry_id) await hass.config_entries.async_reload(config_entry.entry_id)

View File

@ -21,7 +21,6 @@ from homeassistant.components.weather import (
SingleCoordinatorWeatherEntity, SingleCoordinatorWeatherEntity,
WeatherEntityFeature, WeatherEntityFeature,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
CONF_LATITUDE, CONF_LATITUDE,
CONF_LONGITUDE, CONF_LONGITUDE,
@ -37,6 +36,7 @@ from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.unit_system import METRIC_SYSTEM from homeassistant.util.unit_system import METRIC_SYSTEM
from . import MetWeatherConfigEntry
from .const import ( from .const import (
ATTR_CONDITION_CLEAR_NIGHT, ATTR_CONDITION_CLEAR_NIGHT,
ATTR_CONDITION_SUNNY, ATTR_CONDITION_SUNNY,
@ -53,11 +53,11 @@ DEFAULT_NAME = "Met.no"
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: MetWeatherConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Add a weather entity from a config_entry.""" """Add a weather entity from a config_entry."""
coordinator: MetDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id] coordinator = config_entry.runtime_data
entity_registry = er.async_get(hass) entity_registry = er.async_get(hass)
name: str | None name: str | None
@ -120,7 +120,7 @@ class MetWeather(SingleCoordinatorWeatherEntity[MetDataUpdateCoordinator]):
def __init__( def __init__(
self, self,
coordinator: MetDataUpdateCoordinator, coordinator: MetDataUpdateCoordinator,
config_entry: ConfigEntry, config_entry: MetWeatherConfigEntry,
name: str, name: str,
is_metric: bool, is_metric: bool,
) -> None: ) -> None: