diff --git a/prpd_usb/output/mqtt_openwb.py b/prpd_usb/output/mqtt_openwb.py index 18f97df..3f686b0 100644 --- a/prpd_usb/output/mqtt_openwb.py +++ b/prpd_usb/output/mqtt_openwb.py @@ -7,6 +7,7 @@ Direct = namedtuple('Direct', ['key']) Map = namedtuple('Map', ['keys', 'function']) +MultiMap = namedtuple('Map', ['keys', 'functions']) def _neg(value): return value * -1 @@ -14,6 +15,9 @@ def _neg(value): def _sum(*values): return sum(values) +def _m_sum(*values): + return [sum(values)] + MAPPING = { "openWB/set/evu/W": Map(keys=(('grid', 'power_w_phase_1'), ('grid', 'power_w_phase_2'), ('grid', 'power_w_phase_3')), function=_sum), "openWB/set/evu/APhase1": Direct(('grid', 'current_phase_1')), @@ -26,8 +30,8 @@ def _sum(*values): "openWB/set/evu/VPhase3": Direct(('grid', 'voltage_phase_3')), "openWB/set/evu/HzFrequenz": Direct(('platform', 'frequency')), - "openWB/set/pv/1/W": Map(keys=(('solar', 'w_phase_3'),), function=_neg), - "openWB/set/pv/1/WhCounter": Direct(('solar', 'total_phase_3')), + "openWB/set/pv/1/W": MultiMap(keys=(('solar', 'w_phase_1'), ('solar', 'w_phase_2'), ('solar', 'w_phase_3'),), functions=(_m_sum, _neg)), + "openWB/set/pv/1/WhCounter": Map(keys=(('solar', 'total_phase_1'), ('solar', 'total_phase_2'), ('solar', 'total_phase_3')), function=_sum), "openWB/set/houseBattery/W": Map(keys=(('battery', 'power'),), function=_neg), "openWB/set/houseBattery/WhImported": Direct(('battery', 'consumed')), @@ -46,6 +50,11 @@ def transform_to_openwb(prpd_data): for topic, mapping in MAPPING.items(): if isinstance(mapping, Direct): payload = data[mapping.key] + elif isinstance(mapping, MultiMap): + values = [data[k] for k in mapping.keys] + for function in mapping.functions: + values = function(*values) + payload = values else: keys = [data[k] for k in mapping.keys] payload = mapping.function(*keys) diff --git a/tests/test_openwb.py b/tests/test_openwb.py index 8a56cb1..5bfab75 100644 --- a/tests/test_openwb.py +++ b/tests/test_openwb.py @@ -21,7 +21,7 @@ def test_openwb_37bi(): {"payload": "225.60000000000002", "topic": "openWB/set/evu/VPhase2"}, {"payload": "223.60000000000002", "topic": "openWB/set/evu/VPhase3"}, {"payload": "49.99", "topic": "openWB/set/evu/HzFrequenz"}, - {"payload": "1", "topic": "openWB/set/pv/1/W"}, + {"payload": "4", "topic": "openWB/set/pv/1/W"}, {"payload": "25054400", "topic": "openWB/set/pv/1/WhCounter"}, {"payload": "-332", "topic": "openWB/set/houseBattery/W"}, {"payload": "5868399", "topic": "openWB/set/houseBattery/WhImported"},