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

View File

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

View File

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

View File

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