Skip to content

Commit

Permalink
better support for openwb
Browse files Browse the repository at this point in the history
before openwb support was hard coded to solar phase_3
  • Loading branch information
BenediktSeidl committed Oct 3, 2021
1 parent 506bc53 commit d5fcb1a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions prpd_usb/output/mqtt_openwb.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@

Direct = namedtuple('Direct', ['key'])
Map = namedtuple('Map', ['keys', 'function'])
MultiMap = namedtuple('Map', ['keys', 'functions'])

def _neg(value):
return value * -1

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')),
Expand All @@ -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')),
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_openwb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down

0 comments on commit d5fcb1a

Please sign in to comment.