2021-11-07 01:41:12 +00:00
|
|
|
"""Implementation of the emeter module."""
|
2021-11-19 15:41:49 +00:00
|
|
|
from datetime import datetime
|
2024-01-05 01:01:00 +00:00
|
|
|
from typing import Dict, List, Optional, Union
|
2021-11-19 15:41:49 +00:00
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
from ...emeterstatus import EmeterStatus
|
2021-11-07 01:41:12 +00:00
|
|
|
from .usage import Usage
|
|
|
|
|
|
|
|
|
|
|
|
class Emeter(Usage):
|
|
|
|
"""Emeter module."""
|
|
|
|
|
|
|
|
@property # type: ignore
|
|
|
|
def realtime(self) -> EmeterStatus:
|
|
|
|
"""Return current energy readings."""
|
|
|
|
return EmeterStatus(self.data["get_realtime"])
|
|
|
|
|
2021-11-19 15:41:49 +00:00
|
|
|
@property
|
|
|
|
def emeter_today(self) -> Optional[float]:
|
|
|
|
"""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)
|
2021-11-19 15:41:49 +00:00
|
|
|
return data.get(today)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def emeter_this_month(self) -> Optional[float]:
|
|
|
|
"""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)
|
2021-11-19 15:41:49 +00:00
|
|
|
return data.get(current_month)
|
|
|
|
|
2021-11-07 01:41:12 +00:00
|
|
|
async def erase_stats(self):
|
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
|
|
|
|
2022-01-29 19:36:08 +00:00
|
|
|
async def get_realtime(self):
|
|
|
|
"""Return real-time statistics."""
|
|
|
|
return await self.call("get_realtime")
|
|
|
|
|
2023-02-18 19:53:02 +00:00
|
|
|
async def get_daystat(self, *, year=None, month=None, kwh=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
|
|
|
|
|
|
|
|
async def get_monthstat(self, *, year=None, kwh=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,
|
|
|
|
data: List[Dict[str, Union[int, float]]],
|
|
|
|
entry_key: str,
|
2024-01-24 08:10:55 +00:00
|
|
|
kwh: bool = True,
|
2024-01-05 01:01:00 +00:00
|
|
|
key: Optional[int] = None,
|
|
|
|
) -> Dict[Union[int, float], Union[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 {}
|