@@ -169,10 +169,10 @@ def on_connect(client, userdata, flags, rc):
169169
170170def on_message (client , userdata , msg ):
171171 try :
172- global last_changed , grid_power , grid_current , grid_voltage , grid_forward , grid_reverse , \
173- grid_L1_power , grid_L1_current , grid_L1_voltage , grid_L1_frequency , grid_L1_forward , grid_L1_reverse , \
174- grid_L2_power , grid_L2_current , grid_L2_voltage , grid_L2_frequency , grid_L2_forward , grid_L2_reverse , \
175- grid_L3_power , grid_L3_current , grid_L3_voltage , grid_L3_frequency , grid_L3_forward , grid_L3_reverse
172+ global last_changed , grid_power , grid_current , grid_voltage , grid_forward , grid_reverse
173+ global grid_L1_power , grid_L1_current , grid_L1_voltage , grid_L1_frequency , grid_L1_forward , grid_L1_reverse
174+ global grid_L2_power , grid_L2_current , grid_L2_voltage , grid_L2_frequency , grid_L2_forward , grid_L2_reverse
175+ global grid_L3_power , grid_L3_current , grid_L3_voltage , grid_L3_frequency , grid_L3_forward , grid_L3_reverse
176176
177177 # get JSON from topic
178178 if msg .topic == config ["MQTT" ]["topic" ]:
@@ -215,7 +215,9 @@ def on_message(client, userdata, msg):
215215 "L1" in jsonpayload ["grid" ]
216216 and "power" in jsonpayload ["grid" ]["L1" ]
217217 ):
218- grid_L1_power = float (jsonpayload ["grid" ]["L1" ]["power" ])
218+ grid_L1_power = float (
219+ jsonpayload ["grid" ]["L1" ]["power" ]
220+ )
219221 grid_L1_voltage = (
220222 float (jsonpayload ["grid" ]["L1" ]["voltage" ])
221223 if "voltage" in jsonpayload ["grid" ]["L1" ]
@@ -228,7 +230,6 @@ def on_message(client, userdata, msg):
228230 grid_L1_power / grid_L1_voltage
229231 if grid_L1_voltage != 0
230232 else 0
231-
232233 )
233234 )
234235 grid_L1_frequency = (
@@ -252,7 +253,9 @@ def on_message(client, userdata, msg):
252253 "L2" in jsonpayload ["grid" ]
253254 and "power" in jsonpayload ["grid" ]["L2" ]
254255 ):
255- grid_L2_power = float (jsonpayload ["grid" ]["L2" ]["power" ])
256+ grid_L2_power = float (
257+ jsonpayload ["grid" ]["L2" ]["power" ]
258+ )
256259 grid_L2_voltage = (
257260 float (jsonpayload ["grid" ]["L2" ]["voltage" ])
258261 if "voltage" in jsonpayload ["grid" ]["L2" ]
@@ -288,7 +291,9 @@ def on_message(client, userdata, msg):
288291 "L3" in jsonpayload ["grid" ]
289292 and "power" in jsonpayload ["grid" ]["L3" ]
290293 ):
291- grid_L3_power = float (jsonpayload ["grid" ]["L3" ]["power" ])
294+ grid_L3_power = float (
295+ jsonpayload ["grid" ]["L3" ]["power" ]
296+ )
292297 grid_L3_voltage = (
293298 float (jsonpayload ["grid" ]["L3" ]["voltage" ])
294299 if "voltage" in jsonpayload ["grid" ]["L3" ]
@@ -301,7 +306,6 @@ def on_message(client, userdata, msg):
301306 grid_L3_power / grid_L3_voltage
302307 if grid_L3_voltage != 0
303308 else 0
304-
305309 )
306310 )
307311 grid_L3_frequency = (
@@ -325,21 +329,27 @@ def on_message(client, userdata, msg):
325329 elif "power_L1" in jsonpayload ["grid" ]:
326330 grid_L1_power = float (jsonpayload ["grid" ]["power_L1" ])
327331 grid_L1_voltage = float (config ["DEFAULT" ]["voltage" ])
328- grid_L1_current = grid_L1_power / float (config ["DEFAULT" ]["voltage" ])
332+ grid_L1_current = grid_L1_power / float (
333+ config ["DEFAULT" ]["voltage" ]
334+ )
329335 grid_L1_frequency = None
330336 grid_L1_forward = 0
331337 grid_L1_reverse = 0
332338 elif "power_L2" in jsonpayload ["grid" ]:
333339 grid_L2_power = float (jsonpayload ["grid" ]["power_L2" ])
334340 grid_L2_voltage = float (config ["DEFAULT" ]["voltage" ])
335- grid_L2_current = grid_L2_power / float (config ["DEFAULT" ]["voltage" ])
341+ grid_L2_current = grid_L2_power / float (
342+ config ["DEFAULT" ]["voltage" ]
343+ )
336344 grid_L2_frequency = None
337345 grid_L2_forward = 0
338346 grid_L2_reverse = 0
339347 elif "power_L3" in jsonpayload ["grid" ]:
340348 grid_L3_power = float (jsonpayload ["grid" ]["power_L3" ])
341349 grid_L3_voltage = float (config ["DEFAULT" ]["voltage" ])
342- grid_L3_current = grid_L3_power / float (config ["DEFAULT" ]["voltage" ])
350+ grid_L3_current = grid_L3_power / float (
351+ config ["DEFAULT" ]["voltage" ]
352+ )
343353 grid_L3_frequency = None
344354 grid_L3_forward = 0
345355 grid_L3_reverse = 0
@@ -445,34 +455,38 @@ def _update(self):
445455 round (grid_voltage , 2 ) if grid_voltage is not None else None
446456 )
447457 if grid_forward is not None :
448- self ._dbusservice ["/Ac/Energy/Forward" ] = (
449- round (grid_forward , 2 )
450- )
458+ self ._dbusservice ["/Ac/Energy/Forward" ] = round (grid_forward , 2 )
451459 if grid_reverse is not None :
452- self ._dbusservice ["/Ac/Energy/Reverse" ] = (
453- round (grid_reverse , 2 )
454- )
460+ self ._dbusservice ["/Ac/Energy/Reverse" ] = round (grid_reverse , 2 )
455461
456462 if grid_L1_power is not None :
457463 self ._dbusservice ["/Ac/L1/Power" ] = (
458464 round (grid_L1_power , 2 ) if grid_L1_power is not None else None
459465 )
460466 self ._dbusservice ["/Ac/L1/Current" ] = (
461- round (grid_L1_current , 2 ) if grid_L1_current is not None else None
467+ round (grid_L1_current , 2 )
468+ if grid_L1_current is not None
469+ else None
462470 )
463471 self ._dbusservice ["/Ac/L1/Voltage" ] = (
464- round (grid_L1_voltage , 2 ) if grid_L1_voltage is not None else None
472+ round (grid_L1_voltage , 2 )
473+ if grid_L1_voltage is not None
474+ else None
465475 )
466476 self ._dbusservice ["/Ac/L1/Frequency" ] = (
467477 round (grid_L1_frequency , 2 )
468478 if grid_L1_frequency is not None
469479 else None
470480 )
471481 self ._dbusservice ["/Ac/L1/Energy/Forward" ] = (
472- round (grid_L1_forward , 2 ) if grid_L1_forward is not None else None
482+ round (grid_L1_forward , 2 )
483+ if grid_L1_forward is not None
484+ else None
473485 )
474486 self ._dbusservice ["/Ac/L1/Energy/Reverse" ] = (
475- round (grid_L1_reverse , 2 ) if grid_L1_reverse is not None else None
487+ round (grid_L1_reverse , 2 )
488+ if grid_L1_reverse is not None
489+ else None
476490 )
477491 else :
478492 self ._dbusservice ["/Ac/L1/Power" ] = (
@@ -497,43 +511,59 @@ def _update(self):
497511 round (grid_L2_power , 2 ) if grid_L2_power is not None else None
498512 )
499513 self ._dbusservice ["/Ac/L2/Current" ] = (
500- round (grid_L2_current , 2 ) if grid_L2_current is not None else None
514+ round (grid_L2_current , 2 )
515+ if grid_L2_current is not None
516+ else None
501517 )
502518 self ._dbusservice ["/Ac/L2/Voltage" ] = (
503- round (grid_L2_voltage , 2 ) if grid_L2_voltage is not None else None
519+ round (grid_L2_voltage , 2 )
520+ if grid_L2_voltage is not None
521+ else None
504522 )
505523 self ._dbusservice ["/Ac/L2/Frequency" ] = (
506524 round (grid_L2_frequency , 2 )
507525 if grid_L2_frequency is not None
508526 else None
509527 )
510528 self ._dbusservice ["/Ac/L2/Energy/Forward" ] = (
511- round (grid_L2_forward , 2 ) if grid_L2_forward is not None else None
529+ round (grid_L2_forward , 2 )
530+ if grid_L2_forward is not None
531+ else None
512532 )
513533 self ._dbusservice ["/Ac/L2/Energy/Reverse" ] = (
514- round (grid_L2_reverse , 2 ) if grid_L2_reverse is not None else None
534+ round (grid_L2_reverse , 2 )
535+ if grid_L2_reverse is not None
536+ else None
515537 )
516538
517539 if grid_L3_power is not None :
518540 self ._dbusservice ["/Ac/L3/Power" ] = (
519541 round (grid_L3_power , 2 ) if grid_L3_power is not None else None
520542 )
521543 self ._dbusservice ["/Ac/L3/Current" ] = (
522- round (grid_L3_current , 2 ) if grid_L3_current is not None else None
544+ round (grid_L3_current , 2 )
545+ if grid_L3_current is not None
546+ else None
523547 )
524548 self ._dbusservice ["/Ac/L3/Voltage" ] = (
525- round (grid_L3_voltage , 2 ) if grid_L3_voltage is not None else None
549+ round (grid_L3_voltage , 2 )
550+ if grid_L3_voltage is not None
551+ else None
526552 )
527553 self ._dbusservice ["/Ac/L3/Frequency" ] = (
528554 round (grid_L3_frequency , 2 )
529555 if grid_L3_frequency is not None
530556 else None
531557 )
532558 self ._dbusservice ["/Ac/L3/Energy/Forward" ] = (
533- round (grid_L3_forward , 2 ) if grid_L3_forward is not None else None
559+ round (grid_L3_forward , 2 )
560+ if grid_L3_forward is not None
561+ else None
534562 )
535563 self ._dbusservice ["/Ac/L3/Energy/Reverse" ] = (
536- round (grid_L3_reverse , 2 ) if grid_L3_reverse is not None else None
564+ round (grid_L3_reverse , 2 )
565+ if grid_L3_reverse is not None
566+ else None
537567 )
538568
539569 logging .debug (
@@ -618,7 +648,12 @@ def main():
618648 DBusGMainLoop (set_as_default = True )
619649
620650 # MQTT setup
621- client = mqtt .Client ("MqttGrid_" + get_vrm_portal_id () + "_" + str (config ["DEFAULT" ]["device_instance" ]))
651+ client = mqtt .Client (
652+ "MqttGrid_"
653+ + get_vrm_portal_id ()
654+ + "_"
655+ + str (config ["DEFAULT" ]["device_instance" ])
656+ )
622657 client .on_disconnect = on_disconnect
623658 client .on_connect = on_connect
624659 client .on_message = on_message
@@ -755,10 +790,18 @@ def _n(p, v):
755790 )
756791
757792 DbusMqttGridService (
758- servicename = "com.victronenergy." + device_type + ".mqtt_" + device_type + "_"
793+ servicename = "com.victronenergy."
794+ + device_type
795+ + ".mqtt_"
796+ + device_type
797+ + "_"
759798 + str (config ["DEFAULT" ]["device_instance" ]),
760799 deviceinstance = int (config ["DEFAULT" ]["device_instance" ]),
761- customname = config ["DEFAULT" ]["device_name" ] if config ["DEFAULT" ]["device_name" ] != "MQTT Grid" else "MQTT " + device_type_name ,
800+ customname = (
801+ config ["DEFAULT" ]["device_name" ]
802+ if config ["DEFAULT" ]["device_name" ] != "MQTT Grid"
803+ else "MQTT " + device_type_name
804+ ),
762805 paths = paths_dbus ,
763806 )
764807
0 commit comments