2021-11-07 01:41:12 +00:00
|
|
|
"""Provides the current time and timezone information."""
|
2024-04-16 18:21:20 +00:00
|
|
|
|
2024-10-08 07:16:51 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-11-18 18:46:36 +00:00
|
|
|
from datetime import UTC, datetime, tzinfo
|
2021-11-07 01:41:12 +00:00
|
|
|
|
2024-02-21 15:52:55 +00:00
|
|
|
from ...exceptions import KasaException
|
2024-10-15 07:59:25 +00:00
|
|
|
from ...interfaces import Time as TimeInterface
|
2024-02-19 17:01:31 +00:00
|
|
|
from ..iotmodule import IotModule, merge
|
2024-10-15 07:59:25 +00:00
|
|
|
from ..iottimezone import get_timezone, get_timezone_index
|
2021-11-07 01:41:12 +00:00
|
|
|
|
|
|
|
|
2024-10-15 07:59:25 +00:00
|
|
|
class Time(IotModule, TimeInterface):
|
2021-11-07 01:41:12 +00:00
|
|
|
"""Implements the timezone settings."""
|
|
|
|
|
2024-11-18 18:46:36 +00:00
|
|
|
_timezone: tzinfo = UTC
|
2024-10-08 07:16:51 +00:00
|
|
|
|
2024-11-10 18:55:13 +00:00
|
|
|
def query(self) -> dict:
|
2021-11-07 01:41:12 +00:00
|
|
|
"""Request time and timezone."""
|
|
|
|
q = self.query_for_command("get_time")
|
|
|
|
|
|
|
|
merge(q, self.query_for_command("get_timezone"))
|
|
|
|
return q
|
|
|
|
|
2024-11-10 18:55:13 +00:00
|
|
|
async def _post_update_hook(self) -> None:
|
2024-10-08 07:16:51 +00:00
|
|
|
"""Perform actions after a device update."""
|
|
|
|
if res := self.data.get("get_timezone"):
|
|
|
|
self._timezone = await get_timezone(res.get("index"))
|
|
|
|
|
2021-11-07 01:41:12 +00:00
|
|
|
@property
|
|
|
|
def time(self) -> datetime:
|
|
|
|
"""Return current device time."""
|
|
|
|
res = self.data["get_time"]
|
2024-10-08 07:16:51 +00:00
|
|
|
time = datetime(
|
2021-11-07 01:41:12 +00:00
|
|
|
res["year"],
|
|
|
|
res["month"],
|
|
|
|
res["mday"],
|
|
|
|
res["hour"],
|
|
|
|
res["min"],
|
|
|
|
res["sec"],
|
2024-10-08 11:33:19 +00:00
|
|
|
tzinfo=self.timezone,
|
2021-11-07 01:41:12 +00:00
|
|
|
)
|
2024-10-08 11:33:19 +00:00
|
|
|
return time
|
2021-11-07 01:41:12 +00:00
|
|
|
|
|
|
|
@property
|
2024-10-08 07:16:51 +00:00
|
|
|
def timezone(self) -> tzinfo:
|
2021-11-07 01:41:12 +00:00
|
|
|
"""Return current timezone."""
|
2024-10-08 07:16:51 +00:00
|
|
|
return self._timezone
|
2022-01-29 19:36:08 +00:00
|
|
|
|
2024-11-10 18:55:13 +00:00
|
|
|
async def get_time(self) -> datetime | None:
|
2022-01-29 19:36:08 +00:00
|
|
|
"""Return current device time."""
|
|
|
|
try:
|
|
|
|
res = await self.call("get_time")
|
|
|
|
return datetime(
|
|
|
|
res["year"],
|
|
|
|
res["month"],
|
|
|
|
res["mday"],
|
|
|
|
res["hour"],
|
|
|
|
res["min"],
|
|
|
|
res["sec"],
|
2024-10-15 07:59:25 +00:00
|
|
|
tzinfo=self.timezone,
|
2022-01-29 19:36:08 +00:00
|
|
|
)
|
2024-02-21 15:52:55 +00:00
|
|
|
except KasaException:
|
2022-01-29 19:36:08 +00:00
|
|
|
return None
|
|
|
|
|
2024-10-15 07:59:25 +00:00
|
|
|
async def set_time(self, dt: datetime) -> dict:
|
|
|
|
"""Set the device time."""
|
|
|
|
params = {
|
|
|
|
"year": dt.year,
|
|
|
|
"month": dt.month,
|
|
|
|
"mday": dt.day,
|
|
|
|
"hour": dt.hour,
|
|
|
|
"min": dt.minute,
|
|
|
|
"sec": dt.second,
|
|
|
|
}
|
|
|
|
if dt.tzinfo:
|
|
|
|
index = await get_timezone_index(dt.tzinfo)
|
|
|
|
current_index = self.data.get("get_timezone", {}).get("index", -1)
|
|
|
|
if current_index != -1 and current_index != index:
|
|
|
|
params["index"] = index
|
|
|
|
method = "set_timezone"
|
|
|
|
else:
|
|
|
|
method = "set_time"
|
|
|
|
else:
|
|
|
|
method = "set_time"
|
|
|
|
try:
|
|
|
|
return await self.call(method, params)
|
|
|
|
except Exception as ex:
|
|
|
|
raise KasaException(ex) from ex
|
|
|
|
|
2024-11-10 18:55:13 +00:00
|
|
|
async def get_timezone(self) -> dict:
|
2022-01-29 19:36:08 +00:00
|
|
|
"""Request timezone information from the device."""
|
|
|
|
return await self.call("get_timezone")
|