Expose IOT emeter info as features (#844)

Exposes IOT emeter information using features, bases on #843 to allow
defining the units.


---------

Co-authored-by: Steven B <51370195+sdb9696@users.noreply.github.com>
This commit is contained in:
Teemu R
2024-04-25 14:59:17 +02:00
committed by GitHub
parent 724dad02f7
commit 1ff3162112
2 changed files with 87 additions and 4 deletions

View File

@@ -4,13 +4,77 @@ from __future__ import annotations
from datetime import datetime
from ... import Device
from ...emeterstatus import EmeterStatus
from ...feature import Feature
from .usage import Usage
class Emeter(Usage):
"""Emeter module."""
def __init__(self, device: Device, module: str):
super().__init__(device, module)
self._add_feature(
Feature(
device,
name="Current consumption",
attribute_getter="current_consumption",
container=self,
unit="W",
id="current_power_w", # for homeassistant backwards compat
)
)
self._add_feature(
Feature(
device,
name="Today's consumption",
attribute_getter="emeter_today",
container=self,
unit="kWh",
id="today_energy_kwh", # for homeassistant backwards compat
)
)
self._add_feature(
Feature(
device,
name="This month's consumption",
attribute_getter="emeter_this_month",
container=self,
unit="kWh",
)
)
self._add_feature(
Feature(
device,
name="Total consumption since reboot",
attribute_getter="emeter_total",
container=self,
unit="kWh",
id="total_energy_kwh", # for homeassistant backwards compat
)
)
self._add_feature(
Feature(
device,
name="Voltage",
attribute_getter="voltage",
container=self,
unit="V",
id="voltage", # for homeassistant backwards compat
)
)
self._add_feature(
Feature(
device,
name="Current",
attribute_getter="current",
container=self,
unit="A",
id="current_a", # for homeassistant backwards compat
)
)
@property # type: ignore
def realtime(self) -> EmeterStatus:
"""Return current energy readings."""
@@ -32,6 +96,26 @@ class Emeter(Usage):
data = self._convert_stat_data(raw_data, entry_key="month", key=current_month)
return data.get(current_month)
@property
def current_consumption(self) -> float | None:
"""Get the current power consumption in Watt."""
return self.realtime.power
@property
def emeter_total(self) -> float | None:
"""Return total consumption since last reboot in kWh."""
return self.realtime.total
@property
def current(self) -> float | None:
"""Return the current in A."""
return self.realtime.current
@property
def voltage(self) -> float | None:
"""Get the current voltage in V."""
return self.realtime.voltage
async def erase_stats(self):
"""Erase all stats.