mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-08-06 10:44:04 +00:00
Create common Time module and add time set cli command (#1157)
This commit is contained in:
@@ -5,11 +5,12 @@ from __future__ import annotations
|
||||
from datetime import datetime, timezone, tzinfo
|
||||
|
||||
from ...exceptions import KasaException
|
||||
from ...interfaces import Time as TimeInterface
|
||||
from ..iotmodule import IotModule, merge
|
||||
from ..iottimezone import get_timezone
|
||||
from ..iottimezone import get_timezone, get_timezone_index
|
||||
|
||||
|
||||
class Time(IotModule):
|
||||
class Time(IotModule, TimeInterface):
|
||||
"""Implements the timezone settings."""
|
||||
|
||||
_timezone: tzinfo = timezone.utc
|
||||
@@ -57,10 +58,36 @@ class Time(IotModule):
|
||||
res["hour"],
|
||||
res["min"],
|
||||
res["sec"],
|
||||
tzinfo=self.timezone,
|
||||
)
|
||||
except KasaException:
|
||||
return None
|
||||
|
||||
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
|
||||
|
||||
async def get_timezone(self):
|
||||
"""Request timezone information from the device."""
|
||||
return await self.call("get_timezone")
|
||||
|
Reference in New Issue
Block a user