From f0d66e4195079c24378c0ce36b85617188514de3 Mon Sep 17 00:00:00 2001 From: Teemu Rytilahti Date: Sat, 29 Jan 2022 20:36:08 +0100 Subject: [PATCH] move get_time{zone} out from smartdevice + some minor cleanups --- kasa/modules/emeter.py | 4 ++++ kasa/modules/motion.py | 3 +-- kasa/modules/time.py | 20 ++++++++++++++++ kasa/smartdevice.py | 53 ++++++++---------------------------------- kasa/smartplug.py | 1 - 5 files changed, 35 insertions(+), 46 deletions(-) diff --git a/kasa/modules/emeter.py b/kasa/modules/emeter.py index bb161ce6..cd92c3cc 100644 --- a/kasa/modules/emeter.py +++ b/kasa/modules/emeter.py @@ -39,6 +39,10 @@ class Emeter(Usage): """ return await self.call("erase_emeter_stat") + async def get_realtime(self): + """Return real-time statistics.""" + return await self.call("get_realtime") + async def get_daystat(self, *, year, month, kwh=True): """Return daily stats for the given year & month.""" raw_data = await super().get_daystat(year=year, month=month) diff --git a/kasa/modules/motion.py b/kasa/modules/motion.py index d839ca98..45e272be 100644 --- a/kasa/modules/motion.py +++ b/kasa/modules/motion.py @@ -2,8 +2,7 @@ from enum import Enum from typing import Optional -from kasa.smartdevice import SmartDeviceException - +from ..exceptions import SmartDeviceException from .module import Module diff --git a/kasa/modules/time.py b/kasa/modules/time.py index 0bd3f171..d72e2d60 100644 --- a/kasa/modules/time.py +++ b/kasa/modules/time.py @@ -1,6 +1,7 @@ """Provides the current time and timezone information.""" from datetime import datetime +from ..exceptions import SmartDeviceException from .module import Module, merge @@ -32,3 +33,22 @@ class Time(Module): """Return current timezone.""" res = self.data["get_timezone"] return res + + async def get_time(self): + """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"], + ) + except SmartDeviceException: + return None + + async def get_timezone(self): + """Request timezone information from the device.""" + return await self.call("get_timezone") diff --git a/kasa/smartdevice.py b/kasa/smartdevice.py index 812656c8..b589d86a 100755 --- a/kasa/smartdevice.py +++ b/kasa/smartdevice.py @@ -186,7 +186,6 @@ class SmartDevice: """ - TIME_SERVICE = "time" emeter_type = "emeter" def __init__(self, host: str) -> None: @@ -314,11 +313,6 @@ class SmartDevice: _LOGGER.debug("Performing the initial update to obtain sysinfo") self._last_update = await self.protocol.query(req) self._sys_info = self._last_update["system"]["get_sysinfo"] - # If the device has no emeter, we are done for the initial update - # Otherwise we will follow the regular code path to also query - # the emeter data also during the initial update - if not self.has_emeter: - return if self.has_emeter: _LOGGER.debug( @@ -380,22 +374,17 @@ class SmartDevice: async def get_time(self) -> Optional[datetime]: """Return current time from the device, if available.""" - try: - res = await self._query_helper(self.TIME_SERVICE, "get_time") - return datetime( - res["year"], - res["month"], - res["mday"], - res["hour"], - res["min"], - res["sec"], - ) - except SmartDeviceException: - return None + _LOGGER.warning( + "Use `time` property instead, this call will be removed in the future." + ) + return await self.modules["time"].get_time() async def get_timezone(self) -> Dict: """Return timezone information.""" - return await self._query_helper(self.TIME_SERVICE, "get_timezone") + _LOGGER.warning( + "Use `timezone` property instead, this call will be removed in the future." + ) + return await self.modules["time"].get_timezone() @property # type: ignore @requires_update @@ -433,7 +422,7 @@ class SmartDevice: loc["latitude"] = sys_info["latitude_i"] / 10000 loc["longitude"] = sys_info["longitude_i"] / 10000 else: - _LOGGER.warning("Unsupported device location.") + _LOGGER.debug("Unsupported device location.") return loc @@ -481,29 +470,7 @@ class SmartDevice: async def get_emeter_realtime(self) -> EmeterStatus: """Retrieve current energy readings.""" self._verify_emeter() - return EmeterStatus(await self._query_helper(self.emeter_type, "get_realtime")) - - def _create_emeter_request(self, year: int = None, month: int = None): - """Create a Internal method for building a request for all emeter statistics at once.""" - # TODO: this is currently only here for smartstrip plug support, move it there? - if year is None: - year = datetime.now().year - if month is None: - month = datetime.now().month - - req: Dict[str, Any] = {} - merge(req, self._create_request(self.emeter_type, "get_realtime")) - merge( - req, self._create_request(self.emeter_type, "get_monthstat", {"year": year}) - ) - merge( - req, - self._create_request( - self.emeter_type, "get_daystat", {"month": month, "year": year} - ), - ) - - return req + return EmeterStatus(await self.modules["emeter"].get_realtime()) @property # type: ignore @requires_update diff --git a/kasa/smartplug.py b/kasa/smartplug.py index 58144b58..b636c3e1 100644 --- a/kasa/smartplug.py +++ b/kasa/smartplug.py @@ -39,7 +39,6 @@ class SmartPlug(SmartDevice): def __init__(self, host: str) -> None: super().__init__(host) - self.emeter_type = "emeter" self._device_type = DeviceType.Plug self.add_module("schedule", Schedule(self, "schedule")) self.add_module("usage", Usage(self, "schedule"))