Skip to content

Commit

Permalink
Fix bug where area sensor doesn't update
Browse files Browse the repository at this point in the history
  • Loading branch information
wernerhp committed Nov 23, 2024
1 parent a4dd7a8 commit a9f7cdd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
24 changes: 11 additions & 13 deletions custom_components/load_shedding/__init__.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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),
}
]

Expand All @@ -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")))
Expand All @@ -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),
}
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
{
Expand Down Expand Up @@ -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]]):
Expand All @@ -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:
Expand Down
12 changes: 5 additions & 7 deletions custom_components/load_shedding/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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] = []
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit a9f7cdd

Please sign in to comment.