Store AccuWeather runtime data in config entry (#116946)
Co-authored-by: Maciej Bieniek <478555+bieniu@users.noreply.github.com>
This commit is contained in:
parent
1ef09048e6
commit
57283d16d9
5 changed files with 29 additions and 33 deletions
|
@ -33,7 +33,10 @@ class AccuWeatherData:
|
||||||
coordinator_daily_forecast: AccuWeatherDailyForecastDataUpdateCoordinator
|
coordinator_daily_forecast: AccuWeatherDailyForecastDataUpdateCoordinator
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
AccuWeatherConfigEntry = ConfigEntry[AccuWeatherData]
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(hass: HomeAssistant, entry: AccuWeatherConfigEntry) -> bool:
|
||||||
"""Set up AccuWeather as config entry."""
|
"""Set up AccuWeather as config entry."""
|
||||||
api_key: str = entry.data[CONF_API_KEY]
|
api_key: str = entry.data[CONF_API_KEY]
|
||||||
name: str = entry.data[CONF_NAME]
|
name: str = entry.data[CONF_NAME]
|
||||||
|
@ -64,7 +67,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
await coordinator_observation.async_config_entry_first_refresh()
|
await coordinator_observation.async_config_entry_first_refresh()
|
||||||
await coordinator_daily_forecast.async_config_entry_first_refresh()
|
await coordinator_daily_forecast.async_config_entry_first_refresh()
|
||||||
|
|
||||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = AccuWeatherData(
|
entry.runtime_data = AccuWeatherData(
|
||||||
coordinator_observation=coordinator_observation,
|
coordinator_observation=coordinator_observation,
|
||||||
coordinator_daily_forecast=coordinator_daily_forecast,
|
coordinator_daily_forecast=coordinator_daily_forecast,
|
||||||
)
|
)
|
||||||
|
@ -82,11 +85,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(
|
||||||
|
hass: HomeAssistant, entry: AccuWeatherConfigEntry
|
||||||
|
) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a 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:
|
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
|
||||||
|
|
||||||
return unload_ok
|
|
||||||
|
|
|
@ -5,21 +5,19 @@ from __future__ import annotations
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
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_API_KEY, CONF_LATITUDE, CONF_LONGITUDE
|
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from . import AccuWeatherData
|
from . import AccuWeatherConfigEntry, AccuWeatherData
|
||||||
from .const import DOMAIN
|
|
||||||
|
|
||||||
TO_REDACT = {CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE}
|
TO_REDACT = {CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE}
|
||||||
|
|
||||||
|
|
||||||
async def async_get_config_entry_diagnostics(
|
async def async_get_config_entry_diagnostics(
|
||||||
hass: HomeAssistant, config_entry: ConfigEntry
|
hass: HomeAssistant, config_entry: AccuWeatherConfigEntry
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Return diagnostics for a config entry."""
|
"""Return diagnostics for a config entry."""
|
||||||
accuweather_data: AccuWeatherData = hass.data[DOMAIN][config_entry.entry_id]
|
accuweather_data: AccuWeatherData = config_entry.runtime_data
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"config_entry_data": async_redact_data(dict(config_entry.data), TO_REDACT),
|
"config_entry_data": async_redact_data(dict(config_entry.data), TO_REDACT),
|
||||||
|
|
|
@ -12,7 +12,6 @@ from homeassistant.components.sensor import (
|
||||||
SensorEntityDescription,
|
SensorEntityDescription,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONCENTRATION_PARTS_PER_CUBIC_METER,
|
CONCENTRATION_PARTS_PER_CUBIC_METER,
|
||||||
PERCENTAGE,
|
PERCENTAGE,
|
||||||
|
@ -28,7 +27,7 @@ from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from . import AccuWeatherData
|
from . import AccuWeatherConfigEntry
|
||||||
from .const import (
|
from .const import (
|
||||||
API_METRIC,
|
API_METRIC,
|
||||||
ATTR_CATEGORY,
|
ATTR_CATEGORY,
|
||||||
|
@ -38,7 +37,6 @@ from .const import (
|
||||||
ATTR_SPEED,
|
ATTR_SPEED,
|
||||||
ATTR_VALUE,
|
ATTR_VALUE,
|
||||||
ATTRIBUTION,
|
ATTRIBUTION,
|
||||||
DOMAIN,
|
|
||||||
MAX_FORECAST_DAYS,
|
MAX_FORECAST_DAYS,
|
||||||
)
|
)
|
||||||
from .coordinator import (
|
from .coordinator import (
|
||||||
|
@ -458,17 +456,16 @@ SENSOR_TYPES: tuple[AccuWeatherSensorDescription, ...] = (
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant,
|
||||||
|
entry: AccuWeatherConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Add AccuWeather entities from a config_entry."""
|
"""Add AccuWeather entities from a config_entry."""
|
||||||
|
|
||||||
accuweather_data: AccuWeatherData = hass.data[DOMAIN][entry.entry_id]
|
|
||||||
|
|
||||||
observation_coordinator: AccuWeatherObservationDataUpdateCoordinator = (
|
observation_coordinator: AccuWeatherObservationDataUpdateCoordinator = (
|
||||||
accuweather_data.coordinator_observation
|
entry.runtime_data.coordinator_observation
|
||||||
)
|
)
|
||||||
forecast_daily_coordinator: AccuWeatherDailyForecastDataUpdateCoordinator = (
|
forecast_daily_coordinator: AccuWeatherDailyForecastDataUpdateCoordinator = (
|
||||||
accuweather_data.coordinator_daily_forecast
|
entry.runtime_data.coordinator_daily_forecast
|
||||||
)
|
)
|
||||||
|
|
||||||
sensors: list[AccuWeatherSensor | AccuWeatherForecastSensor] = [
|
sensors: list[AccuWeatherSensor | AccuWeatherForecastSensor] = [
|
||||||
|
|
|
@ -9,6 +9,7 @@ from accuweather.const import ENDPOINT
|
||||||
from homeassistant.components import system_health
|
from homeassistant.components import system_health
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
|
|
||||||
|
from . import AccuWeatherConfigEntry
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
|
||||||
|
|
||||||
|
@ -22,9 +23,11 @@ def async_register(
|
||||||
|
|
||||||
async def system_health_info(hass: HomeAssistant) -> dict[str, Any]:
|
async def system_health_info(hass: HomeAssistant) -> dict[str, Any]:
|
||||||
"""Get info for the info page."""
|
"""Get info for the info page."""
|
||||||
remaining_requests = list(hass.data[DOMAIN].values())[
|
config_entry: AccuWeatherConfigEntry = hass.config_entries.async_entries(DOMAIN)[0]
|
||||||
0
|
|
||||||
].coordinator_observation.accuweather.requests_remaining
|
remaining_requests = (
|
||||||
|
config_entry.runtime_data.coordinator_observation.accuweather.requests_remaining
|
||||||
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"can_reach_server": system_health.async_check_can_reach_url(hass, ENDPOINT),
|
"can_reach_server": system_health.async_check_can_reach_url(hass, ENDPOINT),
|
||||||
|
|
|
@ -21,7 +21,6 @@ from homeassistant.components.weather import (
|
||||||
Forecast,
|
Forecast,
|
||||||
WeatherEntityFeature,
|
WeatherEntityFeature,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
UnitOfLength,
|
UnitOfLength,
|
||||||
UnitOfPrecipitationDepth,
|
UnitOfPrecipitationDepth,
|
||||||
|
@ -33,7 +32,7 @@ from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.util.dt import utc_from_timestamp
|
from homeassistant.util.dt import utc_from_timestamp
|
||||||
|
|
||||||
from . import AccuWeatherData
|
from . import AccuWeatherConfigEntry, AccuWeatherData
|
||||||
from .const import (
|
from .const import (
|
||||||
API_METRIC,
|
API_METRIC,
|
||||||
ATTR_DIRECTION,
|
ATTR_DIRECTION,
|
||||||
|
@ -41,7 +40,6 @@ from .const import (
|
||||||
ATTR_VALUE,
|
ATTR_VALUE,
|
||||||
ATTRIBUTION,
|
ATTRIBUTION,
|
||||||
CONDITION_MAP,
|
CONDITION_MAP,
|
||||||
DOMAIN,
|
|
||||||
)
|
)
|
||||||
from .coordinator import (
|
from .coordinator import (
|
||||||
AccuWeatherDailyForecastDataUpdateCoordinator,
|
AccuWeatherDailyForecastDataUpdateCoordinator,
|
||||||
|
@ -52,12 +50,12 @@ PARALLEL_UPDATES = 1
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant,
|
||||||
|
entry: AccuWeatherConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Add a AccuWeather weather entity from a config_entry."""
|
"""Add a AccuWeather weather entity from a config_entry."""
|
||||||
accuweather_data: AccuWeatherData = hass.data[DOMAIN][entry.entry_id]
|
async_add_entities([AccuWeatherEntity(entry.runtime_data)])
|
||||||
|
|
||||||
async_add_entities([AccuWeatherEntity(accuweather_data)])
|
|
||||||
|
|
||||||
|
|
||||||
class AccuWeatherEntity(
|
class AccuWeatherEntity(
|
||||||
|
|
Loading…
Add table
Reference in a new issue