Use runtime_data in iotawatt (#144977)

This commit is contained in:
epenet 2025-05-15 14:43:35 +02:00 committed by GitHub
parent 334f9deaec
commit e8281bb009
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 14 additions and 17 deletions

View File

@ -1,26 +1,22 @@
"""The iotawatt integration.""" """The iotawatt integration."""
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from .const import DOMAIN from .coordinator import IotawattConfigEntry, IotawattUpdater
from .coordinator import IotawattUpdater
PLATFORMS = [Platform.SENSOR] PLATFORMS = [Platform.SENSOR]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: IotawattConfigEntry) -> bool:
"""Set up iotawatt from a config entry.""" """Set up iotawatt from a config entry."""
coordinator = IotawattUpdater(hass, entry) coordinator = IotawattUpdater(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: IotawattConfigEntry) -> 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

View File

@ -21,14 +21,16 @@ _LOGGER = logging.getLogger(__name__)
# Matches iotwatt data log interval # Matches iotwatt data log interval
REQUEST_REFRESH_DEFAULT_COOLDOWN = 5 REQUEST_REFRESH_DEFAULT_COOLDOWN = 5
type IotawattConfigEntry = ConfigEntry[IotawattUpdater]
class IotawattUpdater(DataUpdateCoordinator): class IotawattUpdater(DataUpdateCoordinator):
"""Class to manage fetching update data from the IoTaWatt Energy Device.""" """Class to manage fetching update data from the IoTaWatt Energy Device."""
api: Iotawatt | None = None api: Iotawatt | None = None
config_entry: ConfigEntry config_entry: IotawattConfigEntry
def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None: def __init__(self, hass: HomeAssistant, entry: IotawattConfigEntry) -> None:
"""Initialize IotaWattUpdater object.""" """Initialize IotaWattUpdater object."""
super().__init__( super().__init__(
hass=hass, hass=hass,

View File

@ -14,7 +14,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,
UnitOfApparentPower, UnitOfApparentPower,
@ -31,8 +30,8 @@ from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
from .const import DOMAIN, VOLT_AMPERE_REACTIVE, VOLT_AMPERE_REACTIVE_HOURS from .const import VOLT_AMPERE_REACTIVE, VOLT_AMPERE_REACTIVE_HOURS
from .coordinator import IotawattUpdater from .coordinator import IotawattConfigEntry, IotawattUpdater
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -113,11 +112,11 @@ ENTITY_DESCRIPTION_KEY_MAP: dict[str, IotaWattSensorEntityDescription] = {
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: IotawattConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback, async_add_entities: AddConfigEntryEntitiesCallback,
) -> None: ) -> None:
"""Add sensors for passed config_entry in HA.""" """Add sensors for passed config_entry in HA."""
coordinator: IotawattUpdater = hass.data[DOMAIN][config_entry.entry_id] coordinator = config_entry.runtime_data
created = set() created = set()
@callback @callback

View File

@ -5,7 +5,7 @@ from unittest.mock import AsyncMock, MagicMock, patch
import pytest import pytest
from homeassistant.components.iotawatt import DOMAIN from homeassistant.components.iotawatt.const import DOMAIN
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry from tests.common import MockConfigEntry