forked from dylian94/domoticz-GoodWeSEMS
-
Notifications
You must be signed in to change notification settings - Fork 4
/
plugin.py
732 lines (666 loc) · 43.7 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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
# Copyright 2021 Jan-Jaap Kostelijk
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is furnished
# to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
<plugin key="GoodWeSEMS" name="GoodWe solar inverter via SEMS API" version="4.0.2" author="Jan-Jaap Kostelijk">
<description>
<h2>GoodWe inverter (via SEMS portal)</h2>
<p>This plugin uses the GoodWe SEMS api to retrieve the status information of your GoodWe inverter.</p>
<p>Version: 4.0.1</p>
<p>Important upgrade note: <a href="https://github.com/JanJaapKo/domoticz-GoodWeSEMS/wiki">plugin wiki</a></p>
<h3>Configuration</h3>
<ul>
<li>Register your inverter at GoodWe SEMS portal (if not done already): <a href="https://www.semsportal.com">https://www.semsportal.com</a></li>
<li>Choose one of the following options:</li>
<ol type="a">
<li>You have to add one specific station to Domoticz follow the following steps:</li>
<ol>
<li>Login to your account on: <a href="https://www.semsportal.com">www.semsportal.com</a></li>
<li>Go to the plant status page for the station you want to add to Domoticz</li>
<li>Get the station ID from the URL, this is the sequence of characters after: https://www.semsportal.com/PowerStation/PowerStatusSnMin/, in the pattern:
(8 char)-(4 char)-(4 char)-(4 char)-(12 char), also known as a UUID </li>
<li>Add the power station ID to the hardware configuration (mandatory)</li>
</ol>
<li>Not possible at this moment: If you want all of your stations added to Domoticz you only have to enter your login information below</li>
</ol>
</ul>
</description>
<params>
<param field="Address" label="SEMS Server" width="100px" required="true">
<options>
<option label="Europe" value="eu.semsportal.com"/>
<option label="Australia" value="au.semsportal.com"/>
<option label="Global" value="www.semsportal.com" default="true"/>
</options>
</param>
<param field="Port" label="SEMS API Port" width="30px" required="true" default="443"/>
<param field="Username" label="E-Mail address" width="300px" required="true"/>
<param field="Password" label="Password" width="100px" required="true" password="true"/>
<param field="Mode1" label="Power Station ID (mandatory)" width="300px"/>
<param field="Mode2" label="Refresh interval" width="75px">
<options>
<option label="10s" value="1"/>
<option label="30s" value="3"/>
<option label="1m" value="6"/>
<option label="5m" value="30" default="true"/>
<option label="10m" value="60"/>
<option label="15m" value="90"/>
<option label="30m" value="180"/>
</options>
</param>
<param field="Mode3" label="Peak power [W]" width="300px">
<description>Optional: Peak power, supply values (separated by ';'): total power; power per string</description>
</param>
<param field="Mode6" label="Log level" width="75px">
<options>
<option label="Verbose" value="Verbose"/>
<option label="Debug" value="Debug"/>
<option label="Normal" value="Normal" default="true"/>
</options>
</param>
</params>
</plugin>
"""
import json
#import Domoticz
import DomoticzEx as Domoticz
import sys, time
from datetime import datetime, timedelta
from GoodWe import GoodWe
from GoodWe import PowerStation
import exceptions
import logging
class GoodWeSEMSPlugin:
httpConn = None
runAgain = 6
devicesUpdated = False
goodWeAccount = None
logger = None
baseDeviceIndex = 0
maxDeviceIndex = 0
def __init__(self):
startNum = 0
self.inverterTemperatureUnit = 1 + startNum
self.inverterStateUnit = 9 + startNum
self.outputCurrentUnit = 2 + startNum
self.outputVoltageUnit = 3 + startNum
self.outputPowerUnit = 4 + startNum
self.inputVoltage1Unit = 5 + startNum
self.inputAmps1Unit = 6 + startNum
self.inputPower1Unit = 14 + startNum
self.inputPower2Unit = 15 + startNum
self.inputPower3Unit = 16 + startNum
self.inputPower4Unit = 17 + startNum
self.inputVoltage2Unit = 7 + startNum
self.inputVoltage3Unit = 10 + startNum
self.inputVoltage4Unit = 12 + startNum
self.inputAmps2Unit = 8 + startNum
self.inputAmps3Unit = 11 + startNum
self.inputAmps4Unit = 13 + startNum
self.outputFreq1Unit = 18 + startNum
self.enabled = False
return
def apiConnection(self):
if Parameters["Port"] == "443":
return Domoticz.Connection(Name="SEMS Portal API", Transport="TCP/IP", Protocol="HTTPS",
Address=Parameters["Address"], Port=Parameters["Port"])
else:
return Domoticz.Connection(Name="SEMS Portal API", Transport="TCP/IP", Protocol="HTTP",
Address=Parameters["Address"], Port=Parameters["Port"])
def startDeviceUpdateV2(self):
logging.debug("startDeviceUpdate, token availability: '" + str(self.goodWeAccount.tokenAvailable)+ "'")
if not self.goodWeAccount.tokenAvailable:
self.goodWeAccount.powerStationList = {}
self.goodWeAccount.powerStationIndex = 0
self.devicesUpdated = False
try:
self.goodWeAccount.tokenRequest()
except (exceptions.GoodweException, exceptions.FailureWithMessage, exceptions.FailureWithoutMessage) as exp:
logging.error("Failed to request data: " + str(exp))
Domoticz.Error("Failed to request data: " + str(exp))
return
if self.goodWeAccount.tokenAvailable:
try:
DeviceData = self.goodWeAccount.stationDataRequestV2(Parameters["Mode1"])
except (exceptions.TooManyRetries, exceptions.FailureWithErrorCode, exceptions.FailureWithoutErrorCode) as exp:
logging.error("Failed to request data: " + str(exp))
Domoticz.Error("Failed to request data: " + str(exp))
return
self.goodWeAccount.createStationV2(DeviceData)
self.updateDevices(DeviceData)
def updateDevices(self, apiData):
theStation = self.goodWeAccount.powerStationList[1]
for inverter in apiData["inverter"]:
logging.debug("inverter found with SN: '" + inverter["sn"] + "'")
if inverter["sn"] in theStation.inverters:
#theStation.inverters[inverter["sn"]].createDevices(Devices)
self.createDevices(inverter["sn"])
theInverter = theStation.inverters[inverter["sn"]]
if len(inverter['fault_message']) > 0:
Domoticz.Log("Fault message from GoodWe inverter (SN: " + inverter["sn"] + "): '" + str(inverter['fault_message']) + "'")
logging.info("Fault message from GoodWe inverter (SN: " + inverter["sn"] + "): '" + str(inverter['fault_message']) + "'")
Domoticz.Log("Status of GoodWe inverter (SN: " + inverter["sn"] + "): '" + str(inverter["status"]) + ' ' + self.goodWeAccount.INVERTER_STATE[inverter["status"]] + "'")
logging.info("Status of GoodWe inverter (SN: " + inverter["sn"] + "): '" + str(inverter["status"]) + ' ' + self.goodWeAccount.INVERTER_STATE[inverter["status"]] + "'")
UpdateDevice(inverter["sn"], theInverter.inverterStateUnit, inverter["status"]+1, str((inverter["status"]+2)*10), AlwaysUpdate=True)
#Devices[inverter["sn"]].Unit[theInverter.inverterStateUnit].Update(nValue=inverter["status"]+1, sValue=str((inverter["status"]+2)*10))
if self.goodWeAccount.INVERTER_STATE[inverter["status"]] == 'generating':
logging.debug("inverter generating, log temp")
UpdateDevice(inverter["sn"],theInverter.inverterTemperatureUnit, 0, str(inverter["tempperature"]))
UpdateDevice(inverter["sn"],theInverter.outputFreq1Unit, 0, str(inverter["d"]["fac1"]))
UpdateDevice(inverter["sn"], theInverter.outputCurrentUnit, 0, str(inverter["output_current"]), AlwaysUpdate=True)
UpdateDevice(inverter["sn"], theInverter.outputVoltageUnit, 0, str(inverter["output_voltage"]), AlwaysUpdate=True)
UpdateDevice(inverter["sn"], theInverter.outputPowerUnit, 0, str(inverter["output_power"]) + ";" + str(inverter["etotal"] * 1000), AlwaysUpdate=True)
inputVoltage,inputAmps = inverter["pv_input_1"].split('/')
inputPower = float(inputVoltage[:-1]) * float(inputAmps[:-1]) #calculate the power based on P = I * V in Watt
UpdateDevice(inverter["sn"], theInverter.inputVoltage1Unit, 0, inputVoltage, AlwaysUpdate=True)
UpdateDevice(inverter["sn"], theInverter.inputAmps1Unit, 0, inputAmps, AlwaysUpdate=True)
newCounter = calculateNewEnergy(inverter["sn"], theInverter.inputPower1Unit, inputPower)
UpdateDevice(inverter["sn"],theInverter.inputPower1Unit, 0, "{:5.1f};{:10.2f}".format(inputPower, newCounter), AlwaysUpdate=True)
if "pv_input_2" in inverter:
logging.debug("Second string found")
inputVoltage,inputAmps = inverter["pv_input_2"].split('/')
UpdateDevice(inverter["sn"],theInverter.inputVoltage2Unit, 0, inputVoltage, AlwaysUpdate=True)
UpdateDevice(inverter["sn"],theInverter.inputAmps2Unit, 0, inputAmps, AlwaysUpdate=True)
inputPower = (float(inputVoltage[:-1])) * (float(inputAmps[:-1]))
newCounter = calculateNewEnergy(inverter["sn"], theInverter.inputPower2Unit, inputPower)
UpdateDevice(inverter["sn"],theInverter.inputPower2Unit, 0, "{:5.1f};{:10.2f}".format(inputPower, newCounter), AlwaysUpdate=True)
if "pv_input_3" in inverter:
logging.debug("Third string found")
inputVoltage,inputAmps = inverter["pv_input_3"].split('/')
UpdateDevice(inverter["sn"],theInverter.inputVoltage3Unit, 0, inputVoltage, AlwaysUpdate=True)
UpdateDevice(inverter["sn"],theInverter.inputAmps3Unit, 0, inputAmps, AlwaysUpdate=True)
inputPower = float(inputVoltage[:-1]) * float(inputAmps[:-1])
newCounter = calculateNewEnergy(inverter["sn"], theInverter.inputPower3Unit, inputPower)
UpdateDevice(inverter["sn"],theInverter.inputPower3Unit, 0, "{:5.1f};{:10.2f}".format(inputPower, newCounter), AlwaysUpdate=True)
if "pv_input_4" in inverter:
logging.debug("Fourth string found")
inputVoltage,inputAmps = inverter["pv_input_4"].split('/')
UpdateDevice(inverter["sn"],theInverter.inputVoltage4Unit, 0, inputVoltage, AlwaysUpdate=True)
UpdateDevice(inverter["sn"],theInverter.inputAmps4Unit, 0, inputAmps, AlwaysUpdate=True)
inputPower = float(inputVoltage[:-1]) * float(inputAmps[:-1])
newCounter = calculateNewEnergy(inverter["sn"], theInverter.inputPower4Unit, inputPower)
UpdateDevice(inverter["sn"],theInverter.inputPower4Unit, 0, "{:5.1f};{:10.2f}".format(inputPower, newCounter), AlwaysUpdate=True)
#log data of battery
Domoticz.Debug("Battery values: battery: '{0}', bms_status: '{1}', battery_power: '{2}'".format(inverter["battery"],inverter["bms_status"],inverter["battery_power"]))
logging.debug("Battery values: battery: '{0}', bms_status: '{1}', battery_power: '{2}'".format(inverter["battery"],inverter["bms_status"],inverter["battery_power"]))
def createDevices(self, serialNumber):
#create domoticz devices
logging.info("creating units for device with serial number: "+ serialNumber)
thisDevice = Domoticz.Device(DeviceID=serialNumber) #use serial number as identifier for Domoticz.Device instance
#numDevs = len(Devices[serialNumber].Units)
if serialNumber not in Devices or self.inverterTemperatureUnit not in Devices[serialNumber].Units:
Domoticz.Unit(Name="Inverter temperature (SN: " + serialNumber + ")", DeviceID=serialNumber,
Unit=(self.inverterTemperatureUnit), Type=80, Subtype=5).Create()
#if self.outputCurrentUnit not in Devices:
if self.outputCurrentUnit not in Devices[serialNumber].Units:
Domoticz.Unit(Name="Inverter output current (SN: " + serialNumber + ")", DeviceID=serialNumber,
Unit=(self.outputCurrentUnit), Type=243, Subtype=23).Create()
if self.outputVoltageUnit not in Devices[serialNumber].Units:
Domoticz.Unit(Name="Inverter output voltage (SN: " + serialNumber + ")", DeviceID=serialNumber,
Unit=(self.outputVoltageUnit), Type=243, Subtype=8).Create()
if self.outputPowerUnit not in Devices[serialNumber].Units:
Domoticz.Unit(Name="Inverter output power (SN: " + serialNumber + ")", DeviceID=serialNumber,
Unit=(self.outputPowerUnit), Type=243, Subtype=29,
Switchtype=4, Used=1).Create()
if self.inverterStateUnit not in Devices[serialNumber].Units:
Options = {"LevelActions": "|||",
"LevelNames": "|Offline|Waiting|Generating|Error",
"LevelOffHidden": "true",
"SelectorStyle": "1"}
Domoticz.Unit(Name="Inverter state (SN: " + serialNumber + ")",
Unit=(self.inverterStateUnit), TypeName="Selector Switch", Image=1,
Options=Options, Used=1, DeviceID=serialNumber).Create()
if self.inputVoltage1Unit not in Devices[serialNumber].Units:
Domoticz.Unit(Name="Inverter input 1 voltage (SN: " + serialNumber + ")",
Unit=(self.inputVoltage1Unit), Type=243, Subtype=8,
DeviceID=serialNumber).Create()
if self.inputAmps1Unit not in Devices[serialNumber].Units:
Domoticz.Unit(Name="Inverter input 1 Current (SN: " + serialNumber + ")",
Unit=(self.inputAmps1Unit), Type=243, Subtype=23,
Switchtype=4, Used=0, DeviceID=serialNumber).Create()
if self.inputPower1Unit not in Devices[serialNumber].Units:
Domoticz.Unit(Name="Inverter input 1 power (SN: " + serialNumber + ")",
Unit=(self.inputPower1Unit), Type=243, Subtype=29,
Switchtype=4, Used=1, DeviceID=serialNumber).Create()
if self.inputVoltage2Unit not in Devices[serialNumber].Units:
Domoticz.Unit(Name="Inverter input 2 voltage (SN: " + serialNumber + ")",
Unit=(self.inputVoltage2Unit), Type=243, Subtype=8, Used=0,
DeviceID=serialNumber).Create()
#input string 2.. 4 are optional. Set devices to not-used
if self.inputAmps2Unit not in Devices[serialNumber].Units:
Domoticz.Unit(Name="Inverter input 2 Current (SN: " + serialNumber + ")",
Unit=(self.inputAmps2Unit), Type=243, Subtype=23,
Switchtype=4, Used=0, DeviceID=serialNumber).Create()
if self.inputVoltage3Unit not in Devices[serialNumber].Units:
Domoticz.Unit(Name="Inverter input 3 voltage (SN: " + serialNumber + ")",
Unit=(self.inputVoltage3Unit), Type=243, Subtype=8, Used=0,
DeviceID=serialNumber).Create()
if self.inputAmps3Unit not in Devices[serialNumber].Units:
Domoticz.Unit(Name="Inverter input 3 Current (SN: " + serialNumber + ")",
Unit=(self.inputAmps3Unit), Type=243, Subtype=23,
Switchtype=4, Used=0, DeviceID=serialNumber).Create()
if self.inputVoltage4Unit not in Devices[serialNumber].Units:
Domoticz.Unit(Name="Inverter input 4 voltage (SN: " + serialNumber + ")",
Unit=(self.inputVoltage4Unit), Type=243, Subtype=8, Used=0,
DeviceID=serialNumber).Create()
if self.inputAmps4Unit not in Devices[serialNumber].Units:
Domoticz.Unit(Name="Inverter input 4 Current (SN: " + serialNumber + ")",
Unit=(self.inputAmps4Unit), Type=243, Subtype=23,
Switchtype=4, Used=0, DeviceID=serialNumber).Create()
if self.inputPower2Unit not in Devices[serialNumber].Units:
Domoticz.Unit(Name="Inverter input 2 power (SN: " + serialNumber + ")",
Unit=(self.inputPower2Unit), Type=243, Subtype=29,
Switchtype=4, Used=0, DeviceID=serialNumber).Create()
if self.inputPower3Unit not in Devices[serialNumber].Units:
Domoticz.Unit(Name="Inverter input 3 power (SN: " + serialNumber + ")",
Unit=(self.inputPower3Unit), Type=243, Subtype=29,
Switchtype=4, Used=0, DeviceID=serialNumber).Create()
if self.inputPower4Unit not in Devices[serialNumber].Units:
Domoticz.Unit(Name="Inverter input 4 power (SN: " + serialNumber + ")",
Unit=(self.inputPower4Unit), Type=243, Subtype=29,
Switchtype=4, Used=0, DeviceID=serialNumber).Create()
if self.outputFreq1Unit not in Devices[serialNumber].Units:
Domoticz.Unit(Name="Inverter output frequency 1 (SN: " + serialNumber + ")",
Unit=(self.outputFreq1Unit), TypeName="Custom",
Used=0, DeviceID=serialNumber).Create()
logging.info("finished creating devices, current count: "+str(len(Devices[serialNumber].Units)))
if "54200DSN196R0358" in Devices:
Domoticz.Debug("Number of Units: " + str(len(Devices[serialNumber].Units)) + ", number of units: " + str(len(Devices["54200DSN196R0358"].Units)) + ")")
else:
Domoticz.Debug("no Device with Serial Number found")
#Domoticz.Log("Number of Devices: " + str(len(Devices[serialNumber].Units)) + ", created for GoodWe inverter (SN: " + serialNumber + ")")
def onStart(self):
self.logger = logging.getLogger('root')
self.log_filename = "goodwe "+Parameters["Name"]+".log"
if Parameters["Mode6"] == "Verbose":
Domoticz.Debugging(1)
logging.basicConfig(format='%(asctime)s - %(levelname)-8s - %(filename)-18s - %(message)s', filename=self.log_filename,level=logging.DEBUG)
Domoticz.Status("Starting Goodwe SEMS API plugin, logging to file {0}".format(self.log_filename))
DumpConfigToLog()
elif Parameters["Mode6"] == "Debug":
Domoticz.Debugging(2)
logging.basicConfig(format='%(asctime)s - %(levelname)-8s - %(filename)-18s - %(message)s', filename=self.log_filename,level=logging.DEBUG)
Domoticz.Status("Starting Goodwe SEMS API plugin, logging to file {0}".format(self.log_filename))
DumpConfigToLog()
else:
logging.basicConfig(format='%(asctime)s - %(levelname)-8s - %(filename)-18s - %(message)s', filename=self.log_filename,level=logging.INFO)
Domoticz.Status("Starting Goodwe SEMS API plugin, logging to file {0}".format(self.log_filename))
logging.info("starting plugin version "+Parameters["Version"])
#check upgrading of version needs actions
self.version = Parameters["Version"]
self.enabled = self.checkVersion(self.version)
if not self.enabled:
return False
self.goodWeAccount = GoodWe(Parameters["Address"], Parameters["Port"], Parameters["Username"], Parameters["Password"])
self.runAgain = int(Parameters["Mode2"])
if len(Parameters["Mode1"]) == 0:
Domoticz.Error("No Power Station ID provided, exiting")
logging.error("No Power Station ID provided, exiting")
return
self.startDeviceUpdateV2()
def onStop(self):
Domoticz.Log("onStop - Plugin is stopping.")
logging.info("onStop - Plugin is stopping.")
if self.httpConn is not None:
self.httpConn.Disconnect()
def onConnect(self, Connection, Status, Description):
logging.debug("onConnect: Status: '" + str(Status) + "', Description: '" + Description + "'")
if (Status == 0):
logging.debug("Connected to SEMS portal API successfully.")
self.startDeviceUpdate(Connection)
else:
Domoticz.Log("Failed to connect (" + str(Status) + ") to: " + Parameters["Address"] + ":" + Parameters[
"Port"] + " with error: " + Description)
logging.info("Failed to connect (" + str(Status) + ") to: " + Parameters["Address"] + ":" + Parameters[
"Port"] + " with error: " + Description)
def onMessage(self, Connection, Data):
logging.debug("onMessage: data received: Data : '" + str(Data) + "'")
Domoticz.Error("onMessage called unexpectedly: data received: Data : '" + str(Data) + "'")
return
status = int(Data["Status"])
if status == 200:
apiUrl = ""
try:
#apiResponse sometimes contains invalid data, catch the exception
if "Data" in Data:
responseText = Data["Data"].decode("utf-8", "ignore")
else:
responseText = ""
logging.debug("onMessage: no data found in data")
return
apiResponse = json.loads(responseText)
apiUrl = apiResponse["components"]["api"]
apiData = apiResponse["data"]
except json.JSONDecodeError as err:
msg, doc, pos = err.args
logging.error(msg)
Domoticz.Error(msg)
apiResponse = None
logging.debug("The faulty message: '" + doc)
if "api/v2/Common/CrossLogin" in apiUrl:
logging.debug("message received: CrossLogin")
if apiData == 'Null':
Domoticz.Log("SEMS API Token not received")
logging.info("SEMS API Token not received")
self.goodWeAccount.tokenAvailable = False
self.httpConn.Disconnect()
#self.httpConn = None
else:
logging.debug("message apiData: '" + str(apiData) + "'")
self.goodWeAccount.token = apiData
logging.debug("SEMS API Token: " + json.dumps(self.goodWeAccount.token))
self.goodWeAccount.tokenAvailable = True
#request list of stations on this account
Connection.Send(self.goodWeAccount.stationListRequest())
elif "/api/v2/HistoryData/QueryPowerStationByHistory" in apiUrl:
logging.debug("message received: QueryPowerStationByHistory")
if apiData is None or len(apiData)==0:
Domoticz.Log("Power station history not received")
logging.info("Power station history not received")
self.httpConn.Disconnect()
#self.httpConn = None
else:
logging.debug("message apiData: '" + str(apiData) + "'")
for stations in apiData["list"]:
for data in stations:
logging.debug("station element: '"+ str(data) + "', value: '" + str(stations[data]) +"'")
for key, station in enumerate(apiData["list"]):
if len(Parameters["Mode1"]) <= 0 or (len(Parameters["Mode1"]) > 0 and station["id"] == Parameters["Mode1"]):
#add all found stations if no ID entered, else only log the one that matches the ID
self.goodWeAccount.createStation(key, station)
Domoticz.Log("Station found: " + station["id"])
logging.info("Station found: " + station["id"])
Connection.Send(self.goodWeAccount.stationDataRequest(self.baseDeviceIndex))
elif "api/v2/PowerStation/GetMonitorDetailByPowerstationId" in apiUrl:
logging.debug("message received: GetMonitorDetailByPowerstationId")
if apiData is None or len(apiData) == 0:
Domoticz.Error("No station data received from GoodWe SEMS API (Station ID: " + str(self.goodWeAccount.powerStationList[
self.goodWeAccount.powerStationIndex]) + ")")
logging.error("No station data received from GoodWe SEMS API (Station ID: " + str(self.goodWeAccount.powerStationList[
self.goodWeAccount.powerStationIndex]) + ")")
self.goodWeAccount.tokenAvailable = False
else:
#logging.debug("message apiData: '" + str(apiData) + "'")
Domoticz.Log("Station data received from GoodWe SEMS API ('" + str(self.goodWeAccount.powerStationList[
self.goodWeAccount.powerStationIndex]) + "', index : '" + str(self.goodWeAccount.powerStationIndex)+ "')")
logging.info("Station data received from GoodWe SEMS API ('" + str(self.goodWeAccount.powerStationList[
self.goodWeAccount.powerStationIndex]) + "', index : '" + str(self.goodWeAccount.powerStationIndex)+ "')")
theStation = self.goodWeAccount.powerStationList[self.goodWeAccount.powerStationIndex]
for inverter in apiData["inverter"]:
logging.debug("inverter found with SN: '" + inverter["sn"] + "'")
if inverter["sn"] in theStation.inverters:
#stheStation.inverters[inverter["sn"]].createDevices(Devices)
self.createDevices(inverter["sn"])
logging.debug("Details d in inverter: '" + str(inverter['d']) + "'")
theInverter = theStation.inverters[inverter["sn"]]
if len(inverter['fault_message']) > 0:
Domoticz.Log("Fault message from GoodWe inverter (SN: " + inverter["sn"] + "): '" + str(inverter['fault_message']) + "'")
logging.info("Fault message from GoodWe inverter (SN: " + inverter["sn"] + "): '" + str(inverter['fault_message']) + "'")
Domoticz.Log("Status of GoodWe inverter (SN: " + inverter["sn"] + "): '" + str(inverter["status"]) + ' ' + self.goodWeAccount.INVERTER_STATE[inverter["status"]] + "'")
logging.info("Status of GoodWe inverter (SN: " + inverter["sn"] + "): '" + str(inverter["status"]) + ' ' + self.goodWeAccount.INVERTER_STATE[inverter["status"]] + "'")
Devices[theInverter.inverterStateUnit].Update(nValue=inverter["status"]+1, sValue=str((inverter["status"]+2)*10))
if self.goodWeAccount.INVERTER_STATE[inverter["status"]] == 'generating':
logging.debug("inverter generating, log temp")
UpdateDevice(theInverter.inverterTemperatureUnit, 0, str(inverter["tempperature"]))
UpdateDevice(theInverter.outputFreq1Unit, 0, str(inverter["d"]["fac1"]))
UpdateDevice(theInverter.outputCurrentUnit, 0, str(inverter["output_current"]), AlwaysUpdate=True)
UpdateDevice(theInverter.outputVoltageUnit, 0, str(inverter["output_voltage"]), AlwaysUpdate=True)
UpdateDevice(theInverter.outputPowerUnit, 0, str(inverter["output_power"]) + ";" + str(inverter["etotal"] * 1000), AlwaysUpdate=True)
inputVoltage,inputAmps = inverter["pv_input_1"].split('/')
inputPower = (float(inputVoltage[:-1])) * (float(inputAmps[:-1]))
UpdateDevice(theInverter.inputVoltage1Unit, 0, inputVoltage, AlwaysUpdate=True)
UpdateDevice(theInverter.inputAmps1Unit, 0, inputAmps, AlwaysUpdate=True)
UpdateDevice(theInverter.inputPower1Unit, 0, str(inputPower) + ";0", AlwaysUpdate=True)
#test to log cumulative counting
previousPower,currentCount = Devices[theInverter.inputPowerTest].sValue.split(";")
newCounter = inputPower + float(currentCount)
UpdateDevice(theInverter.inputPowerTest, 0, str(inputPower) + ";" + str(newCounter), AlwaysUpdate=True)
logging.debug("test previousPower, currentCount, newCounter = " + str(previousPower) + ", " + str(currentCount) + ", " + str(newCounter))
if "pv_input_2" in inverter:
logging.debug("Second string found")
inputVoltage,inputAmps = inverter["pv_input_2"].split('/')
UpdateDevice(theInverter.inputVoltage2Unit, 0, inputVoltage, AlwaysUpdate=True)
UpdateDevice(theInverter.inputAmps2Unit, 0, inputAmps, AlwaysUpdate=True)
inputPower = (float(inputVoltage[:-1])) * (float(inputAmps[:-1]))
UpdateDevice(theInverter.inputPower2Unit, 0, str(inputPower) + ";0", AlwaysUpdate=True)
if "pv_input_3" in inverter:
logging.debug("Third string found")
inputVoltage,inputAmps = inverter["pv_input_3"].split('/')
UpdateDevice(theInverter.inputVoltage3Unit, 0, inputVoltage, AlwaysUpdate=True)
UpdateDevice(theInverter.inputAmps3Unit, 0, inputAmps, AlwaysUpdate=True)
inputPower = float(inputVoltage[:-1]) * float(inputAmps[:-1])
UpdateDevice(theInverter.inputPower3Unit, 0, str(inputPower) + ";0", AlwaysUpdate=True)
if "pv_input_4" in inverter:
logging.debug("Fourth string found")
inputVoltage,inputAmps = inverter["pv_input_4"].split('/')
UpdateDevice(theInverter.inputVoltage4Unit, 0, inputVoltage, AlwaysUpdate=True)
UpdateDevice(theInverter.inputAmps4Unit, 0, inputAmps, AlwaysUpdate=True)
inputPower = float(inputVoltage[:-1]) * float(inputAmps[:-1])
UpdateDevice(theInverter.inputPower4Unit, 0, str(inputPower) + ";0", AlwaysUpdate=True)
else:
Domoticz.Log("Unknown inverter found with S/N: '" + inverter["sn"] +"'.")
logging.info("Unknown inverter found with S/N: '" + inverter["sn"] +"'.")
if self.goodWeAccount.powerStationIndex == (len(self.goodWeAccount.powerStationList) - 1):
logging.debug("Last station of list found")
if self.runAgain > 2:
logging.info("Next active heartbeat far away, disconnecting and dropping connection.")
self.httpConn.Disconnect()
self.devicesUpdated = True
else:
logging.debug("Retrieving next station data (ID: " + self.goodWeAccount.powerStationList[self.baseDeviceIndex] + ")")
self.baseDeviceIndex += 1
Connection.Send(self.goodWeAccount.stationDataRequest(self.baseDeviceIndex))
elif status == 302:
logging.error("GoodWe SEMS API returned a Page Moved Error.")
Domoticz.Error("GoodWe SEMS API returned a Page Moved Error.")
elif status == 400:
logging.error("GoodWe SEMS API returned a Bad Request Error.")
Domoticz.Error("GoodWe SEMS API returned a Bad Request Error.")
elif (status == 500):
logging.error("GoodWe SEMS API returned a Server Error.")
Domoticz.Error("GoodWe SEMS API returned a Server Error.")
else:
logging.error("GoodWe SEMS API returned a status: " + str(status))
Domoticz.Error("GoodWe SEMS API returned a status: " + str(status))
def onCommand(self, Unit, Command, Level, Hue):
logging.debug("onCommand called for Unit " + str(Unit) + ": Parameter '" + str(Command) + "', Level: " + str(Level))
def onDisconnect(self, Connection):
logging.debug("onDisconnect called for connection to: " + Connection.Address + ":" + Connection.Port)
self.httpConn = None
def onHeartbeat(self):
if self.enabled:
if self.httpConn is not None and (self.httpConn.Connecting() or self.httpConn.Connected()) and not self.devicesUpdated:
logging.debug("onHeartbeat called, Connection is alive.")
elif len(Parameters["Mode1"]) == 0:
Domoticz.Error("No Power Station ID provided, exiting")
logging.error("No Power Station ID provided, exiting")
return
else:
self.runAgain = self.runAgain - 1
if self.runAgain <= 0:
logging.debug("onHeartbeat called, starting device update.")
self.startDeviceUpdateV2()
self.runAgain = int(Parameters["Mode2"])
# else:
# logging.debug("onHeartbeat called, run again in " + str(self.runAgain) + " heartbeats.")
def checkVersion(self, version):
"""checks actual version against stored version as 'Ma.Mi.Pa' and checks if updates needed"""
#read version from stored configuration
ConfVersion = getConfigItem("plugin version", "0.0.0")
Domoticz.Log("Starting version: " + version )
logging.info("Starting version: " + version )
MaCurrent,MiCurrent,PaCurrent = version.split('.')
MaConf,MiConf,PaConf = ConfVersion.split('.')
logging.debug("checking versions: current '{0}', config '{1}'".format(version, ConfVersion))
can_continue = True
if int(MaConf) < int(MaCurrent):
Domoticz.Log("Major version upgrade: {0} -> {1}".format(MaConf,MaCurrent))
logging.info("Major version upgrade: {0} -> {1}".format(MaConf,MaCurrent))
#add code to perform MAJOR upgrades
if int(MaConf) < 4:
can_continue = self.updateToEx()
elif int(MiConf) < int(MiCurrent):
Domoticz.Debug("Minor version upgrade: {0} -> {1}".format(MiConf,MiCurrent))
logging.debug("Minor version upgrade: {0} -> {1}".format(MiConf,MiCurrent))
#add code to perform MINOR upgrades
elif int(PaConf) < int(PaCurrent):
Domoticz.Debug("Patch version upgrade: {0} -> {1}".format(PaConf,PaCurrent))
logging.debug("Patch version upgrade: {0} -> {1}".format(PaConf,PaCurrent))
#add code to perform PATCH upgrades, if any
if ConfVersion != version and can_continue:
#store new version info
self._setVersion(MaCurrent,MiCurrent,PaCurrent)
return can_continue
def updateToEx(self):
"""routine to check if we can update to the Domoticz extended plugin framework"""
if len(Devices)>0:
Domoticz.Error("Devices are present. Please upgrade them before upgrading to this version!")
Domoticz.Error("Plugin will now exit but will be enabled on next start")
self._setVersion("4","0","0")
return False
else:
return True
def _setVersion(self, major, minor, patch):
#set configs
logging.debug("Setting version to {0}.{1}.{2}".format(major, minor, patch))
setConfigItem(Key="MajorVersion", Value=major)
setConfigItem(Key="MinorVersion", Value=minor)
setConfigItem(Key="patchVersion", Value=patch)
setConfigItem(Key="plugin version", Value="{0}.{1}.{2}".format(major, minor, patch))
global _plugin
_plugin = GoodWeSEMSPlugin()
def calculateNewEnergy(Device, Unit, inputPower):
try:
#read power currently on display (comes from previous update) in Watt and energy counter uptill now in Wh
previousPower,currentCount = Devices[Device].Units[Unit].sValue.split(";")
except:
#in case no values there, just assume zero
previousPower = 0 #Watt
currentCount = 0 #Wh
dt_format = "%Y-%m-%d %H:%M:%S"
dt_string = Devices[Device].Units[Unit].LastUpdate
if len(dt_string) > 0:
lastUpdateDT = datetime.fromtimestamp(time.mktime(time.strptime(dt_string, dt_format)))
else:
lastUpdateDT = datetime.now()
elapsedTime = datetime.now() - lastUpdateDT
logging.debug("Test power, previousPower: {}, last update: {:%Y-%m-%d %H:%M}, elapsedTime: {}, elapsedSeconds: {:6.2f}".format(previousPower, lastUpdateDT, elapsedTime, elapsedTime.total_seconds()))
#average current and previous power (Watt) and multiply by elapsed time (hour) to get Watt hour
previousPower = str(previousPower).replace("w","").replace("W","")
newCount = round(((float(previousPower) + inputPower ) / 2) * elapsedTime.total_seconds()/3600,2)
newCounter = newCount + float(currentCount) #add the amount of energy since last update to the already logged energy
logging.debug("Test power, previousPower: {}, currentCount: {:6.2f}, newCounter: {:6.2f}, added: {:6.2f}".format(previousPower, float(currentCount), newCounter, newCount))
return newCounter
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")
logging.info("File written")
def DumpConfigToLog():
Domoticz.Debug("Parameters count: " + str(len(Parameters)))
for x in Parameters:
if Parameters[x] != "":
Domoticz.Debug("Parameter: '" + x + "':'" + str(Parameters[x]) + "'")
Domoticz.Debug("Device count: " + str(len(Devices)))
for DeviceName in Devices:
Device = Devices[DeviceName]
Domoticz.Debug("Device: '" + str(Device) + "'")
for UnitNo in Device.Units:
Unit = Device.Units[UnitNo]
Domoticz.Debug(" - Unit: '" + str(Unit) + "'")
return
def DumpHTTPResponseToLog(httpDict):
if isinstance(httpDict, dict):
logging.debug("HTTP Details (" + str(len(httpDict)) + "):")
for x in httpDict:
if isinstance(httpDict[x], dict):
logging.debug("--->'" + x + " (" + str(len(httpDict[x])) + "):")
for y in httpDict[x]:
logging.debug("------->'" + y + "':'" + str(httpDict[x][y]) + "'")
else:
logging.debug("--->'" + x + "':'" + str(httpDict[x]) + "'")
def UpdateDevice(Device, Unit, nValue, sValue, AlwaysUpdate=False):
# Make sure that the Domoticz device still exists (they can be deleted) before updating it
if (Device in Devices):
logging.debug("Updating device '"+Devices[Device].Units[Unit].Name+ "' with current sValue '"+Devices[Device].Units[Unit].sValue+"' to '" +sValue+"'")
if (Devices[Device].Units[Unit].nValue != nValue) or (Devices[Device].Units[Unit].sValue != sValue) or AlwaysUpdate:
#try:
Devices[Device].Units[Unit].nValue = nValue
Devices[Device].Units[Unit].sValue = sValue
Devices[Device].Units[Unit].Update()
logging.debug("Update "+str(nValue)+":'"+str(sValue)+"' ("+Devices[Device].Units[Unit].Name+")")
# except:
# Domoticz.Error("Update of device failed: "+str(Unit)+"!")
# logging.error("Update of device failed: "+str(Unit)+"!")
return
# Configuration Helpers
def getConfigItem(Key=None, Default={}):
Value = Default
try:
Config = Domoticz.Configuration()
if (Key != None):
Value = Config[Key] # only return requested key if there was one
else:
Value = Config # return the whole configuration if no key
except KeyError:
Value = Default
except Exception as inst:
Domoticz.Error("Domoticz.Configuration read failed: '"+str(inst)+"'")
return Value
def setConfigItem(Key=None, Value=None):
Config = {}
if type(Value) not in (str, int, float, bool, bytes, bytearray, list, dict):
Domoticz.Error("A value is specified of a not allowed type: '" + str(type(Value)) + "'")
return Config
try:
Config = Domoticz.Configuration()
if (Key != None):
Config[Key] = Value
else:
Config = Value # set whole configuration if no key specified
Config = Domoticz.Configuration(Config)
except Exception as inst:
Domoticz.Error("Domoticz.Configuration operation failed: '"+str(inst)+"'")
return Config