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

@@ -12,7 +12,7 @@ from .usage import Usage
class Emeter(Usage, EnergyInterface):
"""Emeter module."""
def _post_update_hook(self) -> None:
async def _post_update_hook(self) -> None:
self._supported = EnergyInterface.ModuleFeature.PERIODIC_STATS
if (
"voltage_mv" in self.data["get_realtime"]

View File

@@ -239,7 +239,7 @@ class Light(IotModule, LightInterface):
"""Return the current light state."""
return self._light_state
def _post_update_hook(self) -> None:
async def _post_update_hook(self) -> None:
if self._device.is_on is False:
state = LightState(light_on=False)
else:

View File

@@ -41,7 +41,7 @@ class LightPreset(IotModule, LightPresetInterface):
_presets: dict[str, IotLightPreset]
_preset_list: list[str]
def _post_update_hook(self):
async def _post_update_hook(self):
"""Update the internal presets."""
self._presets = {
f"Light preset {index+1}": IotLightPreset(**vals)

View File

@@ -1,14 +1,19 @@
"""Provides the current time and timezone information."""
from datetime import datetime
from __future__ import annotations
from datetime import datetime, timezone, tzinfo
from ...exceptions import KasaException
from ..iotmodule import IotModule, merge
from ..iottimezone import get_timezone
class Time(IotModule):
"""Implements the timezone settings."""
_timezone: tzinfo = timezone.utc
def query(self):
"""Request time and timezone."""
q = self.query_for_command("get_time")
@@ -16,11 +21,16 @@ class Time(IotModule):
merge(q, self.query_for_command("get_timezone"))
return q
async def _post_update_hook(self):
"""Perform actions after a device update."""
if res := self.data.get("get_timezone"):
self._timezone = await get_timezone(res.get("index"))
@property
def time(self) -> datetime:
"""Return current device time."""
res = self.data["get_time"]
return datetime(
time = datetime(
res["year"],
res["month"],
res["mday"],
@@ -28,12 +38,12 @@ class Time(IotModule):
res["min"],
res["sec"],
)
return time.astimezone(self.timezone)
@property
def timezone(self):
def timezone(self) -> tzinfo:
"""Return current timezone."""
res = self.data["get_timezone"]
return res
return self._timezone
async def get_time(self):
"""Return current device time."""