2024-02-19 17:01:31 +00:00
|
|
|
"""Implementation of energy monitoring module."""
|
2024-04-16 18:21:20 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-02-19 17:01:31 +00:00
|
|
|
from ...emeterstatus import EmeterStatus
|
2024-06-17 10:22:05 +00:00
|
|
|
from ...exceptions import KasaException
|
|
|
|
from ...interfaces.energy import Energy as EnergyInterface
|
2024-07-30 18:23:07 +00:00
|
|
|
from ..smartmodule import SmartModule, raise_if_update_error
|
2024-02-19 17:01:31 +00:00
|
|
|
|
|
|
|
|
2024-06-17 10:22:05 +00:00
|
|
|
class Energy(SmartModule, EnergyInterface):
|
2024-02-19 17:01:31 +00:00
|
|
|
"""Implementation of energy monitoring module."""
|
|
|
|
|
|
|
|
REQUIRED_COMPONENT = "energy_monitoring"
|
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
def query(self) -> dict:
|
2024-02-19 17:01:31 +00:00
|
|
|
"""Query to execute during the update cycle."""
|
2024-02-28 17:57:02 +00:00
|
|
|
req = {
|
2024-02-19 17:01:31 +00:00
|
|
|
"get_energy_usage": None,
|
|
|
|
}
|
2024-02-28 17:57:02 +00:00
|
|
|
if self.supported_version > 1:
|
|
|
|
req["get_current_power"] = None
|
|
|
|
return req
|
2024-02-19 17:01:31 +00:00
|
|
|
|
|
|
|
@property
|
2024-07-30 18:23:07 +00:00
|
|
|
@raise_if_update_error
|
2024-06-17 10:22:05 +00:00
|
|
|
def current_consumption(self) -> float | None:
|
2024-04-22 09:25:30 +00:00
|
|
|
"""Current power in watts."""
|
2024-06-17 10:22:05 +00:00
|
|
|
if (power := self.energy.get("current_power")) is not None:
|
2024-04-22 09:25:30 +00:00
|
|
|
return power / 1_000
|
2024-10-25 18:24:43 +00:00
|
|
|
# Fallback if get_energy_usage does not provide current_power,
|
|
|
|
# which can happen on some newer devices (e.g. P304M).
|
|
|
|
elif (
|
|
|
|
power := self.data.get("get_current_power").get("current_power")
|
|
|
|
) is not None:
|
|
|
|
return power
|
2024-04-22 09:25:30 +00:00
|
|
|
return None
|
2024-02-19 17:01:31 +00:00
|
|
|
|
|
|
|
@property
|
2024-07-30 18:23:07 +00:00
|
|
|
@raise_if_update_error
|
2024-02-19 17:01:31 +00:00
|
|
|
def energy(self):
|
|
|
|
"""Return get_energy_usage results."""
|
2024-02-28 17:57:02 +00:00
|
|
|
if en := self.data.get("get_energy_usage"):
|
|
|
|
return en
|
|
|
|
return self.data
|
2024-02-19 17:01:31 +00:00
|
|
|
|
2024-06-17 10:22:05 +00:00
|
|
|
def _get_status_from_energy(self, energy) -> EmeterStatus:
|
2024-02-19 17:01:31 +00:00
|
|
|
return EmeterStatus(
|
|
|
|
{
|
2024-06-17 10:22:05 +00:00
|
|
|
"power_mw": energy.get("current_power"),
|
|
|
|
"total": energy.get("today_energy") / 1_000,
|
2024-02-19 17:01:31 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
2024-07-30 18:23:07 +00:00
|
|
|
@raise_if_update_error
|
2024-06-17 10:22:05 +00:00
|
|
|
def status(self):
|
|
|
|
"""Get the emeter status."""
|
|
|
|
return self._get_status_from_energy(self.energy)
|
|
|
|
|
|
|
|
async def get_status(self):
|
|
|
|
"""Return real-time statistics."""
|
|
|
|
res = await self.call("get_energy_usage")
|
|
|
|
return self._get_status_from_energy(res["get_energy_usage"])
|
|
|
|
|
|
|
|
@property
|
2024-07-30 18:23:07 +00:00
|
|
|
@raise_if_update_error
|
2024-06-17 10:22:05 +00:00
|
|
|
def consumption_this_month(self) -> float | None:
|
|
|
|
"""Get the emeter value for this month in kWh."""
|
|
|
|
return self.energy.get("month_energy") / 1_000
|
|
|
|
|
|
|
|
@property
|
2024-07-30 18:23:07 +00:00
|
|
|
@raise_if_update_error
|
2024-06-17 10:22:05 +00:00
|
|
|
def consumption_today(self) -> float | None:
|
|
|
|
"""Get the emeter value for today in kWh."""
|
|
|
|
return self.energy.get("today_energy") / 1_000
|
|
|
|
|
|
|
|
@property
|
2024-07-30 18:23:07 +00:00
|
|
|
@raise_if_update_error
|
2024-06-17 10:22:05 +00:00
|
|
|
def consumption_total(self) -> float | None:
|
|
|
|
"""Return total consumption since last reboot in kWh."""
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
2024-07-30 18:23:07 +00:00
|
|
|
@raise_if_update_error
|
2024-06-17 10:22:05 +00:00
|
|
|
def current(self) -> float | None:
|
|
|
|
"""Return the current in A."""
|
|
|
|
return None
|
2024-02-19 17:01:31 +00:00
|
|
|
|
|
|
|
@property
|
2024-07-30 18:23:07 +00:00
|
|
|
@raise_if_update_error
|
2024-06-17 10:22:05 +00:00
|
|
|
def voltage(self) -> float | None:
|
|
|
|
"""Get the current voltage in V."""
|
|
|
|
return None
|
|
|
|
|
|
|
|
async def _deprecated_get_realtime(self) -> EmeterStatus:
|
|
|
|
"""Retrieve current energy readings."""
|
|
|
|
return self.status
|
|
|
|
|
|
|
|
async def erase_stats(self):
|
|
|
|
"""Erase all stats."""
|
|
|
|
raise KasaException("Device does not support periodic statistics")
|
|
|
|
|
|
|
|
async def get_daily_stats(self, *, year=None, month=None, kwh=True) -> dict:
|
|
|
|
"""Return daily stats for the given year & month.
|
|
|
|
|
|
|
|
The return value is a dictionary of {day: energy, ...}.
|
|
|
|
"""
|
|
|
|
raise KasaException("Device does not support periodic statistics")
|
|
|
|
|
|
|
|
async def get_monthly_stats(self, *, year=None, kwh=True) -> dict:
|
|
|
|
"""Return monthly stats for the given year."""
|
|
|
|
raise KasaException("Device does not support periodic statistics")
|
2024-10-25 18:24:43 +00:00
|
|
|
|
|
|
|
async def _check_supported(self):
|
|
|
|
"""Additional check to see if the module is supported by the device."""
|
|
|
|
# Energy module is not supported on P304M parent device
|
|
|
|
return "device_on" in self._device.sys_info
|