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
3 changed files with 23 additions and 25 deletions

View File

@ -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)

View File

@ -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__(

View File

@ -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