Improve history coordinator in Teslemetry (#128235)

pull/129952/head
Brett Adams 2024-11-06 22:40:37 +10:00 committed by GitHub
parent c6cb2884f4
commit 96de4b3828
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 15 additions and 9 deletions

View File

@ -135,11 +135,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: TeslemetryConfigEntry) -
elif "energy_site_id" in product and Scope.ENERGY_DEVICE_DATA in scopes:
site_id = product["energy_site_id"]
if not (
product["components"]["battery"]
or product["components"]["solar"]
or "wall_connectors" in product["components"]
):
powerwall = (
product["components"]["battery"] or product["components"]["solar"]
)
wall_connector = "wall_connectors" in product["components"]
if not powerwall and not wall_connector:
LOGGER.debug(
"Skipping Energy Site %s as it has no components",
site_id,
@ -162,7 +162,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: TeslemetryConfigEntry) -
info_coordinator=TeslemetryEnergySiteInfoCoordinator(
hass, api, product
),
history_coordinator=TeslemetryEnergyHistoryCoordinator(hass, api),
history_coordinator=(
TeslemetryEnergyHistoryCoordinator(hass, api)
if powerwall
else None
),
id=site_id,
device=device,
)
@ -185,6 +189,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: TeslemetryConfigEntry) -
*(
energysite.history_coordinator.async_config_entry_first_refresh()
for energysite in energysites
if energysite.history_coordinator
),
)

View File

@ -175,6 +175,8 @@ class TeslemetryEnergyHistoryEntity(TeslemetryEntity):
) -> None:
"""Initialize common aspects of a Teslemetry Energy Site Info entity."""
assert data.history_coordinator
self.api = data.api
self._attr_unique_id = f"{data.id}-{key}"
self._attr_device_info = data.device

View File

@ -49,6 +49,6 @@ class TeslemetryEnergyData:
api: EnergySpecific
live_coordinator: TeslemetryEnergySiteLiveCoordinator
info_coordinator: TeslemetryEnergySiteInfoCoordinator
history_coordinator: TeslemetryEnergyHistoryCoordinator
history_coordinator: TeslemetryEnergyHistoryCoordinator | None
id: int
device: DeviceInfo

View File

@ -482,8 +482,7 @@ async def async_setup_entry(
TeslemetryEnergyHistorySensorEntity(energysite, description)
for energysite in entry.runtime_data.energysites
for description in ENERGY_HISTORY_DESCRIPTIONS
if energysite.info_coordinator.data.get("components_battery")
or energysite.info_coordinator.data.get("components_solar")
if energysite.history_coordinator
),
)
)