2021-11-07 01:41:12 +00:00
|
|
|
"""Implementation of the emeter module."""
|
2024-04-16 18:21:20 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-11-19 15:41:49 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
from ...emeterstatus import EmeterStatus
|
2024-06-17 10:22:05 +00:00
|
|
|
from ...interfaces.energy import Energy as EnergyInterface
|
2021-11-07 01:41:12 +00:00
|
|
|
from .usage import Usage
|
|
|
|
|
|
|
|
|
2024-06-17 10:22:05 +00:00
|
|
|
class Emeter(Usage, EnergyInterface):
|
2021-11-07 01:41:12 +00:00
|
|
|
"""Emeter module."""
|
|
|
|
|
2024-10-08 07:16:51 +00:00
|
|
|
async def _post_update_hook(self) -> None:
|
2024-06-17 10:22:05 +00:00
|
|
|
self._supported = EnergyInterface.ModuleFeature.PERIODIC_STATS
|
|
|
|
if (
|
|
|
|
"voltage_mv" in self.data["get_realtime"]
|
|
|
|
or "voltage" in self.data["get_realtime"]
|
|
|
|
):
|
|
|
|
self._supported = (
|
|
|
|
self._supported | EnergyInterface.ModuleFeature.VOLTAGE_CURRENT
|
2024-04-25 12:59:17 +00:00
|
|
|
)
|
2024-06-17 10:22:05 +00:00
|
|
|
if (
|
|
|
|
"total_wh" in self.data["get_realtime"]
|
|
|
|
or "total" in self.data["get_realtime"]
|
|
|
|
):
|
|
|
|
self._supported = (
|
|
|
|
self._supported | EnergyInterface.ModuleFeature.CONSUMPTION_TOTAL
|
2024-04-25 12:59:17 +00:00
|
|
|
)
|
|
|
|
|
2021-11-07 01:41:12 +00:00
|
|
|
@property # type: ignore
|
2024-06-17 10:22:05 +00:00
|
|
|
def status(self) -> EmeterStatus:
|
2021-11-07 01:41:12 +00:00
|
|
|
"""Return current energy readings."""
|
|
|
|
return EmeterStatus(self.data["get_realtime"])
|
|
|
|
|
2021-11-19 15:41:49 +00:00
|
|
|
@property
|
2024-06-17 10:22:05 +00:00
|
|
|
def consumption_today(self) -> float | None:
|
2021-11-19 15:41:49 +00:00
|
|
|
"""Return today's energy consumption in kWh."""
|
|
|
|
raw_data = self.daily_data
|
|
|
|
today = datetime.now().day
|
2024-01-05 01:01:00 +00:00
|
|
|
data = self._convert_stat_data(raw_data, entry_key="day", key=today)
|
2024-06-17 10:22:05 +00:00
|
|
|
return data.get(today, 0.0)
|
2021-11-19 15:41:49 +00:00
|
|
|
|
|
|
|
@property
|
2024-06-17 10:22:05 +00:00
|
|
|
def consumption_this_month(self) -> float | None:
|
2021-11-19 15:41:49 +00:00
|
|
|
"""Return this month's energy consumption in kWh."""
|
|
|
|
raw_data = self.monthly_data
|
|
|
|
current_month = datetime.now().month
|
2024-01-05 01:01:00 +00:00
|
|
|
data = self._convert_stat_data(raw_data, entry_key="month", key=current_month)
|
2024-06-17 10:22:05 +00:00
|
|
|
return data.get(current_month, 0.0)
|
2021-11-19 15:41:49 +00:00
|
|
|
|
2024-04-25 12:59:17 +00:00
|
|
|
@property
|
|
|
|
def current_consumption(self) -> float | None:
|
|
|
|
"""Get the current power consumption in Watt."""
|
2024-06-17 10:22:05 +00:00
|
|
|
return self.status.power
|
2024-04-25 12:59:17 +00:00
|
|
|
|
|
|
|
@property
|
2024-06-17 10:22:05 +00:00
|
|
|
def consumption_total(self) -> float | None:
|
2024-04-25 12:59:17 +00:00
|
|
|
"""Return total consumption since last reboot in kWh."""
|
2024-06-17 10:22:05 +00:00
|
|
|
return self.status.total
|
2024-04-25 12:59:17 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def current(self) -> float | None:
|
|
|
|
"""Return the current in A."""
|
2024-06-17 10:22:05 +00:00
|
|
|
return self.status.current
|
2024-04-25 12:59:17 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def voltage(self) -> float | None:
|
|
|
|
"""Get the current voltage in V."""
|
2024-06-17 10:22:05 +00:00
|
|
|
return self.status.voltage
|
2024-04-25 12:59:17 +00:00
|
|
|
|
2024-11-10 18:55:13 +00:00
|
|
|
async def erase_stats(self) -> dict:
|
2021-11-19 15:41:49 +00:00
|
|
|
"""Erase all stats.
|
|
|
|
|
|
|
|
Uses different query than usage meter.
|
|
|
|
"""
|
2021-11-07 01:41:12 +00:00
|
|
|
return await self.call("erase_emeter_stat")
|
2021-11-19 15:41:49 +00:00
|
|
|
|
2024-06-17 10:22:05 +00:00
|
|
|
async def get_status(self) -> EmeterStatus:
|
2022-01-29 19:36:08 +00:00
|
|
|
"""Return real-time statistics."""
|
2024-06-17 10:22:05 +00:00
|
|
|
return EmeterStatus(await self.call("get_realtime"))
|
2022-01-29 19:36:08 +00:00
|
|
|
|
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:
|
2023-10-29 22:15:42 +00:00
|
|
|
"""Return daily stats for the given year & month.
|
|
|
|
|
|
|
|
The return value is a dictionary of {day: energy, ...}.
|
|
|
|
"""
|
2023-02-18 19:53:02 +00:00
|
|
|
data = await self.get_raw_daystat(year=year, month=month)
|
|
|
|
data = self._convert_stat_data(data["day_list"], entry_key="day", kwh=kwh)
|
|
|
|
return data
|
|
|
|
|
2024-11-10 18:55:13 +00:00
|
|
|
async def get_monthly_stats(
|
|
|
|
self, *, year: int | None = None, kwh: bool = True
|
|
|
|
) -> dict:
|
2023-10-29 22:15:42 +00:00
|
|
|
"""Return monthly stats for the given year.
|
|
|
|
|
|
|
|
The return value is a dictionary of {month: energy, ...}.
|
|
|
|
"""
|
2023-02-18 19:53:02 +00:00
|
|
|
data = await self.get_raw_monthstat(year=year)
|
|
|
|
data = self._convert_stat_data(data["month_list"], entry_key="month", kwh=kwh)
|
|
|
|
return data
|
2021-11-19 15:41:49 +00:00
|
|
|
|
2024-01-05 01:01:00 +00:00
|
|
|
def _convert_stat_data(
|
|
|
|
self,
|
2024-04-17 13:39:24 +00:00
|
|
|
data: list[dict[str, int | float]],
|
2024-01-05 01:01:00 +00:00
|
|
|
entry_key: str,
|
2024-01-24 08:10:55 +00:00
|
|
|
kwh: bool = True,
|
2024-04-17 13:39:24 +00:00
|
|
|
key: int | None = None,
|
|
|
|
) -> dict[int | float, int | float]:
|
2023-02-18 19:53:02 +00:00
|
|
|
"""Return emeter information keyed with the day/month.
|
2021-11-19 15:41:49 +00:00
|
|
|
|
2023-02-18 19:53:02 +00:00
|
|
|
The incoming data is a list of dictionaries::
|
2021-11-19 15:41:49 +00:00
|
|
|
|
2023-02-18 19:53:02 +00:00
|
|
|
[{'year': int,
|
|
|
|
'month': int,
|
|
|
|
'day': int, <-- for get_daystat not get_monthstat
|
|
|
|
'energy_wh': int, <-- for emeter in some versions (wh)
|
|
|
|
'energy': float <-- for emeter in other versions (kwh)
|
|
|
|
}, ...]
|
|
|
|
|
|
|
|
:return: a dictionary keyed by day or month with energy as the value.
|
|
|
|
"""
|
|
|
|
if not data:
|
2021-11-19 15:41:49 +00:00
|
|
|
return {}
|
|
|
|
|
2023-02-18 19:53:02 +00:00
|
|
|
scale: float = 1
|
2021-11-19 15:41:49 +00:00
|
|
|
|
2023-02-18 19:53:02 +00:00
|
|
|
if "energy_wh" in data[0]:
|
|
|
|
value_key = "energy_wh"
|
|
|
|
if kwh:
|
|
|
|
scale = 1 / 1000
|
|
|
|
else:
|
|
|
|
value_key = "energy"
|
|
|
|
if not kwh:
|
|
|
|
scale = 1000
|
2021-11-19 15:41:49 +00:00
|
|
|
|
2024-01-05 01:01:00 +00:00
|
|
|
if key is None:
|
|
|
|
# Return all the data
|
|
|
|
return {entry[entry_key]: entry[value_key] * scale for entry in data}
|
|
|
|
|
|
|
|
# In this case we want a specific key in the data
|
|
|
|
# i.e. the current day or month.
|
|
|
|
#
|
|
|
|
# Since we usually want the data at the end of the list so we can
|
|
|
|
# optimize the search by starting at the end and avoid scaling
|
|
|
|
# the data we don't need.
|
|
|
|
#
|
|
|
|
for entry in reversed(data):
|
|
|
|
if entry[entry_key] == key:
|
|
|
|
return {entry[entry_key]: entry[value_key] * scale}
|
|
|
|
|
|
|
|
return {}
|