mirror of
				https://github.com/python-kasa/python-kasa.git
				synced 2025-10-31 04:31:54 +00:00 
			
		
		
		
	move get_time{zone} out from smartdevice + some minor cleanups
This commit is contained in:
		| @@ -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) | ||||
|   | ||||
| @@ -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 | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -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") | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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")) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Teemu Rytilahti
					Teemu Rytilahti