Skip to content

Commit

Permalink
Leistungsmessung über Lovato Zähler für Smarthome-
Browse files Browse the repository at this point in the history
Neu wird der Lovato Zähler als separate Leistungsmessung unterstützt.
Ebenso wurde auf static Types umgestellt.
  • Loading branch information
okaegi committed Dec 19, 2022
1 parent 07c816f commit 6ee2ef1
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 78 deletions.
24 changes: 24 additions & 0 deletions modules/smarthome/lovato/lovato.py
@@ -0,0 +1,24 @@
#!/usr/bin/python3
import sys
import struct
from smarthome.smartret import writeret
from pymodbus.client.sync import ModbusTcpClient
import codecs
devicenumber = int(sys.argv[1])
seradd = str(sys.argv[2])
sdmid = int(sys.argv[3])
port = int(sys.argv[4])
client = ModbusTcpClient(seradd, port=port)
# phasen watt
resp = client.read_input_registers(0x0013, 2, unit=sdmid)
all = format(resp.registers[0], '04x') + format(resp.registers[1], '04x')
finalw1 = int(struct.unpack('>i', codecs.decode(all, 'hex'))[0] / 100)
resp = client.read_input_registers(0x0015, 2, unit=sdmid)
all = format(resp.registers[0], '04x') + format(resp.registers[1], '04x')
finalw2 = int(struct.unpack('>i', codecs.decode(all, 'hex'))[0] / 100)
resp = client.read_input_registers(0x0017, 2, unit=sdmid)
all = format(resp.registers[0], '04x') + format(resp.registers[1], '04x')
finalw3 = int(struct.unpack('>i', codecs.decode(all, 'hex'))[0] / 100)
power = finalw1 + finalw2 + finalw3
answer = '{"power":' + str(power) + ',"powerc":0}'
writeret(answer, devicenumber)
2 changes: 1 addition & 1 deletion runs/mqttsub.py
Expand Up @@ -197,7 +197,7 @@ def on_message(client, userdata, msg):
client.publish("openWB/config/get/SmartHome/Devices/"+str(devicenumb)+"/device_type", msg.payload.decode("utf-8"), qos=0, retain=True)
if (( "openWB/config/set/SmartHome/Device" in msg.topic) and ("device_measureType" in msg.topic)):
devicenumb=re.sub(r'\D', '', msg.topic)
validDeviceMeasureTypes = ['shelly','tasmota','http','mystrom','sdm630','we514','fronius','json','avm','mqtt','sdm120','smaem'] # 'pyt' is deprecated and will be removed!
validDeviceMeasureTypes = ['shelly','tasmota','http','mystrom','sdm630','lovato','we514','fronius','json','avm','mqtt','sdm120','smaem'] # 'pyt' is deprecated and will be removed!
if ( 1 <= int(devicenumb) <= numberOfSupportedDevices and len(str(msg.payload.decode("utf-8"))) > 2):
try:
deviceMeasureTypeIndex = validDeviceMeasureTypes.index(msg.payload.decode("utf-8"))
Expand Down
68 changes: 36 additions & 32 deletions runs/usmarthome/smartbase.py
@@ -1,9 +1,10 @@
#!/usr/bin/python3
import time
import os
from typing import Dict, Tuple, Any
from usmarthome.global0 import log
from usmarthome.smartbase0 import Sbase0
from usmarthome.smartmeas import Slsdm630, Slsdm120, Slwe514, Slfronius
from usmarthome.smartmeas import Slsdm630, Sllovato, Slsdm120, Slwe514, Slfronius
from usmarthome.smartmeas import Sljson, Slsmaem, Slshelly, Sltasmota, Slmqtt
from usmarthome.smartmeas import Slhttp, Slavm, Slmystrom
from usmarthome.smartbut import Sbshelly
Expand All @@ -22,12 +23,12 @@ class Sbase(Sbase0):
nureinschaltinsec = 0
eindevstatus = 0

def __init__(self):
def __init__(self) -> None:
# setting
super().__init__()
print('__init__ Sbase executed')
self.mqtt_param = {}
self.mqtt_param_del = {}
self.mqtt_param = {} # type: Dict[str, str]
self.mqtt_param_del = {} # type: Dict[str, str]
self.device_name = 'none'
self.devstatus = 10
# (10 = ueberschuss gesteuert oder manual,
Expand All @@ -53,7 +54,7 @@ def __init__(self):
self.newdevice_manual = 0
self.newdevice_manual_control = 0
self.device_type = 'none'
self._smart_param = {}
self._smart_param = {} # type: Dict[str, str]
self._uberschussoffset = 0
self._uberschuss = 0
self.device_canswitch = 0
Expand Down Expand Up @@ -89,8 +90,8 @@ def __init__(self):
self._device_pbtype = 'none'
self._device_lambdaueb = 'UP'
self._old_pbtype = 'none'
self._mydevicepb = 'none'
self._oldrelais = '2'
self._mydevicepb = 'none' # type: Any
self._oldrelais = 2
self._oldwatt = 0
self._device_chan = 0
self._device_updatesec = 0
Expand Down Expand Up @@ -123,12 +124,13 @@ def __init__(self):
self.device_setauto = 0
self.gruppe = 'none'
self.btchange = 0
self._mydevicemeasure = 'none' # type: Any

