Use config entry runtime data in Open-Meteo (#134198)
parent
88d366b0c5
commit
1e652db37f
|
@ -2,31 +2,27 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import OpenMeteoDataUpdateCoordinator
|
||||
from .coordinator import OpenMeteoConfigEntry, OpenMeteoDataUpdateCoordinator
|
||||
|
||||
PLATFORMS = [Platform.WEATHER]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: OpenMeteoConfigEntry) -> bool:
|
||||
"""Set up Open-Meteo from a config entry."""
|
||||
|
||||
coordinator = OpenMeteoDataUpdateCoordinator(hass, entry)
|
||||
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: OpenMeteoConfigEntry) -> bool:
|
||||
"""Unload Open-Meteo config entry."""
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
if unload_ok:
|
||||
del hass.data[DOMAIN][entry.entry_id]
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
|
|
@ -21,13 +21,15 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda
|
|||
|
||||
from .const import DOMAIN, LOGGER, SCAN_INTERVAL
|
||||
|
||||
type OpenMeteoConfigEntry = ConfigEntry[OpenMeteoDataUpdateCoordinator]
|
||||
|
||||
|
||||
class OpenMeteoDataUpdateCoordinator(DataUpdateCoordinator[Forecast]):
|
||||
"""A Open-Meteo Data Update Coordinator."""
|
||||
|
||||
config_entry: ConfigEntry
|
||||
config_entry: OpenMeteoConfigEntry
|
||||
|
||||
def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None:
|
||||
def __init__(self, hass: HomeAssistant, config_entry: OpenMeteoConfigEntry) -> None:
|
||||
"""Initialize the Open-Meteo coordinator."""
|
||||
super().__init__(
|
||||
hass,
|
||||
|
|
|
@ -4,15 +4,11 @@ from __future__ import annotations
|
|||
|
||||
from typing import Any
|
||||
|
||||
from open_meteo import Forecast
|
||||
|
||||
from homeassistant.components.diagnostics import async_redact_data
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import OpenMeteoConfigEntry
|
||||
|
||||
TO_REDACT = {
|
||||
CONF_LATITUDE,
|
||||
|
@ -21,8 +17,8 @@ TO_REDACT = {
|
|||
|
||||
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant, entry: ConfigEntry
|
||||
hass: HomeAssistant, entry: OpenMeteoConfigEntry
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
coordinator: DataUpdateCoordinator[Forecast] = hass.data[DOMAIN][entry.entry_id]
|
||||
coordinator = entry.runtime_data
|
||||
return async_redact_data(coordinator.data.to_dict(), TO_REDACT)
|
||||
|
|
|
@ -15,7 +15,6 @@ from homeassistant.components.weather import (
|
|||
SingleCoordinatorWeatherEntity,
|
||||
WeatherEntityFeature,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import UnitOfPrecipitationDepth, UnitOfSpeed, UnitOfTemperature
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||
|
@ -24,15 +23,16 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
|||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from .const import DOMAIN, WMO_TO_HA_CONDITION_MAP
|
||||
from .coordinator import OpenMeteoConfigEntry
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
entry: OpenMeteoConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up Open-Meteo weather entity based on a config entry."""
|
||||
coordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
coordinator = entry.runtime_data
|
||||
async_add_entities([OpenMeteoWeatherEntity(entry=entry, coordinator=coordinator)])
|
||||
|
||||
|
||||
|
@ -53,7 +53,7 @@ class OpenMeteoWeatherEntity(
|
|||
def __init__(
|
||||
self,
|
||||
*,
|
||||
entry: ConfigEntry,
|
||||
entry: OpenMeteoConfigEntry,
|
||||
coordinator: DataUpdateCoordinator[OpenMeteoForecast],
|
||||
) -> None:
|
||||
"""Initialize Open-Meteo weather entity."""
|
||||
|
|
Loading…
Reference in New Issue