-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.py
478 lines (436 loc) · 18.9 KB
/
plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# Integración regulador solar solax en domoticz
#
# Author: @ea4gkq 2020
#
# 07/10/2021
# - Corregido pico negativo cuando el inversor deja de generar AC
# 28/05/2020
# - Añadido dummy con valor medio de Grid de las últimas 30 muestras
# 22/05/2020
# - Mejorada reconexión automática
# 19/05/2020
# - Arreglado error cuando V es 0, error división por cero
# Integración regulador solar solax en domoticz
#
"""
<plugin key="SolaxHTTP" name="Solax HTTP" author="EA4GKQ" version="1.0.7" wikilink="https://github.com/ayasystems/SolaxHTTP" externallink="https://www.solaxpower.com/x1-boost/">
<description>
<h2>Solax HTTP Pluging</h2><br/>
<h3>by @ea4gkq</h3>
<br/>
<a href="https://domotuto.com/integracion-del-inversor-solar-solax-boost-en-domoticz/">https://domotuto.com/integracion-del-inversor-solar-solax-boost-en-domoticz/</a>
<br/>
Para la versión V2 necesitarás conectar tu Raspberry a la red que publica el inversor Solax_XXXXXX
</description>
<params>
<param field="Address" label="Solax IP" width="200px" required="true" default="5.8.8.8"/>
<param field="Mode1" label="Protocol" width="75px">
<options>
<option label="HTTP" value="80" default="true" />
</options>
</param>
<param field="Mode2" label="Version" width="75px">
<options>
<option label="V1" value="V1" default="true" />
<option label="V2" value="V2" default="true" />
</options>
</param>
<param field="Mode3" label="Update speed" width="75px">
<options>
<option label="Normal" value="Normal"/>
<option label="High" value="High" default="true" />
</options>
</param>
<param field="Mode6" label="Debug" width="150px">
<options>
<option label="None" value="0" default="true" />
<option label="Python Only" value="2"/>
<option label="Basic Debugging" value="62"/>
<option label="Basic+Messages" value="126"/>
<option label="Connections Only" value="16"/>
<option label="Connections+Queue" value="144"/>
<option label="All" value="-1"/>
</options>
</param>
</params>
</plugin>
"""
errmsg = ""
try:
import Domoticz
except Exception as e:
errmsg += "Domoticz core start error: "+str(e)
try:
import json
except Exception as e:
errmsg += " Json import error: "+str(e)
try:
import time
except Exception as e:
errmsg += " time import error: "+str(e)
try:
import re
except Exception as e:
errmsg += " re import error: "+str(e)
from random import seed
from random import gauss
from functools import reduce
class SolaxHTTP:
httpConn = None
interval = 2
runAgain = interval
disconnectCount = 0
connectedCount = 0
sProtocol = "HTTP"
VERSION = "V2"
S1_CURRENT = ""
S2_CURRENT = ""
S1_VOLTAGE = ""
S2_VOLTAGE = ""
S1_POWER = ""
S2_POWER = ""
TO_GRID = ""
FROM_GRID = ""
TEMP = ""
FREQUENCY = ""
GRID_W = ""
ERROR_LEVEL = ""
listGrid = []
maxGridList = 30
avgGrid = 0
def __init__(self):
return
def onStart(self):
Domoticz.Error("onStart: "+errmsg)
if errmsg =="":
createDevices(self,"S1_CURRENT")
createDevices(self,"S2_CURRENT")
createDevices(self,"S1_VOLTAGE")
createDevices(self,"S2_VOLTAGE")
createDevices(self,"S1_POWER")
createDevices(self,"S2_POWER")
createDevices(self,"TO_GRID")
createDevices(self,"FROM_GRID")
createDevices(self,"FV_POWER")
createDevices(self,"TEMP")
createDevices(self,"FREQUENCY")
createDevices(self,"AVGGRID")
createDevices(self,"GRID_CURRENT")
try:
if Parameters["Mode6"] == "": Parameters["Mode6"] = "-1"
if Parameters["Mode6"] != "0":
Domoticz.Error("if parameters mode 6: "+Parameters["Mode6"])
Domoticz.Debugging(int(Parameters["Mode6"]))
Domoticz.Error("DumpConfigToLog")
DumpConfigToLog()
self.ERROR_LEVEL = Parameters["Mode6"]
if (Parameters["Mode1"].strip() == "443"): self.sProtocol = "HTTPS"
if (Parameters["Mode2"].strip() == "V1"): self.VERSION = "V1"
if (Parameters["Mode3"].strip() == "High"): Domoticz.Heartbeat(1)
self.httpConn = Domoticz.Connection(Name=self.sProtocol+" Test", Transport="TCP/IP", Protocol=self.sProtocol, Address=Parameters["Address"].strip() , Port=Parameters["Mode1"].strip() )
self.httpConn.Connect()
except Exception as e:
Domoticz.Error("Plugin onStart error: "+str(e))
else:
Domoticz.Error("Your Domoticz Python environment is not functional! "+errmsg)
def onStop(self):
Domoticz.Log("onStop - Plugin is stopping.")
def Average(self):
if(len(self.listGrid)>self.maxGridList):
self.listGrid.pop(0)
Domoticz.Debug("List values: "+str(self.listGrid))
self.avgGrid = reduce(lambda a,b: a + b, self.listGrid) / len(self.listGrid)
self.avgGrid = round(self.avgGrid,0)
def onConnect(self, Connection, Status, Description):
if (Status == 0):
Domoticz.Debug("Solax connected successfully.")
if(self.VERSION=="V2"):
sendData = { 'Verb' : 'POST',
'URL' : '/?optType=ReadRealTimeData',
'Headers' : { 'Content-Type': 'text/xml; charset=utf-8', \
'Connection': 'keep-alive', \
'Accept': 'Content-Type: text/html; charset=UTF-8', \
'Host': Parameters["Address"]+":"+Parameters["Mode1"], \
'User-Agent':'Domoticz/1.0' }
}
else:
sendData = { 'Verb' : 'GET',
'URL' : '/api/realTimeData.htm',
'Headers' : { 'Content-Type': 'text/xml; charset=utf-8', \
'Connection': 'keep-alive', \
'Accept': 'Content-Type: text/html; charset=UTF-8', \
'Host': Parameters["Address"]+":"+Parameters["Mode1"], \
'User-Agent':'Domoticz/1.0' }
}
Connection.Send(sendData)
else:
Domoticz.Error("Failed to connect ("+str(Status)+") to: "+Parameters["Address"]+":"+Parameters["Mode1"]+" with error: "+Description)
self.httpConn = None
def onMessage(self, Connection, Data):
DumpHTTPResponseToLog(Data)
try:
strData = Data["Data"].decode("utf-8", "ignore")
Status = int(Data["Status"])
LogMessage(strData)
Domoticz.Debug("Solax returned a status: "+str(Status))
except Exception as e:
Domoticz.Error("HTTP RESPONSE ERROR")
self.httpConn.Disconnect()
self.httpConn = None
return False
if (Status == 200):
if ((self.disconnectCount & 1) == 1):
Domoticz.Log("Good Response received from Solax, Disconnecting.")
self.httpConn.Disconnect()
else:
Domoticz.Log("Good Response received from Solax, Dropping connection.")
self.httpConn = None
self.disconnectCount = self.disconnectCount + 1
processResponse(self,Data)
elif (Status == 302):
Domoticz.Log("Solax returned a Page Moved Error.")
sendData = { 'Verb' : 'POST',
'URL' : Data["Headers"]["Location"],
'Headers' : { 'Content-Type': 'text/xml; charset=utf-8', \
'Connection': 'keep-alive', \
'Accept': 'Content-Type: text/html; charset=UTF-8', \
'Host': Parameters["Address"]+":"+Parameters["Mode1"], \
'User-Agent':'Domoticz/1.0' },
}
#Connection.Send(sendData)
elif (Status == 400):
Domoticz.Error("Solax returned a Bad Request Error.")
self.httpConn.Disconnect()
self.httpConn = None
elif (Status == 500):
Domoticz.Error("Solax returned a Server Error.")
self.httpConn.Disconnect()
self.httpConn = None
else:
Domoticz.Debug("Solax returned a status: "+str(Status))
def onCommand(self, Unit, Command, Level, Hue):
Domoticz.Debug("onCommand called for Unit " + str(Unit) + ": Parameter '" + str(Command) + "', Level: " + str(Level))
def onDisconnect(self, Connection):
Domoticz.Log("onDisconnect called for connection to: "+Connection.Address+":"+Connection.Port)
def onHeartbeat(self):
#Domoticz.Trace(True)
if(self.connectedCount>10):
self.connectedCount = 0
self.httpConn.Disconnect()
self.httpConn = None
if (self.httpConn != None and (self.httpConn.Connecting() or self.httpConn.Connected())):
Domoticz.Debug("self.connectedCount --> "+str(self.connectedCount))
Domoticz.Debug("self.httpConn.Connected() --> "+str(self.httpConn.Connected()))
if(self.httpConn.Connected()):
Domoticz.Debug("self.connectedCount = self.connectedCount +1 "+str(self.connectedCount))
self.connectedCount = self.connectedCount +1
Domoticz.Debug("self.httpConn.Connecting() --> "+str(self.httpConn.Connecting()))
Domoticz.Debug("onHeartbeat called, Connection is alive.")
else:
self.runAgain = self.runAgain - 1
if self.runAgain <= 0:
Domoticz.Debug("self.runAgain | self.httpConn -> "+str(self.httpConn))
if (self.httpConn == None):
self.httpConn = Domoticz.Connection(Name=self.sProtocol+" Test", Transport="TCP/IP", Protocol=self.sProtocol, Address=Parameters["Address"], Port=Parameters["Mode1"])
self.httpConn.Connect()
self.runAgain = self.interval
else:
Domoticz.Debug("onHeartbeat called, run again in "+str(self.runAgain)+" heartbeats.")
#Domoticz.Trace(False)
global _plugin
_plugin = SolaxHTTP()
def onStart():
global _plugin
_plugin.onStart()
def onStop():
global _plugin
_plugin.onStop()
def onConnect(Connection, Status, Description):
global _plugin
_plugin.onConnect(Connection, Status, Description)
def onMessage(Connection, Data):
global _plugin
_plugin.onMessage(Connection, Data)
def onCommand(Unit, Command, Level, Hue):
global _plugin
_plugin.onCommand(Unit, Command, Level, Hue)
def onNotification(Name, Subject, Text, Status, Priority, Sound, ImageFile):
global _plugin
_plugin.onNotification(Name, Subject, Text, Status, Priority, Sound, ImageFile)
def onDisconnect(Connection):
global _plugin
_plugin.onDisconnect(Connection)
def onHeartbeat():
global _plugin
_plugin.onHeartbeat()
# Generic helper functions
def LogMessage(Message):
if Parameters["Mode6"] == "File":
f = open(Parameters["HomeFolder"]+"http.html","w")
f.write(Message)
f.close()
Domoticz.Log("File written")
def DumpConfigToLog():
for x in Parameters:
if Parameters[x] != "":
Domoticz.Debug( "'" + x + "':'" + str(Parameters[x]) + "'")
Domoticz.Debug("Device count: " + str(len(Devices)))
for x in Devices:
Domoticz.Debug("Device: " + str(x) + " - " + str(Devices[x]))
Domoticz.Debug("Device ID: '" + str(Devices[x].ID) + "'")
Domoticz.Debug("Device Name: '" + Devices[x].Name + "'")
Domoticz.Debug("Device nValue: " + str(Devices[x].nValue))
Domoticz.Debug("Device sValue: '" + Devices[x].sValue + "'")
Domoticz.Debug("Device LastLevel: " + str(Devices[x].LastLevel))
return
def createDevices(self,unitname):
OptionsSensor = {"Custom": "1;Hz"}
OptionsSensorAVG = {"Custom": "1;w"}
iUnit = -1
for Device in Devices:
try:
if (Devices[Device].DeviceID.strip() == unitname):
iUnit = Device
break
except:
pass
if iUnit<0: # if device does not exists in Domoticz, than create it
try:
iUnit = 0
for x in range(1,256):
if x not in Devices:
iUnit=x
break
Domoticz.Debug("Creating: "+unitname);
if iUnit==0:
iUnit=len(Devices)+1
if(unitname=="S1_CURRENT"):
Domoticz.Device(Name=unitname, Unit=iUnit,TypeName="Current (Single)",Used=1,DeviceID=unitname).Create()
if(unitname=="S2_CURRENT"):
Domoticz.Device(Name=unitname, Unit=iUnit,TypeName="Current (Single)",Used=1,DeviceID=unitname).Create()
if(unitname=="S1_VOLTAGE"):
Domoticz.Device(Name=unitname, Unit=iUnit,TypeName="Voltage",Used=1,DeviceID=unitname).Create()
if(unitname=="S2_VOLTAGE"):
Domoticz.Device(Name=unitname, Unit=iUnit,TypeName="Voltage",Used=1,DeviceID=unitname).Create()
if(unitname=="FROM_GRID"):
Domoticz.Device(Name=unitname, Unit=iUnit,Type=248,Subtype=1,Used=1,DeviceID=unitname).Create()
if(unitname=="TO_GRID"):
Domoticz.Device(Name=unitname, Unit=iUnit,Type=248,Subtype=1,Used=1,DeviceID=unitname).Create()
if(unitname=="S1_POWER"):
Domoticz.Device(Name=unitname, Unit=iUnit,Type=248,Subtype=1,Used=1,DeviceID=unitname).Create()
if(unitname=="S2_POWER"):
Domoticz.Device(Name=unitname, Unit=iUnit,Type=248,Subtype=1,Used=1,DeviceID=unitname).Create()
if(unitname=="FV_POWER"):
Domoticz.Device(Name=unitname, Unit=iUnit,Type=243,Subtype=29,Switchtype=0,Used=1,DeviceID=unitname).Create()
if(unitname=="TEMP"):
Domoticz.Device(Name=unitname, Unit=iUnit,TypeName="Temperature",Used=1,DeviceID=unitname).Create()
if(unitname=="FREQUENCY"):
Domoticz.Device(Name=unitname, Unit=iUnit,TypeName='Custom',Options=OptionsSensor,Used=1,DeviceID=unitname).Create()
if(unitname=="AVGGRID"):
Domoticz.Device(Name=unitname, Unit=iUnit,TypeName='Custom',Options=OptionsSensorAVG,Used=1,DeviceID=unitname).Create()
if(unitname=="GRID_CURRENT"):
Domoticz.Device(Name=unitname, Unit=iUnit,TypeName="Current (Single)",Used=1,DeviceID=unitname).Create()
except Exception as e:
Domoticz.Debug(str(e))
return False
return
def processResponse(self,httpResp):
#DAY_ENERGY = ""
#PAC = ""
#TOTAL_ENERGY = ""
stringData = httpResp["Data"].decode("utf-8", "ignore")
#Domoticz.Error(stringData)
if(self.VERSION=="V1"):
stringData = stringData.replace(',,',',null,').replace(',,',',null,')
try:
json_object = json.loads(stringData)
except Exception as e:
Domoticz.Error("Error json parse: "+str(e))
if(self.ERROR_LEVEL=="-1"):
Domoticz.Error(str(stringData))
return
self.S1_CURRENT = str(json_object['Data'][0])
self.S2_CURRENT = str(json_object['Data'][1])
self.S1_VOLTAGE = str(json_object['Data'][2])
self.S2_VOLTAGE = str(json_object['Data'][3])
self.S1_POWER = str(json_object['Data'][11])
self.S2_POWER = str(json_object['Data'][12])
self.TO_GRID = json_object['Data'][10]
if(self.TO_GRID<0):
self.TO_GRID = 0
self.FROM_GRID = json_object['Data'][10]*-1
if(self.FROM_GRID<0):
self.FROM_GRID = 0
self.TO_GRID = str(self.TO_GRID)
self.FROM_GRID = str(self.FROM_GRID)
self.listGrid.append(json_object['Data'][10]*-1)
Domoticz.Debug("List: "+str(self.listGrid))
self.Average()
Domoticz.Debug("AVG Grid: "+str(round(self.avgGrid,0)))
if(json_object['Data'][5]>0):
self.GRID_CURRENT = json_object['Data'][10] / json_object['Data'][5]
else:
self.GRID_CURRENT = 0
self.GRID_CURRENT = round(self.GRID_CURRENT * -1 ,2)
kwhdiario = json_object['Data'][8]*1000
acumuladoKwh = json_object['Data'][6]
self.TEMP = str(json_object['Data'][7])
self.FREQUENCY = str(json_object['Data'][50])
UpdateDevice("S1_CURRENT", 0, self.S1_CURRENT)
UpdateDevice("S2_CURRENT", 0, self.S2_CURRENT)
UpdateDevice("S1_VOLTAGE", 0, self.S1_VOLTAGE)
UpdateDevice("S2_VOLTAGE", 0, self.S2_VOLTAGE)
UpdateDevice("S1_POWER", 0, self.S1_POWER)
UpdateDevice("S2_POWER", 0, self.S2_POWER)
UpdateDevice("TO_GRID", 0, self.TO_GRID)
UpdateDevice("FROM_GRID", 0, self.FROM_GRID)
if(acumuladoKwh>0 AND kwhdiario > 0):
UpdateDevice("FV_POWER", 0, acumuladoKwh+";"+kwhdiario)
UpdateDevice("TEMP", 0, self.TEMP)
UpdateDevice("FREQUENCY", 0, self.FREQUENCY)
UpdateDevice("AVGGRID", 0, self.avgGrid)
UpdateDevice("GRID_CURRENT", 0, self.GRID_CURRENT)
self.connectedCount = 0
if(self.ERROR_LEVEL=="-1"):
Domoticz.Error(json.dumps(json_object))
def debugDevices(self,device):
value = ""
try:
value = getattr(self, device)
except AttributeError:
Domoticz.Error("Objeto "+device+" no existe el _self")
#print 'No %s field' % field
return
Domoticz.Debug(device+": "+value)
def DumpHTTPResponseToLog(httpResp, level=0):
if (level==0): Domoticz.Debug("HTTP Details ("+str(len(httpResp))+"):")
indentStr = ""
for x in range(level):
indentStr += "----"
if isinstance(httpResp, dict):
for x in httpResp:
if not isinstance(httpResp[x], dict) and not isinstance(httpResp[x], list):
Domoticz.Debug(indentStr + "a>'" + x + "':'" + str(httpResp[x]) + "'")
else:
Domoticz.Debug(indentStr + "b>'" + x + "':")
DumpHTTPResponseToLog(httpResp[x], level+1)
elif isinstance(httpResp, list):
for x in httpResp:
Domoticz.Debug(indentStr + "['" + x + "']")
else:
Domoticz.Debug(indentStr + "c>'" + x + "':'" + str(httpResp[x]) + "'")
def UpdateDevice(unitname, nValue, sValue):
# Make sure that the Domoticz device still exists (they can be deleted) before updating it
# if (Devices[Device].DeviceID.strip() == unitname):
iUnit = -1
for Device in Devices:
try:
if (Devices[Device].DeviceID.strip() == unitname):
iUnit = Device
break
except:
pass
if iUnit>=0: # existe, actualizamos
if (Devices[iUnit].nValue != nValue) or (Devices[iUnit].sValue != sValue):
Devices[iUnit].Update(nValue=nValue, sValue=str(sValue))
Domoticz.Debug("Update "+str(nValue)+":'"+str(sValue)+"' ("+Devices[iUnit].Name+")")