Migrate landisgyr_heat_meter to use runtime_data (#147329)

This commit is contained in:
epenet 2025-06-23 13:14:38 +02:00 committed by GitHub
parent 436fcb7e85
commit 0ab23ccb51
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 25 deletions

View File

@ -7,20 +7,19 @@ from typing import Any
import ultraheat_api import ultraheat_api
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_DEVICE, Platform from homeassistant.const import CONF_DEVICE, Platform
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_registry import RegistryEntry, async_migrate_entries from homeassistant.helpers.entity_registry import RegistryEntry, async_migrate_entries
from .const import DOMAIN from .const import DOMAIN
from .coordinator import UltraheatCoordinator from .coordinator import UltraheatConfigEntry, UltraheatCoordinator
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
PLATFORMS: list[Platform] = [Platform.SENSOR] PLATFORMS: list[Platform] = [Platform.SENSOR]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: UltraheatConfigEntry) -> bool:
"""Set up heat meter from a config entry.""" """Set up heat meter from a config entry."""
_LOGGER.debug("Initializing %s integration on %s", DOMAIN, entry.data[CONF_DEVICE]) _LOGGER.debug("Initializing %s integration on %s", DOMAIN, entry.data[CONF_DEVICE])
@ -30,22 +29,21 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
coordinator = UltraheatCoordinator(hass, entry, api) coordinator = UltraheatCoordinator(hass, entry, api)
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: UltraheatConfigEntry) -> bool:
"""Unload a config entry.""" """Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: async def async_migrate_entry(
hass: HomeAssistant, config_entry: UltraheatConfigEntry
) -> 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

@ -15,14 +15,19 @@ from .const import POLLING_INTERVAL, ULTRAHEAT_TIMEOUT
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
type UltraheatConfigEntry = ConfigEntry[UltraheatCoordinator]
class UltraheatCoordinator(DataUpdateCoordinator[HeatMeterResponse]): class UltraheatCoordinator(DataUpdateCoordinator[HeatMeterResponse]):
"""Coordinator for getting data from the ultraheat api.""" """Coordinator for getting data from the ultraheat api."""
config_entry: ConfigEntry config_entry: UltraheatConfigEntry
def __init__( def __init__(
self, hass: HomeAssistant, config_entry: ConfigEntry, api: HeatMeterService self,
hass: HomeAssistant,
config_entry: UltraheatConfigEntry,
api: HeatMeterService,
) -> None: ) -> None:
"""Initialize my coordinator.""" """Initialize my coordinator."""
super().__init__( super().__init__(

View File

@ -15,7 +15,6 @@ from homeassistant.components.sensor import (
SensorEntityDescription, SensorEntityDescription,
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
EntityCategory, EntityCategory,
UnitOfEnergy, UnitOfEnergy,
@ -29,13 +28,11 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.typing import StateType from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import ( from homeassistant.helpers.update_coordinator import CoordinatorEntity
CoordinatorEntity,
DataUpdateCoordinator,
)
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
from . import DOMAIN from .const import DOMAIN
from .coordinator import UltraheatConfigEntry, UltraheatCoordinator
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -270,14 +267,12 @@ HEAT_METER_SENSOR_TYPES = (
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: UltraheatConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback, async_add_entities: AddConfigEntryEntitiesCallback,
) -> None: ) -> None:
"""Set up the sensor platform.""" """Set up the sensor platform."""
unique_id = entry.entry_id unique_id = entry.entry_id
coordinator: DataUpdateCoordinator[HeatMeterResponse] = hass.data[DOMAIN][ coordinator = entry.runtime_data
entry.entry_id
]
model = entry.data["model"] model = entry.data["model"]
@ -295,7 +290,7 @@ async def async_setup_entry(
class HeatMeterSensor( class HeatMeterSensor(
CoordinatorEntity[DataUpdateCoordinator[HeatMeterResponse]], CoordinatorEntity[UltraheatCoordinator],
SensorEntity, SensorEntity,
): ):
"""Representation of a Sensor.""" """Representation of a Sensor."""
@ -304,7 +299,7 @@ class HeatMeterSensor(
def __init__( def __init__(
self, self,
coordinator: DataUpdateCoordinator[HeatMeterResponse], coordinator: UltraheatCoordinator,
description: HeatMeterSensorEntityDescription, description: HeatMeterSensorEntityDescription,
device: DeviceInfo, device: DeviceInfo,
) -> None: ) -> None:
@ -312,7 +307,7 @@ class HeatMeterSensor(
super().__init__(coordinator) super().__init__(coordinator)
self.key = description.key self.key = description.key
self._attr_unique_id = ( self._attr_unique_id = (
f"{coordinator.config_entry.data['device_number']}_{description.key}" # type: ignore[union-attr] f"{coordinator.config_entry.data['device_number']}_{description.key}"
) )
self._attr_name = f"Heat Meter {description.name}" self._attr_name = f"Heat Meter {description.name}"
self.entity_description = description self.entity_description = description