diff --git a/packages/modules/smarthome/avmhomeautomation/avmcommon.py b/packages/modules/smarthome/avmhomeautomation/avmcommon.py index 8124f3d2c1..4dd27c5c94 100644 --- a/packages/modules/smarthome/avmhomeautomation/avmcommon.py +++ b/packages/modules/smarthome/avmhomeautomation/avmcommon.py @@ -42,9 +42,8 @@ def __init__(self): if os.path.isfile(CACHEFILE): self.logMessage(LOGLEVELDEBUG, "found an AVM cache file, trying to load") try: - f = open(CACHEFILE, 'r') - self.cache = json.loads(f.read().strip()) - f.close() + with open(CACHEFILE, 'r') as f: + self.cache = json.loads(f.read().strip()) except Exception as e: self.logMessage(LOGLEVELDEBUG, "unable to load cache file: %s" % (e)) @@ -65,9 +64,8 @@ def writeCacheToRamdisk(self): else: cacheToWrite[login] = self.cache[login] try: - f = open(CACHEFILE, 'w') - json.dump(cacheToWrite, f) - f.close() + with open(CACHEFILE, 'w') as f: + json.dump(cacheToWrite, f) except Exception as e: self.logMessage(LOGLEVELDEBUG, "unable to write cache file: %s" % (e)) @@ -166,28 +164,7 @@ def connect(self): # logMessage writes a message to the logfile for the smarthome device. def logMessage(self, level, message): - if level < self.loglevel: - return - now = time.localtime() # getstruct_time - time_string = time.strftime("%Y-%m-%d %H:%M:%S", now) - logfile_string = '/var/www/html/openWB/ramdisk/smarthome.log' - try: - if os.path.isfile(logfile_string): - f = open(logfile_string, 'a', encoding='utf8') - else: - f = open(logfile_string, 'w', encoding='utf8') - prefix = "" - if level == LOGLEVELDEBUG: - prefix = "[DEBUG] " - if level == LOGLEVELINFO: - prefix = "[INFO] " - if level == LOGLEVELERROR: - prefix = "[ERROR] " - log.debug('%s: (%s) AVM (actor: %s) %s%s' % - (time_string, self.devicenumber, self.switchname, prefix, message), file=f) - f.close() - except IOError: - pass + log.debug(f'({self.devicenumber}) AVM (actor: {self.switchname}) {message}') # getDevicesDict returns a dictionary that maps defined actor names to its # unique hardware ID (called "AIN": "Actuator Identification Number") and @@ -362,9 +339,8 @@ def getActualPower(self): outFileString = '/var/www/html/openWB/ramdisk/smarthome_device_ret' + str(self.devicenumber) self.logMessage(LOGLEVELDEBUG, "handing answer back to smarthomehandler via %s" % (outFileString)) try: - f1 = open(outFileString, 'w') - json.dump(answer, f1) - f1.close() + with open(outFileString, 'w') as f1: + json.dump(answer, f1) except IOError as e: self.logMessage(LOGLEVELERROR, "error writing power result %s" % (e)) log.debug(answer) # dump answer to stdout if file cannot be written diff --git a/packages/modules/smarthome/fronius/watt.py b/packages/modules/smarthome/fronius/watt.py index ca0eb4e505..9f805c35b8 100644 --- a/packages/modules/smarthome/fronius/watt.py +++ b/packages/modules/smarthome/fronius/watt.py @@ -1,9 +1,12 @@ #!/usr/bin/python3 +import logging import sys import json import jq import urllib.request +log = logging.getLogger(__name__) + devicenumber = str(sys.argv[1]) ipadr = str(sys.argv[2]) # IP-ADresse des Fronius Wechselrichters, mit dem der Zähler kommuniziert smid = int(sys.argv[3]) # ID des Zählers im Wechselrichter (Hauptzähler 0, weitere fortlaufend) @@ -27,7 +30,4 @@ except Exception: powerc = 0 -f1 = open('/var/www/html/openWB/ramdisk/smarthome_device_ret' + str(devicenumber), 'w') -answer = '{"power":' + str(power) + ',"powerc":' + str(powerc) + '}' -json.dump(answer, f1) -f1.close() +log.debug("Device" + str(devicenumber) + '{"power":' + str(power) + ',"powerc":' + str(powerc) + '}') diff --git a/packages/modules/smarthome/json/watt.py b/packages/modules/smarthome/json/watt.py index efe2381ad9..a5131ee715 100644 --- a/packages/modules/smarthome/json/watt.py +++ b/packages/modules/smarthome/json/watt.py @@ -1,9 +1,12 @@ #!/usr/bin/python3 +import logging import sys import json import jq import urllib.request +log = logging.getLogger(__name__) + devicenumber = str(sys.argv[1]) # Abfrage-URL, die die .json Antwort liefert. Z.B. # "http://192.168.0.150/solar_api/v1/GetMeterRealtimeData.cgi?Scope=Device&DeviceID=1" @@ -26,7 +29,4 @@ except Exception: powerc = 0 -f1 = open('/var/www/html/openWB/ramdisk/smarthome_device_ret' + str(devicenumber), 'w') -answer = '{"power":' + str(power) + ',"powerc":' + str(powerc) + '}' -json.dump(answer, f1) -f1.close() +log.debug('Device' + str(devicenumber) + '{"power":' + str(power) + ',"powerc":' + str(powerc) + '}') diff --git a/packages/modules/smarthome/mqtt/off.py b/packages/modules/smarthome/mqtt/off.py index d7cea04010..672f1e9c28 100644 --- a/packages/modules/smarthome/mqtt/off.py +++ b/packages/modules/smarthome/mqtt/off.py @@ -35,6 +35,5 @@ def on_message(client, userdata, msg) -> None: client.disconnect() file_stringpv = '/var/www/html/openWB/ramdisk/smarthome_device_' + str(devicenumber) + '_pv' pvmodus = 0 -f = open(file_stringpv, 'w') -f.write(str(pvmodus)) -f.close() +with open(file_stringpv, 'w') as f: + f.write(str(pvmodus)) diff --git a/packages/modules/smarthome/mqtt/on.py b/packages/modules/smarthome/mqtt/on.py index b5f70ed825..781d850a81 100644 --- a/packages/modules/smarthome/mqtt/on.py +++ b/packages/modules/smarthome/mqtt/on.py @@ -34,6 +34,5 @@ def on_message(client, userdata, msg) -> None: client.loop(timeout=2.0) client.disconnect() file_stringpv = '/var/www/html/openWB/ramdisk/smarthome_device_' + str(devicenumber) + '_pv' -f = open(file_stringpv, 'w') -f.write(str(1)) -f.close() +with open(file_stringpv, 'w') as f: + f.write(str(1)) diff --git a/packages/modules/smarthome/mqtt/watt.py b/packages/modules/smarthome/mqtt/watt.py index d590c36417..d1cb945fbc 100644 --- a/packages/modules/smarthome/mqtt/watt.py +++ b/packages/modules/smarthome/mqtt/watt.py @@ -69,9 +69,8 @@ def on_message(client, userdata, msg) -> None: # PV-Modus pvmodus = 0 if os.path.isfile(file_stringpv): - f = open(file_stringpv, 'r') - pvmodus = int(f.read()) - f.close() + with open(file_stringpv, 'r') as f: + pvmodus = int(f.read()) answer = '{"power":' + str(aktpower) + ',"powerc":' + str(powerc) answer += ',"on":' + str(pvmodus) + ',"temp0":' + str(tempa) answer += ',"temp1":' + str(tempb) + ',"temp2":' + str(tempc) + '}' diff --git a/packages/modules/smarthome/mystrom/watt.py b/packages/modules/smarthome/mystrom/watt.py index 3ae83cf393..e58764e96b 100644 --- a/packages/modules/smarthome/mystrom/watt.py +++ b/packages/modules/smarthome/mystrom/watt.py @@ -1,9 +1,12 @@ #!/usr/bin/python3 +import logging import sys import time import json import urllib.request +log = logging.getLogger(__name__) + named_tuple = time.localtime() # getstruct_time time_string = time.strftime("%m/%d/%Y, %H:%M:%S mystrom watty.py", named_tuple) devicenumber = str(sys.argv[1]) @@ -21,6 +24,5 @@ powerc = 0 answer = '{"power":' + str(aktpower) + ',"powerc":' + str(powerc) + ',"on":' + \ str(relais) + ',"temp0":' + str(temp) + '} ' -f1 = open('/var/www/html/openWB/ramdisk/smarthome_device_ret' + str(devicenumber), 'w') -json.dump(answer, f1) -f1.close() +with open('/var/www/html/openWB/ramdisk/smarthome_device_ret' + str(devicenumber), 'w') as f1: + log.debug('Device' + str(devicenumber) + ' ' + answer) diff --git a/packages/modules/smarthome/nibe/watt.py b/packages/modules/smarthome/nibe/watt.py index 3b9bc3f286..d723965d22 100644 --- a/packages/modules/smarthome/nibe/watt.py +++ b/packages/modules/smarthome/nibe/watt.py @@ -29,8 +29,7 @@ CurrentPower = None # Handle error case answer = '{"power":' + str(CurrentPower) + '}' -f1 = open('/var/www/html/openWB/ramdisk/smarthome_device_ret' + str(devicenumber), 'w') -json.dump(answer, f1) -f1.close() +with open('/var/www/html/openWB/ramdisk/smarthome_device_ret' + str(devicenumber), 'w') as f1: + json.dump(answer, f1) client.close() # clean disconnect from modbus server diff --git a/packages/modules/smarthome/smaem/watt.py b/packages/modules/smarthome/smaem/watt.py index bfb767a89a..f31ef9c3d2 100644 --- a/packages/modules/smarthome/smaem/watt.py +++ b/packages/modules/smarthome/smaem/watt.py @@ -33,7 +33,6 @@ import os import datetime import time -import json import signal import socket import struct @@ -68,7 +67,7 @@ def abortprogram(signal, frame): timefile = '/var/www/html/openWB/ramdisk/smarthome_device_ret' + \ str(devicenumber) + '_time' # Dummy file needed for timestamp of last metering # Logfile for additional output beside of the smarthome.log -debugfile = open('/var/www/html/openWB/ramdisk/smaem.log', 'a', newline='\r\n') + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) @@ -108,9 +107,9 @@ def abortprogram(signal, frame): "HomeManager (2) is sending in the network.") emparts = decode_speedwire(sock_data) -debugfile.write(str(datetime.datetime.now()) + ': smaserial: #' + str(smaserial) + '# - Current SMA serial number:#' + - str(emparts['serial']) + '# - watt:#' + str(int(emparts.get("pconsume"))) + '# - wattc:#' + - str("{:.3f}".format(int(emparts.get('pconsumecounter')*1000))) + '#\n') +log.debug('SMA:: smaserial: #' + str(smaserial) + '# - Current SMA serial number:#' + + str(emparts['serial']) + '# - watt:#' + str(int(emparts.get("pconsume"))) + '# - wattc:#' + + str("{:.3f}".format(int(emparts.get('pconsumecounter')*1000))) + '#') # Remember: We assume that beside of our EnergyMeter there are more SMA devices present (like HomeManager 2.0 or other # EnergyMeter) - so must not accept any data or smaserial = None @@ -118,8 +117,8 @@ def abortprogram(signal, frame): # our output variables watt = str(int(emparts.get("pconsume"))) wattc = str("{:.3f}".format(int(emparts.get('pconsumecounter')*1000))) - debugfile.write(str(datetime.datetime.now()) + ': 1 - Our SMA EM ' + str(smaserial) + - ' is sending, everything fine. watt: #' + str(watt) + '# - wattc: #' + str(wattc) + '#\n') + log.debug('SMA:: 1 - Our SMA EM ' + str(smaserial) + + ' is sending, everything fine. watt: #' + str(watt) + '# - wattc: #' + str(wattc) + '#') # Scenario 2: Our EnergyMeter is not sending but we have a returnfile which is older than n seconds (parameter # secondssincelastmetering) elif ((os.path.isfile(returnfile)) and @@ -128,29 +127,27 @@ def abortprogram(signal, frame): # We set "0" as current Power Consume (pconsume) and (from the existing ret-file) the last value for the Power # Consume Counter (pconsumecounter) watt = '0' - ret = open(returnfile, 'r') - lastvalues = ret.read() - ret.close() + with open(returnfile, 'r') as ret: + lastvalues = ret.read() timesincelastmetering = int(round(time.time(), 0)-lastmodificationtime) wattc = lastvalues[int(lastvalues.rfind('powerc')) + 9:lastvalues.find('}')] - debugfile.write(str(datetime.datetime.now()) + ': 2 - Debug: time.time(): #' + str(round(time.time(), 0)) + - '# - lastmodificationtime: #' + str(lastmodificationtime) + - '# - timesincelastmetering: #' + str(timesincelastmetering) + - '# - int(secondssincelastmetering): #' + str(int(secondssincelastmetering)) + '#\n') - debugfile.write(str(datetime.datetime.now()) + ': 2 - We create a fake ret-file. watt: #' + str(watt) + - '# - wattc: #' + str( + log.debug('SMA:: 2 - Debug: time.time(): #' + str(round(time.time(), 0)) + + '# - lastmodificationtime: #' + str(lastmodificationtime) + + '# - timesincelastmetering: #' + str(timesincelastmetering) + + '# - int(secondssincelastmetering): #' + str(int(secondssincelastmetering)) + '#') + log.debug('SMA:: 2 - We create a fake ret-file. watt: #' + str(watt) + + '# - wattc: #' + str( wattc) + '# - lastvalues: #' + lastvalues + '# - int-lastvalues.rfind-powerc: #' + - str((lastvalues.rfind('powerc'))) + '#\n') + str((lastvalues.rfind('powerc'))) + '#') # Scenario 3: Our EnergyMeter is not sending but we have a returnfile which is younger than n seconds # (parameter secondssincelastmetering) elif ((os.path.isfile(returnfile)) and (int((round(time.time(), 0)-lastmodificationtime)) < int(secondssincelastmetering))): # We have a ret-file which is younger than n seconds. We do nothing as the existing ret-file is good enough. - debugfile.write(str(datetime.datetime.now()) + - ': 3 - The existing ret-file is fine enough. round(time.time(),0): #' + str(round(time.time(), 0)) - + '# - lastmodificationtime: #' + str(lastmodificationtime) + '# - secondssincelastmetering: #' - + str(secondssincelastmetering) + '#\n') + log.debug('SMA:: 3 - The existing ret-file is fine enough. round(time.time(),0): #' + str(round(time.time(), 0)) + + '# - lastmodificationtime: #' + str(lastmodificationtime) + '# - secondssincelastmetering: #' + + str(secondssincelastmetering) + '#') sys.exit("Module SMAEM: No data received but we have historical data which is younger than " + str(secondssincelastmetering) + " seconds.") else: @@ -162,20 +159,16 @@ def abortprogram(signal, frame): # Module SMAEM: No data received and no historical data since boot time # Leistungsmessung smaem [...] Fehlermeldung: [Errno 2] No such file or directory: # '/var/www/html/openWB/ramdisk/smarthome_device_ret1' - debugfile.write(str(datetime.datetime.now()) + ': 4 - No data received and no historical data since boottime\n') + log.debug('SMA:: 4 - No data received and no historical data since boottime') sys.exit(str(datetime.datetime.now()) + ": Module SMAEM: No data received and no historical data since boottime") # General output section answer = '{"power":' + watt + ',"powerc":' + wattc + '}' -f = open(returnfile, 'w') -json.dump(answer, f) -f.close() -t = open(timefile, 'w') -t.write(str(datetime.datetime.now()) + - ': File is created by Smarthome module SMAEM for validating the timestamp of the last return-file creation.') -t.close() +with open(timefile, 'w') as t: + t.write(str(datetime.datetime.now()) + + ': File is created by Smarthome module SMAEM for validating ' + 'the timestamp of the last return-file creation.') -debugfile.write(str(datetime.datetime.now()) + ': 99 - Output answer: #' + answer + '#\n') -debugfile.close() +log.debug('SMA:: 99 - Output answer: #' + answer + '#') diff --git a/packages/modules/smarthome/tasmota/watt.py b/packages/modules/smarthome/tasmota/watt.py index b5f9a54832..533d796d39 100644 --- a/packages/modules/smarthome/tasmota/watt.py +++ b/packages/modules/smarthome/tasmota/watt.py @@ -26,6 +26,5 @@ relais = 1 powerc = 0 answer = '{"power":' + str(aktpower) + ',"powerc":' + str(powerc) + ',"on":' + str(relais) + '} ' -f1 = open('/var/www/html/openWB/ramdisk/smarthome_device_ret' + str(devicenumber), 'w') -json.dump(answer, f1) -f1.close() +with open('/var/www/html/openWB/ramdisk/smarthome_device_ret' + str(devicenumber), 'w') as f1: + json.dump(answer, f1) diff --git a/packages/modules/smarthome/viessmann/off.py b/packages/modules/smarthome/viessmann/off.py index 12cf698ef9..d294441d9e 100644 --- a/packages/modules/smarthome/viessmann/off.py +++ b/packages/modules/smarthome/viessmann/off.py @@ -1,14 +1,10 @@ #!/usr/bin/python3 import sys -import os -import time from pymodbus.client.sync import ModbusTcpClient import logging log = logging.getLogger(__name__) -named_tuple = time.localtime() # getstruct_time -time_string = time.strftime("%m/%d/%Y, %H:%M:%S viessmann off.py", named_tuple) devicenumber = str(sys.argv[1]) ipadr = str(sys.argv[2]) uberschuss = int(sys.argv[3]) @@ -22,22 +18,14 @@ # coils read write boolean # register start 00000 # -file_string = '/var/www/html/openWB/ramdisk/smarthome_device_' + str(devicenumber) + '_viessmann.log' -file_stringpv = '/var/www/html/openWB/ramdisk/smarthome_device_' + str(devicenumber) + '_pv' -if os.path.isfile(file_string): - f = open(file_string, 'a') -else: - f = open(file_string, 'w') -log.debug('%s devicenr %s ipadr %s ueberschuss %6d try to connect (modbus)' % - (time_string, devicenumber, ipadr, uberschuss), file=f) +log.debug(f"[Viessmann {devicenumber}] devicenr {devicenumber} ipadr {ipadr} " + f"ueberschuss {uberschuss:6d} try to connect (modbus)") client = ModbusTcpClient(ipadr, port=502) rq = client.write_coil(16, False, unit=1) -log.debug(rq, file=f) +log.debug(f"[Viessmann {devicenumber}] Modbus write_coil response: {rq}") client.close() -log.debug('%s devicenr %s ipadr %s Einmalige Warmwasseraufbereitung deaktiviert CO-17 = 0 ' % - (time_string, devicenumber, ipadr), file=f) -f.close() +log.debug( + f"[Viessmann {devicenumber}] devicenr {devicenumber} ipadr {ipadr} " + "Einmalige Warmwasseraufbereitung deaktiviert CO-17 = 0") pvmodus = 0 -f = open(file_stringpv, 'w') -f.write(str(pvmodus)) -f.close() +log.debug(f"[Viessmann {devicenumber}] PV-Modus gesetzt: {pvmodus}") diff --git a/packages/modules/smarthome/viessmann/on.py b/packages/modules/smarthome/viessmann/on.py index f85a7d6d6f..5f7415cd16 100644 --- a/packages/modules/smarthome/viessmann/on.py +++ b/packages/modules/smarthome/viessmann/on.py @@ -1,14 +1,10 @@ #!/usr/bin/python3 import sys -import os -import time from pymodbus.client.sync import ModbusTcpClient import logging log = logging.getLogger(__name__) -named_tuple = time.localtime() # getstruct_time -time_string = time.strftime("%m/%d/%Y, %H:%M:%S viessmann on.py", named_tuple) devicenumber = str(sys.argv[1]) ipadr = str(sys.argv[2]) uberschuss = int(sys.argv[3]) @@ -24,21 +20,14 @@ # coils read write boolean # register start 00000 # -file_string = '/var/www/html/openWB/ramdisk/smarthome_device_' + str(devicenumber) + '_viessmann.log' -file_stringpv = '/var/www/html/openWB/ramdisk/smarthome_device_' + str(devicenumber) + '_pv' -if os.path.isfile(file_string): - f = open(file_string, 'a') -else: - f = open(file_string, 'w') -log.debug('%s devicenr %s ipadr %s ueberschuss %6d try to connect (modbus)' % - (time_string, devicenumber, ipadr, uberschuss), file=f) +log.debug( + f"[Viessmann {devicenumber}] devicenr {devicenumber} ipadr {ipadr} " + f"ueberschuss {uberschuss:6d} try to connect (modbus)") client = ModbusTcpClient(ipadr, port=502) rq = client.write_coil(16, True, unit=1) -log.debug(rq, file=f) +log.debug(f"[Viessmann {devicenumber}] Modbus write_coil response: {rq}") client.close() -log.debug('%s devicenr %s ipadr %s Einmalige Warmwasseraufbereitung aktiviert CO-17 = 1' % - (time_string, devicenumber, ipadr), file=f) -f.close() -f = open(file_stringpv, 'w') -f.write(str(1)) -f.close() +log.debug( + f"[Viessmann {devicenumber}] devicenr {devicenumber} ipadr {ipadr} " + "Einmalige Warmwasseraufbereitung aktiviert CO-17 = 1") +log.debug(f"[Viessmann {devicenumber}] PV-Modus gesetzt: 1") diff --git a/packages/modules/smarthome/viessmann/watt.py b/packages/modules/smarthome/viessmann/watt.py index e46ec14986..5d192050aa 100644 --- a/packages/modules/smarthome/viessmann/watt.py +++ b/packages/modules/smarthome/viessmann/watt.py @@ -1,24 +1,24 @@ #!/usr/bin/python3 + import sys import os -import time import json +import logging + +log = logging.getLogger(__name__) -named_tuple = time.localtime() # getstruct_time -time_string = time.strftime("%m/%d/%Y, %H:%M:%S viessmann watty.py", named_tuple) devicenumber = str(sys.argv[1]) ipadr = str(sys.argv[2]) uberschuss = int(sys.argv[3]) -file_stringpv = '/var/www/html/openWB/ramdisk/smarthome_device_' + str(devicenumber) + '_pv' +file_stringpv = f'/var/www/html/openWB/ramdisk/smarthome_device_{devicenumber}_pv' # PV-Modus pvmodus = 0 if os.path.isfile(file_stringpv): - f = open(file_stringpv, 'r') - pvmodus = int(f.read()) - f.close() + with open(file_stringpv, 'r') as f: + pvmodus = int(f.read()) aktpower = 0 powerc = 0 -answer = '{"power":' + str(aktpower) + ',"powerc":' + str(powerc) + ',"on":' + str(pvmodus) + '} ' -f1 = open('/var/www/html/openWB/ramdisk/smarthome_device_ret' + str(devicenumber), 'w') -json.dump(answer, f1) -f1.close() +answer = {"power": aktpower, "powerc": powerc, "on": pvmodus} +outfile = f'/var/www/html/openWB/ramdisk/smarthome_device_ret{devicenumber}' +with open(outfile, 'w') as f1: + json.dump(answer, f1) diff --git a/packages/modules/smarthome/we514/watt.py b/packages/modules/smarthome/we514/watt.py index 5a12363c25..f9d3afe00e 100644 --- a/packages/modules/smarthome/we514/watt.py +++ b/packages/modules/smarthome/we514/watt.py @@ -33,9 +33,8 @@ resp = client.read_holding_registers(CurrentPowerRegisterAddress, 1, unit=MODBUS_DEVICEID) CurrentPower = int(resp.registers[0]) -answer = '{"power":' + str(CurrentPower) + ',"powerc":' + str(TotalEnergy) + '} ' -f1 = open('/var/www/html/openWB/ramdisk/smarthome_device_ret' + str(devicenumber), 'w') -json.dump(answer, f1) -f1.close() +answer = {"power": CurrentPower, "powerc": TotalEnergy} +with open(f'/var/www/html/openWB/ramdisk/smarthome_device_ret{devicenumber}', 'w') as f1: + json.dump(answer, f1) client.close() # clean disconnect from modbus server