def __del__(self):
def __del__(self) -> None:

print('__del__ Sbase executed ')

def prewatt(self, uberschuss, uberschussoffset):
def prewatt(self, uberschuss: int, uberschussoffset: int) -> None:
self._uberschuss = uberschuss
self._uberschussoffset = uberschussoffset
if (self._deviceconfiguredold == "9") and (self._deviceconfigured ==
Expand All @@ -154,7 +156,7 @@ def prewatt(self, uberschuss, uberschussoffset):
# prepare end
self.getueb()

def postwatt(self):
def postwatt(self) -> None:
(self.newwatt, self.newwattk) = self.sepwatt(self.newwatt,
self.newwattk)
# bei reiner Leistungsmessung relais nur nach Watt setzten
Expand All @@ -168,7 +170,7 @@ def postwatt(self):
self.relais = 0
self.mqtt_param = {}
pref = 'openWB/SmartHome/Devices/' + str(self.device_nummer) + '/'
self.mqtt_param[pref + 'RelayStatus'] = self.relais
self.mqtt_param[pref + 'RelayStatus'] = str(self.relais)
if (self.c_mantime_f == 'Y') and (self.device_manual != 1):
# nach Ausschalten manueller Modus mindestens 30 Sek +
# max( ausschaltverzögerung,mindeseinschaltdauer
Expand Down Expand Up @@ -226,7 +228,7 @@ def postwatt(self):
else:
self.c_oldstampeinschaltdauer = 0
self.c_oldstampeinschaltdauer_f = 'N'
self.mqtt_param[pref + 'RunningTimeToday'] = self.runningtime
self.mqtt_param[pref + 'RunningTimeToday'] = str(self.runningtime)
# Einschaltzeit des Relais setzen
if (self._oldrelais == 0):
if (self.relais == 1):
Expand All @@ -242,25 +244,25 @@ def postwatt(self):
self.mqtt_param[pref + 'TemperatureSensor1'] = self.temp1
if (self.device_temperatur_configured > 2):
self.mqtt_param[pref + 'TemperatureSensor2'] = self.temp2
self.mqtt_param[pref + 'Watt'] = self._oldwatt
self.mqtt_param[pref + 'Wh'] = self._wh
self.mqtt_param[pref + 'WHImported_temp'] = self._wpos
self.mqtt_param[pref + 'Watt'] = str(self._oldwatt)
self.mqtt_param[pref + 'Wh'] = str(self._wh)
self.mqtt_param[pref + 'WHImported_temp'] = str(self._wpos)
self.mqtt_param[pref + 'oncountnor'] = self.oncountnor
self.mqtt_param[pref + 'OnCntStandby'] = self.oncntstandby
# nur bei Status 10 on status mitnehmen
if (self.devstatus == 10):
sendstatus = self.relais + self.devstatus
else:
sendstatus = self.devstatus
self.mqtt_param[pref + 'Status'] = sendstatus
self.mqtt_param[pref + 'Status'] = str(sendstatus)
if (self.gruppe == 'A'):
Sbase.ausschaltwatt = Sbase.ausschaltwatt + self._oldwatt
elif (self.gruppe == 'E'):
if (self.relais == 1):
Sbase.einrelais = 1
Sbase.eindevstatus = max(Sbase.eindevstatus, self.devstatus)

def updatepar(self, input_param):
def updatepar(self, input_param: Dict[str, str]) -> None:
self._smart_param = input_param.copy()
self.device_nummer = int(self._smart_param.get('device_nummer', '0'))
for key, value in self._smart_param.items():
Expand Down Expand Up @@ -390,7 +392,7 @@ def updatepar(self, input_param):
" " + value)
else:
log.info("(" + str(self.device_nummer) + ") "
+ __class__.__name__ + " überlesen " + key +
+ "Sbase überlesen " + key +
" " + value)
self._first_run = 0
pref = 'openWB/SmartHome/Devices/' + str(self.device_nummer) + '/'
Expand Down Expand Up @@ -451,6 +453,8 @@ def updatepar(self, input_param):
del self._mydevicemeasure
if (self._device_measuretype == 'sdm630'):
self._mydevicemeasure = Slsdm630()
elif (self._device_measuretype == 'lovato'):
self._mydevicemeasure = Sllovato()
elif (self._device_measuretype == 'sdm120'):
self._mydevicemeasure = Slsdm120()
elif (self._device_measuretype == 'we514'):
Expand Down Expand Up @@ -494,14 +498,14 @@ def updatepar(self, input_param):
str(self.device_nummer), 'w') as f:
f.write(str(self.device_homeconsumtion))

