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