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-11-10 18:55:13 +00:00
|
|
|
from typing import NoReturn
|
|
|
|
|
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-11-20 18:18:30 +00:00
|
|
|
async def _post_update_hook(self) -> None:
|
|
|
|
if "voltage_mv" in self.data.get("get_emeter_data", {}):
|
|
|
|
self._supported = (
|
|
|
|
self._supported | EnergyInterface.ModuleFeature.VOLTAGE_CURRENT
|
|
|
|
)
|
|
|
|
|
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
|
2024-11-20 18:18:30 +00:00
|
|
|
req["get_emeter_data"] = None
|
|
|
|
req["get_emeter_vgain_igain"] = None
|
2024-02-28 17:57:02 +00:00
|
|
|
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-11-20 18:18:30 +00:00
|
|
|
if (power := self.energy.get("current_power")) is not None or (
|
|
|
|
power := self.data.get("get_emeter_data", {}).get("power_mw")
|
|
|
|
) 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 (
|
2024-11-10 18:55:13 +00:00
|
|
|
power := self.data.get("get_current_power", {}).get("current_power")
|
2024-10-25 18:24:43 +00:00
|
|
|
) 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-11-10 18:55:13 +00:00
|
|
|
def energy(self) -> dict:
|
2024-02-19 17:01:31 +00:00
|
|
|
"""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-11-10 18:55:13 +00:00
|
|
|
def _get_status_from_energy(self, energy: dict) -> EmeterStatus:
|
2024-02-19 17:01:31 +00:00
|
|
|
return EmeterStatus(
|
|
|
|
{
|
2024-11-10 18:55:13 +00:00
|
|
|
"power_mw": energy.get("current_power", 0),
|
|
|
|
"total": energy.get("today_energy", 0) / 1_000,
|
2024-02-19 17:01:31 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
2024-07-30 18:23:07 +00:00
|
|
|
@raise_if_update_error
|
2024-11-10 18:55:13 +00:00
|
|
|
def status(self) -> EmeterStatus:
|
2024-06-17 10:22:05 +00:00
|
|
|
"""Get the emeter status."""
|
2024-11-20 18:18:30 +00:00
|
|
|
if "get_emeter_data" in self.data:
|
|
|
|
return EmeterStatus(self.data["get_emeter_data"])
|
|
|
|
else:
|
|
|
|
return self._get_status_from_energy(self.energy)
|
2024-06-17 10:22:05 +00:00
|
|
|
|
2024-11-10 18:55:13 +00:00
|
|
|
async def get_status(self) -> EmeterStatus:
|
2024-06-17 10:22:05 +00:00
|
|
|
"""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."""
|
2024-11-10 18:55:13 +00:00
|
|
|
return self.energy.get("month_energy", 0) / 1_000
|
2024-06-17 10:22:05 +00:00
|
|
|
|
|
|
|
@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."""
|
2024-11-10 18:55:13 +00:00
|
|
|
return self.energy.get("today_energy", 0) / 1_000
|
2024-06-17 10:22:05 +00:00
|
|
|
|
|
|
|
@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."""
|
2024-11-20 18:18:30 +00:00
|
|
|
ma = self.data.get("get_emeter_data", {}).get("current_ma")
|
|
|
|
return ma / 1000 if ma else 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."""
|
2024-11-20 18:18:30 +00:00
|
|
|
mv = self.data.get("get_emeter_data", {}).get("voltage_mv")
|
|
|
|
return mv / 1000 if mv else None
|
2024-06-17 10:22:05 +00:00
|
|
|
|
|
|
|
async def _deprecated_get_realtime(self) -> EmeterStatus:
|
|
|
|
"""Retrieve current energy readings."""
|
|
|
|
return self.status
|
|
|
|
|
2024-11-10 18:55:13 +00:00
|
|
|
async def erase_stats(self) -> NoReturn:
|
2024-06-17 10:22:05 +00:00
|
|
|
"""Erase all stats."""
|
|
|
|
raise KasaException("Device does not support periodic statistics")
|
|
|
|
|
2024-11-10 18:55:13 +00:00
|
|
|
async def get_daily_stats(
|
|
|
|
self, *, year: int | None = None, month: int | None = None, kwh: bool = True
|
|
|
|
) -> dict:
|
2024-06-17 10:22:05 +00:00
|
|
|
"""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")
|
|
|
|
|
2024-11-10 18:55:13 +00:00
|
|
|
async def get_monthly_stats(
|
|
|
|
self, *, year: int | None = None, kwh: bool = True
|
|
|
|
) -> dict:
|
2024-06-17 10:22:05 +00:00
|
|
|
"""Return monthly stats for the given year."""
|
|
|
|
raise KasaException("Device does not support periodic statistics")
|
2024-10-25 18:24:43 +00:00
|
|
|
|
2024-11-10 18:55:13 +00:00
|
|
|
async def _check_supported(self) -> bool:
|
2024-10-25 18:24:43 +00:00
|
|
|
"""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
|