mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
Store runtime data inside the config entry in Forecast Solar (#117033)
This commit is contained in:
parent
0ea1474556
commit
a43fe71413
@ -11,12 +11,13 @@ from .const import (
|
|||||||
CONF_DAMPING_EVENING,
|
CONF_DAMPING_EVENING,
|
||||||
CONF_DAMPING_MORNING,
|
CONF_DAMPING_MORNING,
|
||||||
CONF_MODULES_POWER,
|
CONF_MODULES_POWER,
|
||||||
DOMAIN,
|
|
||||||
)
|
)
|
||||||
from .coordinator import ForecastSolarDataUpdateCoordinator
|
from .coordinator import ForecastSolarDataUpdateCoordinator
|
||||||
|
|
||||||
PLATFORMS = [Platform.SENSOR]
|
PLATFORMS = [Platform.SENSOR]
|
||||||
|
|
||||||
|
ForecastSolarConfigEntry = ConfigEntry[ForecastSolarDataUpdateCoordinator]
|
||||||
|
|
||||||
|
|
||||||
async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Migrate old config entry."""
|
"""Migrate old config entry."""
|
||||||
@ -36,12 +37,14 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant, entry: ForecastSolarConfigEntry
|
||||||
|
) -> bool:
|
||||||
"""Set up Forecast.Solar from a config entry."""
|
"""Set up Forecast.Solar from a config entry."""
|
||||||
coordinator = ForecastSolarDataUpdateCoordinator(hass, entry)
|
coordinator = ForecastSolarDataUpdateCoordinator(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)
|
||||||
|
|
||||||
@ -52,11 +55,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> 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
|
|
||||||
|
|
||||||
|
|
||||||
async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
||||||
|
@ -4,15 +4,11 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from forecast_solar import Estimate
|
|
||||||
|
|
||||||
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 homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
|
||||||
|
|
||||||
from .const import DOMAIN
|
from . import ForecastSolarConfigEntry
|
||||||
|
|
||||||
TO_REDACT = {
|
TO_REDACT = {
|
||||||
CONF_API_KEY,
|
CONF_API_KEY,
|
||||||
@ -22,10 +18,10 @@ TO_REDACT = {
|
|||||||
|
|
||||||
|
|
||||||
async def async_get_config_entry_diagnostics(
|
async def async_get_config_entry_diagnostics(
|
||||||
hass: HomeAssistant, entry: ConfigEntry
|
hass: HomeAssistant, entry: ForecastSolarConfigEntry
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Return diagnostics for a config entry."""
|
"""Return diagnostics for a config entry."""
|
||||||
coordinator: DataUpdateCoordinator[Estimate] = hass.data[DOMAIN][entry.entry_id]
|
coordinator = entry.runtime_data
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"entry": {
|
"entry": {
|
||||||
|
@ -4,19 +4,21 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import ForecastSolarDataUpdateCoordinator
|
||||||
|
|
||||||
|
|
||||||
async def async_get_solar_forecast(
|
async def async_get_solar_forecast(
|
||||||
hass: HomeAssistant, config_entry_id: str
|
hass: HomeAssistant, config_entry_id: str
|
||||||
) -> dict[str, dict[str, float | int]] | None:
|
) -> dict[str, dict[str, float | int]] | None:
|
||||||
"""Get solar forecast for a config entry ID."""
|
"""Get solar forecast for a config entry ID."""
|
||||||
if (coordinator := hass.data[DOMAIN].get(config_entry_id)) is None:
|
if (
|
||||||
|
entry := hass.config_entries.async_get_entry(config_entry_id)
|
||||||
|
) is None or not isinstance(entry.runtime_data, ForecastSolarDataUpdateCoordinator):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"wh_hours": {
|
"wh_hours": {
|
||||||
timestamp.isoformat(): val
|
timestamp.isoformat(): val
|
||||||
for timestamp, val in coordinator.data.wh_period.items()
|
for timestamp, val in entry.runtime_data.data.wh_period.items()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,6 @@ from homeassistant.components.sensor import (
|
|||||||
SensorEntityDescription,
|
SensorEntityDescription,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import UnitOfEnergy, UnitOfPower
|
from homeassistant.const import UnitOfEnergy, UnitOfPower
|
||||||
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
|
||||||
@ -24,6 +23,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||||||
from homeassistant.helpers.typing import StateType
|
from homeassistant.helpers.typing import StateType
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
|
from . import ForecastSolarConfigEntry
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import ForecastSolarDataUpdateCoordinator
|
from .coordinator import ForecastSolarDataUpdateCoordinator
|
||||||
|
|
||||||
@ -133,10 +133,12 @@ SENSORS: tuple[ForecastSolarSensorEntityDescription, ...] = (
|
|||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant,
|
||||||
|
entry: ForecastSolarConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Defer sensor setup to the shared sensor module."""
|
"""Defer sensor setup to the shared sensor module."""
|
||||||
coordinator: ForecastSolarDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
coordinator = entry.runtime_data
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
ForecastSolarSensorEntity(
|
ForecastSolarSensorEntity(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user