Store runtime data inside the config entry in solarlog (#120773)

This commit is contained in:
dontinelli 2024-06-29 19:32:22 +02:00 committed by GitHub
parent 559caf4179
commit bcee5f4d9f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 11 deletions

View File

@ -7,29 +7,31 @@ from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from .const import DOMAIN
from .coordinator import SolarlogData from .coordinator import SolarlogData
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
PLATFORMS = [Platform.SENSOR] PLATFORMS = [Platform.SENSOR]
type SolarlogConfigEntry = ConfigEntry[SolarlogData]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: SolarlogConfigEntry) -> bool:
"""Set up a config entry for solarlog.""" """Set up a config entry for solarlog."""
coordinator = SolarlogData(hass, entry) coordinator = SolarlogData(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: SolarlogConfigEntry) -> bool:
"""Unload a config entry.""" """Unload a config entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: async def async_migrate_entry(
hass: HomeAssistant, config_entry: SolarlogConfigEntry
) -> bool:
"""Migrate old entry.""" """Migrate old entry."""
_LOGGER.debug("Migrating from version %s", config_entry.version) _LOGGER.debug("Migrating from version %s", config_entry.version)

View File

@ -1,7 +1,10 @@
"""DataUpdateCoordinator for solarlog integration.""" """DataUpdateCoordinator for solarlog integration."""
from __future__ import annotations
from datetime import timedelta from datetime import timedelta
import logging import logging
from typing import TYPE_CHECKING
from urllib.parse import ParseResult, urlparse from urllib.parse import ParseResult, urlparse
from solarlog_cli.solarlog_connector import SolarLogConnector from solarlog_cli.solarlog_connector import SolarLogConnector
@ -10,7 +13,6 @@ from solarlog_cli.solarlog_exceptions import (
SolarLogUpdateError, SolarLogUpdateError,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryNotReady
@ -18,11 +20,14 @@ from homeassistant.helpers import update_coordinator
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
if TYPE_CHECKING:
from . import SolarlogConfigEntry
class SolarlogData(update_coordinator.DataUpdateCoordinator): class SolarlogData(update_coordinator.DataUpdateCoordinator):
"""Get and update the latest data.""" """Get and update the latest data."""
def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None: def __init__(self, hass: HomeAssistant, entry: SolarlogConfigEntry) -> None:
"""Initialize the data object.""" """Initialize the data object."""
super().__init__( super().__init__(
hass, _LOGGER, name="SolarLog", update_interval=timedelta(seconds=60) hass, _LOGGER, name="SolarLog", update_interval=timedelta(seconds=60)

View File

@ -10,7 +10,6 @@ from homeassistant.components.sensor import (
SensorEntityDescription, SensorEntityDescription,
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
PERCENTAGE, PERCENTAGE,
UnitOfElectricPotential, UnitOfElectricPotential,
@ -22,7 +21,7 @@ from homeassistant.helpers.device_registry import DeviceInfo
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 SolarlogData from . import SolarlogConfigEntry, SolarlogData
from .const import DOMAIN from .const import DOMAIN
@ -201,10 +200,12 @@ SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = (
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant,
entry: SolarlogConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Add solarlog entry.""" """Add solarlog entry."""
coordinator = hass.data[DOMAIN][entry.entry_id] coordinator = entry.runtime_data
async_add_entities( async_add_entities(
SolarlogSensor(coordinator, description) for description in SENSOR_TYPES SolarlogSensor(coordinator, description) for description in SENSOR_TYPES
) )