mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-10-21 14:58:02 +00:00
Pull up emeter handling to tapodevice base class (#601)
* Pull has_emeter property up to tapodevice base class This will also use the existence of energy_monitoring in the component_nego query to decide if the device has the service. * Move emeter related functions to tapodevice * Remove supported_modules override for now This should be done in a separate PR, if we want to expose the available components to cli and downstreams * Dedent extra reqs * Move extra_reqs initialization * Fix tests
This commit is contained in:
@@ -4,10 +4,8 @@ from datetime import datetime, timedelta
|
||||
from typing import Any, Dict, Optional, cast
|
||||
|
||||
from ..deviceconfig import DeviceConfig
|
||||
from ..emeterstatus import EmeterStatus
|
||||
from ..modules import Emeter
|
||||
from ..protocol import TPLinkProtocol
|
||||
from ..smartdevice import DeviceType, requires_update
|
||||
from ..smartdevice import DeviceType
|
||||
from .tapodevice import TapoDevice
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -25,32 +23,6 @@ class TapoPlug(TapoDevice):
|
||||
) -> None:
|
||||
super().__init__(host=host, config=config, protocol=protocol)
|
||||
self._device_type = DeviceType.Plug
|
||||
self.modules: Dict[str, Any] = {}
|
||||
self.emeter_type = "emeter"
|
||||
self.modules["emeter"] = Emeter(self, self.emeter_type)
|
||||
|
||||
@property # type: ignore
|
||||
@requires_update
|
||||
def has_emeter(self) -> bool:
|
||||
"""Return that the plug has an emeter."""
|
||||
return True
|
||||
|
||||
async def update(self, update_children: bool = True):
|
||||
"""Call the device endpoint and update the device data."""
|
||||
await super().update(update_children)
|
||||
|
||||
req = {
|
||||
"get_energy_usage": None,
|
||||
"get_current_power": None,
|
||||
}
|
||||
resp = await self.protocol.query(req)
|
||||
self._energy = resp["get_energy_usage"]
|
||||
self._emeter = resp["get_current_power"]
|
||||
|
||||
self._data["energy"] = self._energy
|
||||
self._data["emeter"] = self._emeter
|
||||
|
||||
_LOGGER.debug("Got an update: %s %s", self._energy, self._emeter)
|
||||
|
||||
@property
|
||||
def state_information(self) -> Dict[str, Any]:
|
||||
@@ -64,35 +36,6 @@ class TapoPlug(TapoDevice):
|
||||
},
|
||||
}
|
||||
|
||||
@property
|
||||
def emeter_realtime(self) -> EmeterStatus:
|
||||
"""Get the emeter status."""
|
||||
return EmeterStatus(
|
||||
{
|
||||
"power_mw": self._energy.get("current_power"),
|
||||
"total": self._convert_energy_data(
|
||||
self._energy.get("today_energy"), 1 / 1000
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
async def get_emeter_realtime(self) -> EmeterStatus:
|
||||
"""Retrieve current energy readings."""
|
||||
self._verify_emeter()
|
||||
resp = await self.protocol.query("get_energy_usage")
|
||||
self._energy = resp["get_energy_usage"]
|
||||
return self.emeter_realtime
|
||||
|
||||
@property
|
||||
def emeter_today(self) -> Optional[float]:
|
||||
"""Get the emeter value for today."""
|
||||
return self._convert_energy_data(self._energy.get("today_energy"), 1 / 1000)
|
||||
|
||||
@property
|
||||
def emeter_this_month(self) -> Optional[float]:
|
||||
"""Get the emeter value for this month."""
|
||||
return self._convert_energy_data(self._energy.get("month_energy"), 1 / 1000)
|
||||
|
||||
@property
|
||||
def on_since(self) -> Optional[datetime]:
|
||||
"""Return the time that the device was turned on or None if turned off."""
|
||||
@@ -100,7 +43,3 @@ class TapoPlug(TapoDevice):
|
||||
return None
|
||||
on_time = cast(float, self._info.get("on_time"))
|
||||
return datetime.now().replace(microsecond=0) - timedelta(seconds=on_time)
|
||||
|
||||
def _convert_energy_data(self, data, scale) -> Optional[float]:
|
||||
"""Return adjusted emeter information."""
|
||||
return data if not data else data * scale
|
||||
|
Reference in New Issue
Block a user