mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-10-09 08:58:01 +00:00
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:
@@ -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):
|
||||
|
Reference in New Issue
Block a user