FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

move get_time{zone} out from smartdevice + some minor cleanups · python-kasa/python-kasa@ac01e4f · GitHub

Repository navigation

Commit ac01e4f

Browse files
committed
move get_time{zone} out from smartdevice + some minor cleanups
1 parent 25e41e7 commit ac01e4f

5 files changed

Lines changed: 35 additions & 46 deletions

File tree

‎kasa/modules/emeter.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ async def erase_stats(self):
3939
"""
4040
return await self.call("erase_emeter_stat")
4141

42+
async def get_realtime(self):
43+
"""Return real-time statistics."""
44+
return await self.call("get_realtime")
45+
4246
async def get_daystat(self, *, year, month, kwh=True):
4347
"""Return daily stats for the given year & month."""
4448
raw_data = await super().get_daystat(year=year, month=month)

‎kasa/modules/motion.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
from enum import Enum
33
from typing import Optional
44

5-
from kasa.smartdevice import SmartDeviceException
6-
5+
from ..exceptions import SmartDeviceException
76
from .module import Module
87

98

‎kasa/modules/time.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Provides the current time and timezone information."""
22
from datetime import datetime
33

4+
from ..exceptions import SmartDeviceException
45
from .module import Module, merge
56

67

@@ -32,3 +33,22 @@ def timezone(self):
3233
"""Return current timezone."""
3334
res = self.data["get_timezone"]
3435
return res
36+
37+
async def get_time(self):
38+
"""Return current device time."""
39+
try:
40+
res = await self.call("get_time")
41+
return datetime(
42+
res["year"],
43+
res["month"],
44+
res["mday"],
45+
res["hour"],
46+
res["min"],
47+
res["sec"],
48+
)
49+
except SmartDeviceException:
50+
return None
51+
52+
async def get_timezone(self):
53+
"""Request timezone information from the device."""
54+
return await self.call("get_timezone")

‎kasa/smartdevice.py‎

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ class SmartDevice:
186186
187187
"""
188188

189-
TIME_SERVICE = "time"
190189
emeter_type = "emeter"
191190

192191
def __init__(self, host: str) -> None:
@@ -314,11 +313,6 @@ async def update(self, update_children: bool = True):
314313
_LOGGER.debug("Performing the initial update to obtain sysinfo")
315314
self._last_update = await self.protocol.query(req)
316315
self._sys_info = self._last_update["system"]["get_sysinfo"]
317-
# If the device has no emeter, we are done for the initial update
318-
# Otherwise we will follow the regular code path to also query
319-
# the emeter data also during the initial update
320-
if not self.has_emeter:
321-
return
322316

323317
if self.has_emeter:
324318
_LOGGER.debug(
@@ -380,22 +374,17 @@ def timezone(self) -> Dict:
380374

381375
async def get_time(self) -> Optional[datetime]:
382376
"""Return current time from the device, if available."""
383-
try:
384-
res = await self._query_helper(self.TIME_SERVICE, "get_time")
385-
return datetime(
386-
res["year"],
387-
res["month"],
388-
res["mday"],
389-
res["hour"],
390-
res["min"],
391-
res["sec"],
392-
)
393-
except SmartDeviceException:
394-
return None
377+
_LOGGER.warning(
378+
"Use `time` property instead, this call will be removed in the future."
379+
)
380+
return await self.modules["time"].get_time()
395381

396382
async def get_timezone(self) -> Dict:
397383
"""Return timezone information."""
398-
return await self._query_helper(self.TIME_SERVICE, "get_timezone")
384+
_LOGGER.warning(
385+
"Use `timezone` property instead, this call will be removed in the future."
386+
)
387+
return await self.modules["time"].get_timezone()
399388

400389
@property # type: ignore
401390
@requires_update
@@ -433,7 +422,7 @@ def location(self) -> Dict:
433422
loc["latitude"] = sys_info["latitude_i"] / 10000
434423
loc["longitude"] = sys_info["longitude_i"] / 10000
435424
else:
436-
_LOGGER.warning("Unsupported device location.")
425+
_LOGGER.debug("Unsupported device location.")
437426

438427
return loc
439428

@@ -481,29 +470,7 @@ def emeter_realtime(self) -> EmeterStatus:
481470
async def get_emeter_realtime(self) -> EmeterStatus:
482471
"""Retrieve current energy readings."""
483472
self._verify_emeter()
484-
return EmeterStatus(await self._query_helper(self.emeter_type, "get_realtime"))
485-
486-
def _create_emeter_request(self, year: int = None, month: int = None):
487-
"""Create a Internal method for building a request for all emeter statistics at once."""
488-
# TODO: this is currently only here for smartstrip plug support, move it there?
489-
if year is None:
490-
year = datetime.now().year
491-
if month is None:
492-
month = datetime.now().month
493-
494-
req: Dict[str, Any] = {}
495-
merge(req, self._create_request(self.emeter_type, "get_realtime"))
496-
merge(
497-
req, self._create_request(self.emeter_type, "get_monthstat", {"year": year})
498-
)
499-
merge(
500-
req,
501-
self._create_request(
502-
self.emeter_type, "get_daystat", {"month": month, "year": year}
503-
),
504-
)
505-
506-
return req
473+
return EmeterStatus(await self.modules["emeter"].get_realtime())
507474

508475
@property # type: ignore
509476
@requires_update

‎kasa/smartplug.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ class SmartPlug(SmartDevice):
3939

4040
def __init__(self, host: str) -> None:
4141
super().__init__(host)
42-
self.emeter_type = "emeter"
4342
self._device_type = DeviceType.Plug
4443
self.add_module("schedule", Schedule(self, "schedule"))
4544
self.add_module("usage", Usage(self, "schedule"))

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL