From faccf110914f5f036a361c8db27df902fd5a5995 Mon Sep 17 00:00:00 2001 From: Werner Pieterson Date: Sat, 12 Nov 2022 18:34:44 +0200 Subject: [PATCH] Fix coordinator update bug --- custom_components/load_shedding/const.py | 2 +- custom_components/load_shedding/sensor.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/custom_components/load_shedding/const.py b/custom_components/load_shedding/const.py index bca14f4..90a15f4 100644 --- a/custom_components/load_shedding/const.py +++ b/custom_components/load_shedding/const.py @@ -12,7 +12,7 @@ VERSION: Final = "1.1.0" DEFAULT_SCAN_INTERVAL: Final = 60 AREA_UPDATE_INTERVAL: Final = 86400 # 60sec * 60min * 24h / daily -QUOTA_UPDATE_INTERVAL: Final = 300 # 60sec * 5min +QUOTA_UPDATE_INTERVAL: Final = 1800 # 60sec * 30min STAGE_UPDATE_INTERVAL: Final = 3600 # 60sec * 60min / hourly CONF_DEFAULT_SCHEDULE_STAGE: Final = "default_schedule_stage" diff --git a/custom_components/load_shedding/sensor.py b/custom_components/load_shedding/sensor.py index c0cf658..ea089ae 100644 --- a/custom_components/load_shedding/sensor.py +++ b/custom_components/load_shedding/sensor.py @@ -103,6 +103,7 @@ def __init__(self, coordinator: CoordinatorEntity, idx: str) -> None: """Initialize.""" super().__init__(coordinator) self.idx = idx + self.data = self.coordinator.data.get(self.idx) self.entity_description = LoadSheddingSensorDescription( key=f"{DOMAIN} stage", @@ -110,7 +111,6 @@ def __init__(self, coordinator: CoordinatorEntity, idx: str) -> None: name=f"{DOMAIN} stage", entity_registry_enabled_default=True, ) - self.data = self.coordinator.data.get(self.idx) self._attr_unique_id = f"{self.coordinator.config_entry.entry_id}_{self.idx}" self.entity_id = f"{DOMAIN}.{DOMAIN}_stage_{idx}" @@ -191,7 +191,7 @@ def extra_state_attributes(self) -> dict[str, list, Any]: def _handle_coordinator_update(self) -> None: """Handle updated data from the coordinator.""" if data := self.coordinator.data: - self.data = data + self.data = data.get(self.idx) self.async_write_ha_state() @@ -205,8 +205,8 @@ class LoadSheddingAreaSensorEntity( def __init__(self, coordinator: CoordinatorEntity, area: Area) -> None: """Initialize.""" super().__init__(coordinator) - self.data = self.coordinator.data.get(area.id) self.area = area + self.data = self.coordinator.data.get(self.area.id) self.entity_description = LoadSheddingSensorDescription( key=f"{DOMAIN} schedule {area.id}", @@ -297,7 +297,7 @@ def extra_state_attributes(self) -> dict[str, list, Any]: def _handle_coordinator_update(self) -> None: """Handle updated data from the coordinator.""" if data := self.coordinator.data: - self.data = data + self.data = data.get(self.area.id) self.async_write_ha_state()