-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplugin.py
293 lines (254 loc) · 13 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
########################################################################################
# Omnik Inverter Python Plugin for Domoticz #
# #
# MIT License #
# #
# Copyright (c) 2018 tixi #
# #
# 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. #
# #
# Author: sincze #
# #
# This plugin will read the status from the running inverter via the web interface. #
# #
# V1.0.2 21-11-20 Fix issue for Domoticz 2022.2 #
# V2.0.0 04-09-23 Multi Thread version #
# #
########################################################################################
"""
<plugin key="OmnikLocalWeb2" name="Omnik Local Inverter Plugin" author="sincze" version="2.0.0" externallink="https://github.com/sincze/Domoticz-Omnik-Local-Web-Plugin">
<description>
<h2>Retrieve available Information from Local Omnik Inverter Web Page</h2><br/>
</description>
<params>
<param field="Address" label="IP Address" width="200px" required="true" default="192.168.1.100"/>
<param field="Mode1" label="Protocol" width="75px">
<options>
<option label="HTTPS" value="https"/>
<option label="HTTP" value="http" default="true" />
</options>
</param>
<param field="Mode2" label="Inverter" width="75px">
<options>
<option label="webData" value="1" default="true"/>
<option label="myDeviceArray" value="2" />
</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+Python" value="18"/>
<option label="Connections+Queue" value="144"/>
<option label="All" value="-1"/>
</options>
</param>
</params>
</plugin>
"""
try:
import Domoticz
import threading
import re # Needed to extract data from Some result
import requests
local = False
except ImportError:
local = True
import fakeDomoticz as Domoticz
from fakeDomoticz import Devices
from fakeDomoticz import Parameters
import threading
import requests
import re # Needed to extract data from Some result
class BasePlugin:
runAgain = 6
url = ""
dataAvailable = False
inverterResponded = False
def __init__(self):
self.threads = []
def onStart(self):
if Parameters["Mode6"] == "-1":
Domoticz.Debugging(1)
# Domoticz.Log("Debugger started, use '0.0.0.0 5678' to connect")
# import debugpy
# self.debugging=True
# self.debugpy=debugpy
# debugpy.listen(("0.0.0.0", 5678))
## debugpy.wait_for_client()
# time.sleep(10)
# debugpy.breakpoint()
else:
Domoticz.Log("onStart called")
if Parameters["Mode6"] != "0":
Domoticz.Debugging(int(Parameters["Mode6"]))
DumpConfigToLog()
Domoticz.Log("Plugin started")
createDevices()
self.url = Parameters["Mode1"] + "://" + Parameters["Address"] + "/js/status.js"
Domoticz.Debug(f"Fetching URL {self.url}")
thread = threading.Thread(target=self.fetch_url, args=(self.url,))
self.threads.append(thread)
thread.start()
def onStop(self):
Domoticz.Log("Plugin stopped")
def fetch_url(self, url):
try:
response = requests.get(url)
self.inverterResponded = True
UpdateDevice(Unit=3, nValue=1, sValue="On", TimedOut=0) # Inverter device is on
except:
self.inverterResponded = False
UpdateDevice(Unit=3, nValue=0, sValue="Off", TimedOut=0)
Domoticz.Debug(f"No Response from inverter {url}")
return
try:
if ( response.ok ) and ( self.inverterResponded ):
strData = str(response.content)
LogMessage(strData)
if (Parameters["Mode2"] == "1"):
try:
strData = re.search(r'(?<=webData=").*?(?=";)', strData).group(0) # Search for the beginning of>
self.dataAvailable = True
except AttributeError:
Domoticz.Debug("No datastring found")
elif (Parameters["Mode2"] == "2"):
try:
strData = re.search(r'(?<=myDeviceArray\[0\]=").*?(?=";)', strData).group(0) # Search for the beginning of>
self.dataAvailable = True
except AttributeError:
Domoticz.Debug("No datastring found")
else:
message = f"Switching Received HTTP {response.status_code}"
Domoticz.Debug(message)
except:
Domoticz.Debug("Server did not respond!")
return
if self.dataAvailable:
strData = strData.split(",") # Split the result string in a list so we can retrieve data
Domoticz.Debug(f"Received RAW Inverter Data: {strData}") # Maybe error correction later if len(line.split()) == 11 / we expect 11 items
current=float(strData[5]) # Amount in Watt
daily=float(strData[6]) # Amount in kWh of the day
total=float(strData[7]) # Total generated kWh needs to be converted to Wh
Domoticz.Debug(f"Received Data: Current Power: {current} W, Daily Power: {daily/100} kWh, Total Power: {total/10} kWh")
sValue=f"{current};{(total*100)}"
message = f"Received Data: String for the sensor is: {sValue} !"
Domoticz.Debug(message)
UpdateDevice(Unit=1, nValue=0, sValue=sValue, TimedOut=0)
UpdateDevice(Unit=2, nValue=0, sValue=current, TimedOut=0)
UpdateDevice(Unit=4, nValue=0, sValue=f"{total*100}", TimedOut=0)
# if current == 0:
# UpdateDevice(Unit=3, nValue=1, sValue="Off", TimedOut=0) # Inverter device is on
# Domoticz.Log(f"Switching Off")
else:
Domoticz.Debug("No Inverter data found")
def onHeartbeat(self):
# Check if all threads have completed
#if all(not thread.is_alive() for thread in self.threads):
# Domoticz.Log("All threads have completed.")
# self.stop()
self.runAgain = self.runAgain - 1
if self.runAgain <= 0:
message = f"onHeartbeat Fetching URL {self.url}"
Domoticz.Debug(message)
thread = threading.Thread(target=self.fetch_url, args=(self.url,))
self.threads.append(thread)
thread.start()
self.runAgain = 6
else:
message = f"onHeartbeat called, run again in {self.runAgain} heartbeats."
Domoticz.Debug(message)
def stop(self):
# Cleanup and stop the plugin
for thread in self.threads:
thread.join()
self.stop()
global _plugin
_plugin = BasePlugin()
def onStart():
global _plugin
_plugin.onStart()
def onStop():
global _plugin
_plugin.onStop()
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 DumpHTTPResponseToLog(httpDict):
if isinstance(httpDict, dict):
Domoticz.Debug("HTTP Details ("+str(len(httpDict))+"):")
for x in httpDict:
if isinstance(httpDict[x], dict):
Domoticz.Debug("--->'"+x+" ("+str(len(httpDict[x]))+"):")
for y in httpDict[x]:
Domoticz.Debug("------->'" + y + "':'" + str(httpDict[x][y]) + "'")
else:
Domoticz.Debug("--->'" + x + "':'" + str(httpDict[x]) + "'")
def UpdateDevice(Unit, nValue, sValue, TimedOut=0, AlwaysUpdate=False):
# Make sure that the Domoticz device still exists (they can be deleted) before updating it
if (Unit in Devices):
if (Devices[Unit].nValue != nValue) or (Devices[Unit].sValue != sValue) or (Devices[Unit].TimedOut != TimedOut):
Devices[Unit].Update(nValue=nValue, sValue=str(sValue), TimedOut=TimedOut)
Devices[Unit].Update(nValue=nValue, sValue=f"{sValue}", TimedOut=TimedOut)
Domoticz.Log(f"Update {nValue};{sValue} {Devices[Unit].Name}")
return
#############################################################################
# Deviceplugin specific functions #
#############################################################################
def createDevices():
# Images
# Check if images are in database
if "Omnik" not in Images:
Domoticz.Image("Omnik.zip").Create()
image = Images["Omnik"].ID # Get id from database
Domoticz.Log(f"Image created. ID: {image}" )
# Give the devices a unique unit number. This makes updating them more easy.
# UpdateDevice() checks if the device exists before trying to update it.
if (len(Devices) == 0):
Domoticz.Device(Name="Inverter (kWh)", Unit=1, TypeName="kWh", Used=1, Image=image).Create()
Domoticz.Log("Inverter Device kWh created.")
Domoticz.Device(Name="Inverter (W)", Unit=2, TypeName="Usage", Used=1, Image=image).Create()
Domoticz.Log("Inverter Device (W) created.")
Domoticz.Device(Name="Inverter Status", Unit=3, TypeName="Switch", Used=1, Image=image).Create()
Domoticz.Log("Inverter Device (Switch) created.")
Domoticz.Device(Name="Inverter Generate", Unit=4, Type=113, Subtype=0, Used=1, Image=image).Create()
Domoticz.Log("Inverter Device (Generated) created.")