def getueb(self):
def getueb(self) -> None:
# (1 = mit Speicher, 2 = mit offset , 0 = manual eingeschaltet)
if (self.ueberschussberechnung == 2):
self.devuberschuss = self._uberschussoffset
else:
self.devuberschuss = self._uberschuss

def preturn(self, zustand, ueberschussberechnung, updatecnt):
def preturn(self, zustand: int, ueberschussberechnung: int, updatecnt: int) -> None:
self.ueberschussberechnung = ueberschussberechnung
with open(self._basePath+'/ramdisk/device' + str(self.device_nummer) +
'_req_relais', 'w') as f:
Expand All @@ -519,7 +523,7 @@ def preturn(self, zustand, ueberschussberechnung, updatecnt):
self._c_eintime_f = 'Y'
self._c_eintime = int(time.time())

def sepwatt(self, newwatt, newwattk):
def sepwatt(self, newwatt: int, newwattk: int) -> Tuple[int, int]:
if (self._device_differentmeasurement == 0):
return newwatt, newwattk
# ueberschuss übertragen
Expand All @@ -529,7 +533,7 @@ def sepwatt(self, newwatt, newwattk):
self.newwattk = self._mydevicemeasure.newwattk
return self.newwatt, self.newwattk

def conditions(self, speichersoc):
def conditions(self, speichersoc: int) -> None:
# do not do anything in case none type or can switch = no
# or device manuam mode
if ((self.device_canswitch == 0) or
Expand All @@ -550,8 +554,8 @@ def conditions(self, speichersoc):
work_ausschaltschwelle = self._device_ausschaltschwelle
work_ausschaltverzoegerung = self._device_ausschaltverzoegerung
local_time = datetime.now(timezone.utc).astimezone()
localhour = int(local_time.strftime(format="%H"))
localminute = int(local_time.strftime(format="%M"))
localhour = int(local_time.strftime("%H"))
localminute = int(local_time.strftime("%M"))
localinsec = int((localhour * 60 * 60) + (localminute * 60))
if (localinsec < Sbase.nureinschaltinsec) and (Sbase.eindevices > 0):
log.info("(" + str(self.device_nummer) + ") " +
Expand Down Expand Up @@ -966,8 +970,8 @@ def conditions(self, speichersoc):
self._c_ausverz_f = 'N'
# rechne Zeit exclusive einschaltgruppe
local_time = datetime.now(timezone.utc).astimezone()
localh = int(local_time.strftime(format="%H"))
localminute = int(local_time.strftime(format="%M"))
localh = int(local_time.strftime("%H"))
localminute = int(local_time.strftime("%M"))
localinsec = int((localh * 60 * 60) + (localminute * 60))
Sbase.nureinschaltinsec = localinsec + Sbase.einverz
return
Expand Down Expand Up @@ -1132,7 +1136,7 @@ def conditions(self, speichersoc):
self._c_einverz_f = 'N'
self._c_ausverz_f = 'N'

def simcount(self, watt2, pref, importfn, exportfn, nummer, wattks):
def simcount(self, watt2: int, pref: str, importfn: str, exportfn: str, nummer: str, wattks: int) -> None:
# Zaehler mitgeliefert in WH , zurueckrechnen fuer simcount
if wattks > 0:
wattposkh = wattks
Expand Down Expand Up @@ -1191,8 +1195,8 @@ def simcount(self, watt2, pref, importfn, exportfn, nummer, wattks):
wattnegh = wattnegh + watt1
else:
wattposh = wattposh + watt1
wattposkh = wattposh/3600
wattnegkh = (wattnegh*-1)/3600
wattposkh = int(wattposh/3600)
wattnegkh = int((wattnegh*-1)/3600)
with open(self._basePath+'/ramdisk/'+pref+'watt0pos', 'w') as f:
f.write(str(wattposh))
self._wpos = wattposh
Expand All @@ -1210,17 +1214,17 @@ def simcount(self, watt2, pref, importfn, exportfn, nummer, wattks):
with open(self._basePath+'/ramdisk/'+pref+'wh0', 'w') as f:
f.write(str(watt2))

def getwatt(self, uberschuss, uberschussoffset):
def getwatt(self, uberschuss: int, uberschussoffset: int) -> None:
self.prewatt(uberschuss, uberschussoffset)
self.newwatt = 0
self.newwattk = 0
self.relais = 0
self.postwatt()

def turndevicerelais(self, zustand, ueberschussberechnung, updatecnt):
def turndevicerelais(self, zustand: int, ueberschussberechnung: int, updatecnt: int) -> None:
pass

def updatebutton(self):
def updatebutton(self) -> None:
self.newdevice_manual = self.device_manual
self.newdevice_manual_control = self.device_manual_control
self.btchange = 0
Expand Down

0 comments on commit 6ee2ef1

Please sign in to comment.