mirror of
https://github.com/home-assistant/core.git
synced 2026-04-25 16:36:37 +00:00
Use runtime_data in srp_energy integration (#167870)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -2,17 +2,16 @@
|
||||
|
||||
from srpenergy.client import SrpEnergyClient
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_ID, CONF_PASSWORD, CONF_USERNAME, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import DOMAIN, LOGGER
|
||||
from .coordinator import SRPEnergyDataUpdateCoordinator
|
||||
from .const import LOGGER
|
||||
from .coordinator import SRPEnergyConfigEntry, SRPEnergyDataUpdateCoordinator
|
||||
|
||||
PLATFORMS = [Platform.SENSOR]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: SRPEnergyConfigEntry) -> bool:
|
||||
"""Set up the SRP Energy component from a config entry."""
|
||||
api_account_id: str = entry.data[CONF_ID]
|
||||
api_username: str = entry.data[CONF_USERNAME]
|
||||
@@ -30,17 +29,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
hass.data[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: SRPEnergyConfigEntry) -> 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)
|
||||
|
||||
@@ -23,14 +23,19 @@ from .const import (
|
||||
TIMEOUT = 10
|
||||
PHOENIX_ZONE_INFO = dt_util.get_time_zone(PHOENIX_TIME_ZONE)
|
||||
|
||||
type SRPEnergyConfigEntry = ConfigEntry[SRPEnergyDataUpdateCoordinator]
|
||||
|
||||
|
||||
class SRPEnergyDataUpdateCoordinator(DataUpdateCoordinator[float]):
|
||||
"""A srp_energy Data Update Coordinator."""
|
||||
|
||||
config_entry: ConfigEntry
|
||||
config_entry: SRPEnergyConfigEntry
|
||||
|
||||
def __init__(
|
||||
self, hass: HomeAssistant, config_entry: ConfigEntry, client: SrpEnergyClient
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
config_entry: SRPEnergyConfigEntry,
|
||||
client: SrpEnergyClient,
|
||||
) -> None:
|
||||
"""Initialize the srp_energy data coordinator."""
|
||||
self._client = client
|
||||
|
||||
@@ -7,7 +7,6 @@ from homeassistant.components.sensor import (
|
||||
SensorEntity,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import UnitOfEnergy
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||
@@ -15,19 +14,17 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.helpers.typing import StateType
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from . import SRPEnergyDataUpdateCoordinator
|
||||
from .const import DEVICE_CONFIG_URL, DEVICE_MANUFACTURER, DEVICE_MODEL, DOMAIN
|
||||
from .coordinator import SRPEnergyConfigEntry, SRPEnergyDataUpdateCoordinator
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
entry: SRPEnergyConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the SRP Energy Usage sensor."""
|
||||
coordinator: SRPEnergyDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
|
||||
async_add_entities([SrpEntity(coordinator, entry)])
|
||||
async_add_entities([SrpEntity(entry.runtime_data, entry)])
|
||||
|
||||
|
||||
class SrpEntity(CoordinatorEntity[SRPEnergyDataUpdateCoordinator], SensorEntity):
|
||||
@@ -43,7 +40,7 @@ class SrpEntity(CoordinatorEntity[SRPEnergyDataUpdateCoordinator], SensorEntity)
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: SRPEnergyDataUpdateCoordinator,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: SRPEnergyConfigEntry,
|
||||
) -> None:
|
||||
"""Initialize the SrpEntity class."""
|
||||
super().__init__(coordinator)
|
||||
|
||||
Reference in New Issue
Block a user