From 0ab23ccb51a039a6bbbb91b7bf5821a558a3bdd3 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 23 Jun 2025 13:14:38 +0200 Subject: [PATCH] Migrate landisgyr_heat_meter to use runtime_data (#147329) --- .../landisgyr_heat_meter/__init__.py | 18 +++++++--------- .../landisgyr_heat_meter/coordinator.py | 9 ++++++-- .../components/landisgyr_heat_meter/sensor.py | 21 +++++++------------ 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/homeassistant/components/landisgyr_heat_meter/__init__.py b/homeassistant/components/landisgyr_heat_meter/__init__.py index 7e7ebe61eb7..669de160811 100644 --- a/homeassistant/components/landisgyr_heat_meter/__init__.py +++ b/homeassistant/components/landisgyr_heat_meter/__init__.py @@ -7,20 +7,19 @@ from typing import Any import ultraheat_api -from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_DEVICE, Platform from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_registry import RegistryEntry, async_migrate_entries from .const import DOMAIN -from .coordinator import UltraheatCoordinator +from .coordinator import UltraheatConfigEntry, UltraheatCoordinator _LOGGER = logging.getLogger(__name__) 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.""" _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) 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) 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.""" - if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): - hass.data[DOMAIN].pop(entry.entry_id) - - return unload_ok + 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: UltraheatConfigEntry +) -> bool: """Migrate old entry.""" _LOGGER.debug("Migrating from version %s", config_entry.version) diff --git a/homeassistant/components/landisgyr_heat_meter/coordinator.py b/homeassistant/components/landisgyr_heat_meter/coordinator.py index 4214fa1db3e..bda19fd6fc3 100644 --- a/homeassistant/components/landisgyr_heat_meter/coordinator.py +++ b/homeassistant/components/landisgyr_heat_meter/coordinator.py @@ -15,14 +15,19 @@ from .const import POLLING_INTERVAL, ULTRAHEAT_TIMEOUT _LOGGER = logging.getLogger(__name__) +type UltraheatConfigEntry = ConfigEntry[UltraheatCoordinator] + class UltraheatCoordinator(DataUpdateCoordinator[HeatMeterResponse]): """Coordinator for getting data from the ultraheat api.""" - config_entry: ConfigEntry + config_entry: UltraheatConfigEntry def __init__( - self, hass: HomeAssistant, config_entry: ConfigEntry, api: HeatMeterService + self, + hass: HomeAssistant, + config_entry: UltraheatConfigEntry, + api: HeatMeterService, ) -> None: """Initialize my coordinator.""" super().__init__( diff --git a/homeassistant/components/landisgyr_heat_meter/sensor.py b/homeassistant/components/landisgyr_heat_meter/sensor.py index 9bb4af572fd..6a7d7c63103 100644 --- a/homeassistant/components/landisgyr_heat_meter/sensor.py +++ b/homeassistant/components/landisgyr_heat_meter/sensor.py @@ -15,7 +15,6 @@ from homeassistant.components.sensor import ( SensorEntityDescription, SensorStateClass, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( EntityCategory, UnitOfEnergy, @@ -29,13 +28,11 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.typing import StateType -from homeassistant.helpers.update_coordinator import ( - CoordinatorEntity, - DataUpdateCoordinator, -) +from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.util import dt as dt_util -from . import DOMAIN +from .const import DOMAIN +from .coordinator import UltraheatConfigEntry, UltraheatCoordinator _LOGGER = logging.getLogger(__name__) @@ -270,14 +267,12 @@ HEAT_METER_SENSOR_TYPES = ( async def async_setup_entry( hass: HomeAssistant, - entry: ConfigEntry, + entry: UltraheatConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up the sensor platform.""" unique_id = entry.entry_id - coordinator: DataUpdateCoordinator[HeatMeterResponse] = hass.data[DOMAIN][ - entry.entry_id - ] + coordinator = entry.runtime_data model = entry.data["model"] @@ -295,7 +290,7 @@ async def async_setup_entry( class HeatMeterSensor( - CoordinatorEntity[DataUpdateCoordinator[HeatMeterResponse]], + CoordinatorEntity[UltraheatCoordinator], SensorEntity, ): """Representation of a Sensor.""" @@ -304,7 +299,7 @@ class HeatMeterSensor( def __init__( self, - coordinator: DataUpdateCoordinator[HeatMeterResponse], + coordinator: UltraheatCoordinator, description: HeatMeterSensorEntityDescription, device: DeviceInfo, ) -> None: @@ -312,7 +307,7 @@ class HeatMeterSensor( super().__init__(coordinator) self.key = description.key 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.entity_description = description