Use runtime_data in huisbaasje (#144953)

This commit is contained in:
epenet 2025-05-19 10:57:22 +02:00 committed by GitHub
parent a3aae68229
commit 177afea5ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 13 additions and 24 deletions

View File

@ -4,19 +4,18 @@ import logging
from energyflip import EnergyFlip, EnergyFlipException
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
from homeassistant.core import HomeAssistant
from .const import DATA_COORDINATOR, DOMAIN, FETCH_TIMEOUT, SOURCE_TYPES
from .coordinator import EnergyFlipUpdateCoordinator
from .const import FETCH_TIMEOUT, SOURCE_TYPES
from .coordinator import EnergyFlipConfigEntry, EnergyFlipUpdateCoordinator
PLATFORMS = [Platform.SENSOR]
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: EnergyFlipConfigEntry) -> bool:
"""Set up EnergyFlip from a config entry."""
# Create the EnergyFlip client
energyflip = EnergyFlip(
@ -39,7 +38,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
await coordinator.async_config_entry_first_refresh()
# Load the client in the data of home assistant
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {DATA_COORDINATOR: coordinator}
entry.runtime_data = coordinator
# Offload the loading of entities to the platform
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
@ -47,13 +46,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: EnergyFlipConfigEntry) -> bool:
"""Unload a config entry."""
# Forward the unloading of the entry to the platform
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
# If successful, unload the EnergyFlip client
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

View File

@ -9,8 +9,6 @@ from energyflip.const import (
SOURCE_TYPE_GAS,
)
DATA_COORDINATOR = "coordinator"
DOMAIN = "huisbaasje"
"""Interval in seconds between polls to EnergyFlip."""

View File

@ -27,16 +27,18 @@ PLATFORMS = [Platform.SENSOR]
_LOGGER = logging.getLogger(__name__)
type EnergyFlipConfigEntry = ConfigEntry[EnergyFlipUpdateCoordinator]
class EnergyFlipUpdateCoordinator(DataUpdateCoordinator[dict[str, dict[str, Any]]]):
"""EnergyFlip data update coordinator."""
config_entry: ConfigEntry
config_entry: EnergyFlipConfigEntry
def __init__(
self,
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: EnergyFlipConfigEntry,
energyflip: EnergyFlip,
) -> None:
"""Initialize the Huisbaasje data coordinator."""

View File

@ -20,7 +20,6 @@ from homeassistant.components.sensor import (
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_ID,
UnitOfEnergy,
@ -33,7 +32,6 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import (
DATA_COORDINATOR,
DOMAIN,
SENSOR_TYPE_RATE,
SENSOR_TYPE_THIS_DAY,
@ -41,7 +39,7 @@ from .const import (
SENSOR_TYPE_THIS_WEEK,
SENSOR_TYPE_THIS_YEAR,
)
from .coordinator import EnergyFlipUpdateCoordinator
from .coordinator import EnergyFlipConfigEntry, EnergyFlipUpdateCoordinator
_LOGGER = logging.getLogger(__name__)
@ -215,13 +213,11 @@ SENSORS_INFO = [
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: EnergyFlipConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up the sensor platform."""
coordinator: EnergyFlipUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id][
DATA_COORDINATOR
]
coordinator = config_entry.runtime_data
user_id = config_entry.data[CONF_ID]
async_add_entities(