From a9f7cddf88279e2a28a008ae2d834981984c825c Mon Sep 17 00:00:00 2001 From: Werner Pieterson Date: Sat, 23 Nov 2024 15:07:05 +0000 Subject: [PATCH] Fix bug where area sensor doesn't update --- custom_components/load_shedding/__init__.py | 24 ++++++++++----------- custom_components/load_shedding/sensor.py | 12 +++++------ 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/custom_components/load_shedding/__init__.py b/custom_components/load_shedding/__init__.py index ccce85d..7acd7fa 100644 --- a/custom_components/load_shedding/__init__.py +++ b/custom_components/load_shedding/__init__.py @@ -1,7 +1,7 @@ """The LoadShedding component.""" from __future__ import annotations -from datetime import datetime, timedelta, timezone +from datetime import UTC, datetime, timedelta, timezone import logging from typing import Any @@ -186,7 +186,7 @@ def __init__(self, hass: HomeAssistant, sepush: SePush) -> None: async def _async_update_data(self) -> dict: """Retrieve latest load shedding data.""" - now = datetime.now(datetime.UTC).replace(microsecond=0) + now = datetime.now(UTC).replace(microsecond=0) diff = 0 if self.last_update is not None: diff = (now - self.last_update).seconds @@ -207,7 +207,7 @@ async def _async_update_data(self) -> dict: async def async_update_stage(self) -> dict: """Retrieve latest stage.""" - now = datetime.now(datetime.UTC).replace(microsecond=0) + now = datetime.now(UTC).replace(microsecond=0) try: esp = await self.hass.async_add_executor_job(self.sepush.status) except SePushError as err: @@ -222,7 +222,7 @@ async def async_update_stage(self) -> dict: planned = [ { ATTR_STAGE: stage, - ATTR_START_TIME: start_time.astimezone(datetime.UTC), + ATTR_START_TIME: start_time.astimezone(UTC), } ] @@ -233,7 +233,7 @@ async def async_update_stage(self) -> dict: next_stage.get("stage_start_timestamp") ) prev_end = prev_end.replace(second=0, microsecond=0) - planned[i][ATTR_END_TIME] = prev_end.astimezone(datetime.UTC) + planned[i][ATTR_END_TIME] = prev_end.astimezone(UTC) # Next stage = Stage(int(next_stage.get("stage", "0"))) @@ -244,7 +244,7 @@ async def async_update_stage(self) -> dict: planned.append( { ATTR_STAGE: stage, - ATTR_START_TIME: start_time.astimezone(datetime.UTC), + ATTR_START_TIME: start_time.astimezone(UTC), } ) @@ -289,7 +289,7 @@ def add_area(self, area: Area = None) -> None: async def _async_update_data(self) -> dict: """Retrieve latest load shedding data.""" - now = datetime.now(datetime.UTC).replace(microsecond=0) + now = datetime.now(UTC).replace(microsecond=0) diff = 0 if self.last_update is not None: diff = (now - self.last_update).seconds @@ -332,10 +332,8 @@ async def async_update_area(self) -> dict: if note == str(Stage.LOAD_REDUCTION): stage = Stage.LOAD_REDUCTION - start = datetime.fromisoformat(event.get("start")).astimezone( - datetime.UTC - ) - end = datetime.fromisoformat(event.get("end")).astimezone(datetime.UTC) + start = datetime.fromisoformat(event.get("start")).astimezone(UTC) + end = datetime.fromisoformat(event.get("end")).astimezone(UTC) events.append( { @@ -478,7 +476,7 @@ def utc_dt(date: datetime, time: datetime) -> datetime: second=0, microsecond=0, tzinfo=sast, - ).astimezone(datetime.UTC) + ).astimezone(UTC) class LoadSheddingQuotaCoordinator(DataUpdateCoordinator[dict[str, Any]]): @@ -494,7 +492,7 @@ def __init__(self, hass: HomeAssistant, sepush: SePush) -> None: async def _async_update_data(self) -> dict: """Retrieve latest load shedding data.""" - now = datetime.now(datetime.UTC).replace(microsecond=0) + now = datetime.now(UTC).replace(microsecond=0) try: quota = await self.async_update_quota() except UpdateFailed as err: diff --git a/custom_components/load_shedding/sensor.py b/custom_components/load_shedding/sensor.py index 9b5fce4..d99ef40 100644 --- a/custom_components/load_shedding/sensor.py +++ b/custom_components/load_shedding/sensor.py @@ -2,7 +2,7 @@ from __future__ import annotations from dataclasses import dataclass -from datetime import datetime, timedelta +from datetime import UTC, datetime, timedelta from typing import Any, cast from load_shedding.providers import Area, Stage @@ -153,7 +153,7 @@ def extra_state_attributes(self) -> dict[str, list, Any]: if not self.data: return self._attr_extra_state_attributes - now = datetime.now(datetime.UTC) + now = datetime.now(UTC) # data = get_sensor_attrs(planned, planned[0].get(ATTR_STAGE, Stage.UNKNOWN)) # data[ATTR_PLANNED] = [] data = dict(self._attr_extra_state_attributes) @@ -200,8 +200,6 @@ class LoadSheddingAreaSensorEntity( ): """Define a LoadShedding Area sensor entity.""" - coordinator: CoordinatorEntity - def __init__(self, coordinator: CoordinatorEntity, area: Area) -> None: """Initialize.""" super().__init__(coordinator) @@ -241,7 +239,7 @@ def native_value(self) -> StateType: if not events: return STATE_OFF - now = datetime.now(datetime.UTC) + now = datetime.now(UTC) for event in events: if ATTR_END_TIME in event and event.get(ATTR_END_TIME) < now: @@ -270,7 +268,7 @@ def extra_state_attributes(self) -> dict[str, list, Any]: if not self.data: return self._attr_extra_state_attributes - now = datetime.now(datetime.UTC) + now = datetime.now(UTC) data = dict(self._attr_extra_state_attributes) if events := self.data.get(ATTR_FORECAST, []): data[ATTR_FORECAST] = [] @@ -388,7 +386,7 @@ def get_sensor_attrs(forecast: list, stage: Stage = Stage.NO_LOAD_SHEDDING) -> d ATTR_STAGE: stage.value, } - now = datetime.now(datetime.UTC) + now = datetime.now(UTC) data = dict(DEFAULT_DATA) data[ATTR_STAGE] = stage.value