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
|
|
|
|
|
|
|
|
from typing import TYPE_CHECKING
|
2024-02-19 17:01:31 +00:00
|
|
|
|
|
|
|
from ...emeterstatus import EmeterStatus
|
|
|
|
from ...feature import Feature
|
|
|
|
from ..smartmodule import SmartModule
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from ..smartdevice import SmartDevice
|
|
|
|
|
|
|
|
|
|
|
|
class EnergyModule(SmartModule):
|
|
|
|
"""Implementation of energy monitoring module."""
|
|
|
|
|
|
|
|
REQUIRED_COMPONENT = "energy_monitoring"
|
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
def __init__(self, device: SmartDevice, module: str):
|
2024-02-19 17:01:31 +00:00
|
|
|
super().__init__(device, module)
|
|
|
|
self._add_feature(
|
|
|
|
Feature(
|
|
|
|
device,
|
2024-05-07 09:13:35 +00:00
|
|
|
"consumption_current",
|
2024-02-19 17:01:31 +00:00
|
|
|
name="Current consumption",
|
|
|
|
attribute_getter="current_power",
|
|
|
|
container=self,
|
2024-04-22 09:25:30 +00:00
|
|
|
unit="W",
|
2024-04-29 11:31:42 +00:00
|
|
|
precision_hint=1,
|
2024-05-07 09:13:35 +00:00
|
|
|
category=Feature.Category.Primary,
|
2024-02-19 17:01:31 +00:00
|
|
|
)
|
2024-04-22 09:25:30 +00:00
|
|
|
)
|
2024-02-19 17:01:31 +00:00
|
|
|
self._add_feature(
|
|
|
|
Feature(
|
|
|
|
device,
|
2024-05-07 09:13:35 +00:00
|
|
|
"consumption_today",
|
2024-02-19 17:01:31 +00:00
|
|
|
name="Today's consumption",
|
|
|
|
attribute_getter="emeter_today",
|
|
|
|
container=self,
|
2024-04-22 09:25:30 +00:00
|
|
|
unit="Wh",
|
2024-04-29 11:31:42 +00:00
|
|
|
precision_hint=2,
|
2024-05-07 09:13:35 +00:00
|
|
|
category=Feature.Category.Info,
|
2024-02-19 17:01:31 +00:00
|
|
|
)
|
2024-04-22 09:25:30 +00:00
|
|
|
)
|
2024-02-19 17:01:31 +00:00
|
|
|
self._add_feature(
|
|
|
|
Feature(
|
|
|
|
device,
|
2024-05-07 09:13:35 +00:00
|
|
|
"consumption_this_month",
|
2024-02-19 17:01:31 +00:00
|
|
|
name="This month's consumption",
|
|
|
|
attribute_getter="emeter_this_month",
|
|
|
|
container=self,
|
2024-04-22 09:25:30 +00:00
|
|
|
unit="Wh",
|
2024-04-29 11:31:42 +00:00
|
|
|
precision_hint=2,
|
2024-05-07 09:13:35 +00:00
|
|
|
category=Feature.Category.Info,
|
2024-02-19 17:01:31 +00:00
|
|
|
)
|
2024-04-22 09:25:30 +00:00
|
|
|
)
|
2024-02-19 17:01:31 +00:00
|
|
|
|
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-04-22 09:25:30 +00:00
|
|
|
def current_power(self) -> float | None:
|
|
|
|
"""Current power in watts."""
|
|
|
|
if power := self.energy.get("current_power"):
|
|
|
|
return power / 1_000
|
|
|
|
return None
|
2024-02-19 17:01:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
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
|
|
|
|
|
|
|
@property
|
|
|
|
def emeter_realtime(self):
|
|
|
|
"""Get the emeter status."""
|
|
|
|
# TODO: Perhaps we should get rid of emeterstatus altogether for smartdevices
|
|
|
|
return EmeterStatus(
|
|
|
|
{
|
|
|
|
"power_mw": self.energy.get("current_power"),
|
2024-04-22 09:25:30 +00:00
|
|
|
"total": self.energy.get("today_energy") / 1_000,
|
2024-02-19 17:01:31 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
2024-04-17 13:39:24 +00:00
|
|
|
def emeter_this_month(self) -> float | None:
|
2024-02-19 17:01:31 +00:00
|
|
|
"""Get the emeter value for this month."""
|
2024-04-22 09:25:30 +00:00
|
|
|
return self.energy.get("month_energy")
|
2024-02-19 17:01:31 +00:00
|
|
|
|
|
|
|
@property
|
2024-04-17 13:39:24 +00:00
|
|
|
def emeter_today(self) -> float | None:
|
2024-02-19 17:01:31 +00:00
|
|
|
"""Get the emeter value for today."""
|
2024-04-22 09:25:30 +00:00
|
|
|
return self.energy.get("today_energy")
|