Skip to content

Commit cce0991

Browse files
committed
tasmota: Add combined 'energy' connector
1 parent a276382 commit cce0991

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

docs/interfaces/tasmota.rst

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Tasmota Interface
1515
.. automethod:: color_rgbw
1616
.. automethod:: color_rgbcct
1717
.. automethod:: ir_receiver
18+
.. automethod:: energy
1819
.. automethod:: energy_power
1920
.. automethod:: energy_voltage
2021
.. automethod:: energy_current
@@ -23,4 +24,5 @@ Tasmota Interface
2324
.. automethod:: energy_reactive_power
2425
.. automethod:: energy_apparent_power
2526

27+
.. autonamedtuple:: TasmotaEnergyMeasurement
2628
.. autonamedtuple:: TasmotaTelemetry

shc/interfaces/tasmota.py

+55
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,15 @@ def ir_receiver(self) -> "TasmotaIRReceiverConnector":
284284
"""
285285
return self._get_or_create_connector(TasmotaIRReceiverConnector)
286286

287+
def energy(self) -> "TasmotaEnergyConnector":
288+
"""
289+
Returns a *subscribable* :class:`float`-typed Connector, publishing the currently measured power consumption
290+
values in from the Tasmota device.
291+
292+
Unavailable power/energy values are set to NaN.
293+
"""
294+
return self._get_or_create_connector(TasmotaEnergyConnector)
295+
287296
def energy_power(self) -> "TasmotaEnergyPowerConnector":
288297
"""
289298
Returns a *subscribable* :class:`float`-typed Connector, publishing the currently measured power consumption in
@@ -597,6 +606,52 @@ def _decode(self, value: JSONType) -> float:
597606
return float(value['Factor'])
598607

599608

609+
class TasmotaEnergyMeasurement(NamedTuple):
610+
#: the currently measured power consumption in W
611+
power: float
612+
#: the currently measured mains voltage in V
613+
voltage: float
614+
#: the currently measured current flow in A
615+
current: float
616+
#: the currently measured apparent power in VA
617+
apparent_power: float
618+
#: the currently measured reactive power in VAr
619+
reactive_power: float
620+
#: the currently measured power factor
621+
power_factor: float
622+
#: the currently measured mains frequency in Hz
623+
frequency: float
624+
#: the total energy consumption in kWh
625+
total_energy: float
626+
#: today's energy consumption in kWh
627+
total_energy_today: float
628+
#: yesterday's energy consumption in kWh
629+
total_energy_yesterday: float
630+
631+
632+
class TasmotaEnergyConnector(AbstractTasmotaConnector[TasmotaEnergyMeasurement]):
633+
type = TasmotaEnergyMeasurement
634+
NAN = float('NaN')
635+
636+
def __init__(self, _interface: TasmotaInterface):
637+
super().__init__("ENERGY")
638+
639+
def _decode(self, value: JSONType) -> TasmotaEnergyMeasurement:
640+
assert isinstance(value, dict)
641+
return TasmotaEnergyMeasurement(
642+
float(value.get('Power', self.NAN)),
643+
float(value.get('Voltage', self.NAN)),
644+
float(value.get('Current', self.NAN)),
645+
float(value.get('ApparentPower', self.NAN)),
646+
float(value.get('ReactivePower', self.NAN)),
647+
float(value.get('Factor', self.NAN)),
648+
float(value.get('Frequency', self.NAN)),
649+
float(value.get('Total', self.NAN)),
650+
float(value.get('Today', self.NAN)),
651+
float(value.get('Yesterday', self.NAN)),
652+
)
653+
654+
600655
class TasmotaOnlineConnector(Readable[bool], Subscribable[bool]):
601656
type = bool
602657

0 commit comments

Comments
 (0)