Make iot time timezone aware (#1147)

Also makes on_since for iot devices use device time.
Changes the return value for device.timezone to be tzinfo instead of a dict.
This commit is contained in:
Steven B.
2024-10-08 08:16:51 +01:00
committed by GitHub
parent 8bb2cca7cf
commit 9641edcbc0
22 changed files with 289 additions and 45 deletions

View File

@@ -2,10 +2,12 @@
from __future__ import annotations
from datetime import datetime, timedelta, timezone
from datetime import datetime, timedelta, timezone, tzinfo
from time import mktime
from typing import cast
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
from ...feature import Feature
from ..smartmodule import SmartModule
@@ -31,18 +33,27 @@ class Time(SmartModule):
)
@property
def time(self) -> datetime:
"""Return device's current datetime."""
def timezone(self) -> tzinfo:
"""Return current timezone."""
td = timedelta(minutes=cast(float, self.data.get("time_diff")))
if self.data.get("region"):
tz = timezone(td, str(self.data.get("region")))
if region := self.data.get("region"):
try:
# Zoneinfo will return a DST aware object
tz: tzinfo = ZoneInfo(region)
except ZoneInfoNotFoundError:
tz = timezone(td, region)
else:
# in case the device returns a blank region this will result in the
# tzname being a UTC offset
tz = timezone(td)
return tz
@property
def time(self) -> datetime:
"""Return device's current datetime."""
return datetime.fromtimestamp(
cast(float, self.data.get("timestamp")),
tz=tz,
tz=self.timezone,
)
async def set_time(self, dt: datetime):