2021-09-23 15:58:19 +00:00
|
|
|
"""Module for emeter container."""
|
2024-04-16 18:21:20 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-09-23 15:58:19 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class EmeterStatus(dict):
|
|
|
|
"""Container for converting different representations of emeter data.
|
|
|
|
|
|
|
|
Newer FW/HW versions postfix the variable names with the used units,
|
|
|
|
where-as the olders do not have this feature.
|
|
|
|
|
|
|
|
This class automatically converts between these two to allow
|
|
|
|
backwards and forwards compatibility.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@property
|
2024-04-17 13:39:24 +00:00
|
|
|
def voltage(self) -> float | None:
|
2021-09-23 15:58:19 +00:00
|
|
|
"""Return voltage in V."""
|
|
|
|
try:
|
|
|
|
return self["voltage"]
|
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
2024-04-17 13:39:24 +00:00
|
|
|
def power(self) -> float | None:
|
2021-09-23 15:58:19 +00:00
|
|
|
"""Return power in W."""
|
|
|
|
try:
|
|
|
|
return self["power"]
|
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
2024-04-17 13:39:24 +00:00
|
|
|
def current(self) -> float | None:
|
2021-09-23 15:58:19 +00:00
|
|
|
"""Return current in A."""
|
|
|
|
try:
|
|
|
|
return self["current"]
|
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
2024-04-17 13:39:24 +00:00
|
|
|
def total(self) -> float | None:
|
2021-09-23 15:58:19 +00:00
|
|
|
"""Return total in kWh."""
|
|
|
|
try:
|
|
|
|
return self["total"]
|
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def __repr__(self):
|
2023-10-29 22:15:42 +00:00
|
|
|
return (
|
|
|
|
f"<EmeterStatus power={self.power} voltage={self.voltage}"
|
|
|
|
f" current={self.current} total={self.total}>"
|
|
|
|
)
|
2021-09-23 15:58:19 +00:00
|
|
|
|
|
|
|
def __getitem__(self, item):
|
2023-10-29 22:15:42 +00:00
|
|
|
"""Return value in wanted units."""
|
2021-09-23 15:58:19 +00:00
|
|
|
valid_keys = [
|
|
|
|
"voltage_mv",
|
|
|
|
"power_mw",
|
|
|
|
"current_ma",
|
|
|
|
"energy_wh",
|
|
|
|
"total_wh",
|
|
|
|
"voltage",
|
|
|
|
"power",
|
|
|
|
"current",
|
|
|
|
"total",
|
|
|
|
"energy",
|
|
|
|
]
|
|
|
|
|
|
|
|
# 1. if requested data is available, return it
|
2023-10-29 22:15:42 +00:00
|
|
|
if item in super().keys(): # noqa: SIM118
|
2021-09-23 15:58:19 +00:00
|
|
|
return super().__getitem__(item)
|
|
|
|
# otherwise decide how to convert it
|
|
|
|
else:
|
|
|
|
if item not in valid_keys:
|
|
|
|
raise KeyError(item)
|
|
|
|
if "_" in item: # upscale
|
|
|
|
return super().__getitem__(item[: item.find("_")]) * 1000
|
|
|
|
else: # downscale
|
2023-10-29 22:15:42 +00:00
|
|
|
for i in super().keys(): # noqa: SIM118
|
2024-02-02 16:29:14 +00:00
|
|
|
if (
|
|
|
|
i.startswith(item)
|
|
|
|
and (value := self.__getitem__(i)) is not None
|
|
|
|
):
|
|
|
|
return value / 1000
|
2021-09-23 15:58:19 +00:00
|
|
|
|
2024-08-30 14:13:14 +00:00
|
|
|
_LOGGER.debug("Unable to find value for '%s'", item)
|
2021-09-23 15:58:19 +00:00
|
|
|
return None
|