-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
365 lines (276 loc) · 16.2 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
"""
<plugin key="GyverLamp" name="GyverLamp domoticz plugin" author="Whilser" version="0.1.0" wikilink="" externallink="">
<description>
<h2>GyverLamp </h2><br/>
<h3>Настройка</h3>
Введите идентификатор лампы. Если ID лампы неизвестен, просто оставьте в поле идентификатора значение 0, это запустит процедуру поиска лампы в сети. <br/>
По окончании поиска ID лампы отобразится в Журнале. Идентификатор лампы можно также посмотреть в веб интерфейсе на вкладке "Инфо" в графе ID лампы. <br/>
</description>
<params>
<param field="Mode1" label="ID" width="300px" required="true" default="0"/>
<param field="Mode2" label="Debug" width="75px">
<options>
<option label="True" value="Debug"/>
<option label="False" value="Normal" default="True" />
</options>
</param>
</params>
</plugin>
"""
import os
import sys
import os.path
import json
import socket
import Domoticz
class BasePlugin:
UNIT_LAMP = 1
UNIT_SPEED = 2
UNIT_SCALE = 3
UNIT_EFFECTS = 4
discovered = False
nextTimeSync = 0
id = 1
IP = ''
port = 0
def __init__(self):
return
def onStart(self):
Domoticz.Debug("onStart called")
if Parameters['Mode2'] == 'Debug': Domoticz.Debugging(1)
if Parameters['Mode1'] == '0':
self.discover()
return
self.loadConfig()
self.createLamp()
DumpConfigToLog()
Domoticz.Heartbeat(20)
def onStop(self):
Domoticz.Debug("onStop called")
def onConnect(self, Connection, Status, Description):
Domoticz.Debug("onConnect called")
def onMessage(self, Connection, Data):
Domoticz.Debug("onMessage called")
def onCommand(self, Unit, Command, Level, Color):
Domoticz.Debug("onCommand called for Unit " + str(Unit) + ": Parameter '" + str(Command) + "', Level: " + str(Level)+ ", Color: "+str(Color))
if Unit == self.UNIT_EFFECTS:
self.HandleEffects(Level)
return
Level = max(min(Level, 100), 1)
if not self.discover(Parameters['Mode1']): return
try:
if Command == 'On':
reply = self.sendCommand('P_ON').split()
if reply[5] == '1':
if self.UNIT_LAMP in Devices: Devices[self.UNIT_LAMP].Update(nValue=1, sValue='On', TimedOut = False)
if self.UNIT_SPEED in Devices: Devices[self.UNIT_SPEED].Update(nValue=1, sValue='On', TimedOut = False)
if self.UNIT_SCALE in Devices: Devices[self.UNIT_SCALE].Update(nValue=1, sValue='On', TimedOut = False)
if self.UNIT_EFFECTS in Devices: Devices[self.UNIT_EFFECTS].Update(nValue=1, sValue=Devices[self.UNIT_EFFECTS].sValue, TimedOut = False)
elif Command == 'Off':
reply = self.sendCommand('P_OFF').split()
if reply[5] == '0':
if self.UNIT_LAMP in Devices: Devices[self.UNIT_LAMP].Update(nValue=0, sValue='Off', TimedOut = False)
if self.UNIT_SPEED in Devices: Devices[self.UNIT_SPEED].Update(nValue=0, sValue='Off', TimedOut = False)
if self.UNIT_SCALE in Devices: Devices[self.UNIT_SCALE].Update(nValue=0, sValue='Off', TimedOut = False)
if self.UNIT_EFFECTS in Devices: Devices[self.UNIT_EFFECTS].Update(nValue=0, sValue=Devices[self.UNIT_EFFECTS].sValue, TimedOut = False)
elif Command == 'Set Level':
if (Unit == self.UNIT_LAMP):
reply = self.sendCommand('BRI ' + str(Level)).split()
if reply[0] == 'BRI': Devices[self.UNIT_LAMP].Update(nValue=1, sValue=reply[1], TimedOut = False)
elif (Unit == self.UNIT_SCALE):
reply = self.sendCommand('SCA ' + str(Level)).split()
if reply[0] == 'SCA': Devices[self.UNIT_SCALE].Update(nValue=1, sValue=reply[1], TimedOut = False)
elif (Unit == self.UNIT_SPEED):
reply = self.sendCommand('SPD ' + str(Level)).split()
if reply[0] == 'SPD': Devices[self.UNIT_SPEED].Update(nValue=1, sValue=reply[1], TimedOut = False)
except Exception as e:
Domoticz.Log('Error send command to {0} with IP {1}. Device is not responding, check power/network connection. Errror: {2}'.format(Parameters['Name'], self.IP, e.__class__))
Devices[self.UNIT_LAMP].Update(nValue=Devices[self.UNIT_LAMP].nValue, sValue=Devices[self.UNIT_LAMP].sValue, TimedOut = True)
Devices[self.UNIT_SPEED].Update(nValue=Devices[self.UNIT_SPEED].nValue, sValue=Devices[self.UNIT_SPEED].sValue, TimedOut = True)
Devices[self.UNIT_SCALE].Update(nValue=Devices[self.UNIT_SCALE].nValue, sValue=Devices[self.UNIT_SCALE].sValue, TimedOut = True)
Devices[self.UNIT_EFFECTS].Update(nValue=Devices[self.UNIT_EFFECTS].nValue, sValue=Devices[self.UNIT_EFFECTS].sValue, TimedOut = True)
def onNotification(self, Name, Subject, Text, Status, Priority, Sound, ImageFile):
Domoticz.Debug("Notification: " + Name + "," + Subject + "," + Text + "," + Status + "," + str(Priority) + "," + Sound + "," + ImageFile)
def onDisconnect(self, Connection):
Domoticz.Debug("onDisconnect called")
def onHeartbeat(self):
Domoticz.Debug("onHeartbeat called")
if Parameters['Mode1'] == '0': return
if not self.discover(Parameters['Mode1']): return
self.nextTimeSync -= 1
try:
if ((self.nextTimeSync <= 0) and (self.UNIT_LAMP in Devices)):
self.nextTimeSync = 3 #синхронизация 1 раз в минуту. 15 - один раз в пять минут
reply = self.sendCommand("GET").split()
if (reply[5] == "1"):
if ((Devices[self.UNIT_LAMP].sValue != 'On') or (Devices[self.UNIT_LAMP].nValue != 1) or (Devices[self.UNIT_LAMP].TimedOut == True)):
Devices[self.UNIT_LAMP].Update(nValue=1, sValue='On', TimedOut = False)
if (self.UNIT_SCALE in Devices):
if ((Devices[self.UNIT_SCALE].sValue != 'On') or (Devices[self.UNIT_SCALE].nValue != 1) or (Devices[self.UNIT_SCALE].TimedOut == True)):
Devices[self.UNIT_SCALE].Update(nValue=1, sValue='On', TimedOut = False)
if (self.UNIT_SPEED in Devices):
if ((Devices[self.UNIT_SPEED].sValue != 'On') or (Devices[self.UNIT_SPEED].nValue != 1) or (Devices[self.UNIT_SPEED].TimedOut == True)):
Devices[self.UNIT_SPEED].Update(nValue=1, sValue='On', TimedOut = False)
if (self.UNIT_EFFECTS in Devices):
Devices[self.UNIT_EFFECTS].Update(nValue=1, sValue=Devices[self.UNIT_EFFECTS].sValue, TimedOut = False)
if (reply[5] == "0"):
if ((Devices[self.UNIT_LAMP].sValue != 'Off') or (Devices[self.UNIT_LAMP].nValue != 0) or (Devices[self.UNIT_LAMP].TimedOut == True)):
Devices[self.UNIT_LAMP].Update(nValue=0, sValue='Off', TimedOut = False)
if (self.UNIT_SCALE in Devices):
if ((Devices[self.UNIT_SCALE].sValue != 'Off') or (Devices[self.UNIT_SCALE].nValue != 0) or (Devices[self.UNIT_SCALE].TimedOut == True)):
Devices[self.UNIT_SCALE].Update(nValue=0, sValue='Off', TimedOut = False)
if (self.UNIT_SPEED in Devices):
if ((Devices[self.UNIT_SPEED].sValue != 'Off') or (Devices[self.UNIT_SPEED].nValue != 0) or (Devices[self.UNIT_SPEED].TimedOut == True)):
Devices[self.UNIT_SPEED].Update(nValue=0, sValue='Off', TimedOut = False)
if (self.UNIT_EFFECTS in Devices):
Devices[self.UNIT_EFFECTS].Update(nValue=0, sValue=Devices[self.UNIT_EFFECTS].sValue, TimedOut = False)
except Exception as e:
for x in Devices:
if Devices[x].TimedOut == False: Devices[x].Update(nValue=Devices[x].nValue, sValue=Devices[x].sValue, TimedOut = True)
def HandleEffects(self, Level):
if (Level > 0):
reply = self.sendCommand("EFF "+str((Level/10)-1))
Domoticz.Debug('Reply: '+reply)
if self.UNIT_EFFECTS in Devices: Devices[self.UNIT_EFFECTS].Update(nValue=1, sValue=str(Level))
reply = self.sendCommand('P_ON').split()
if reply[5] == '1':
if (self.UNIT_LAMP in Devices): Devices[self.UNIT_LAMP].Update(nValue=1, sValue='On', TimedOut = False)
if self.UNIT_SPEED in Devices: Devices[self.UNIT_SPEED].Update(nValue=1, sValue='On', TimedOut = False)
if self.UNIT_SCALE in Devices: Devices[self.UNIT_SCALE].Update(nValue=1, sValue='On', TimedOut = False)
def loadConfig(self):
config_Path = os.path.join(str(Parameters['HomeFolder']), "GyverLamp"+str(Parameters["HardwareID"])+'.json')
if os.path.isfile(config_Path):
Domoticz.Debug('Loading config from '+config_Path)
with open(config_Path) as json_file:
config = json.load(json_file)
self.IP = config['IP']
self.deviceID = config['DeviceID']
self.port = config['Port']
else: self.discover(Parameters['Mode1'])
def createLamp(self):
if self.UNIT_LAMP not in Devices: Domoticz.Device(Name='lamp', Unit=self.UNIT_LAMP, Type=244, Subtype=73, Switchtype=7, Used=1).Create()
if self.UNIT_SPEED not in Devices: Domoticz.Device(Name='Speed', Unit=self.UNIT_SPEED, Type=244, Subtype=73, Switchtype=7, Used=1).Create()
if self.UNIT_SCALE not in Devices: Domoticz.Device(Name='Scale', Unit=self.UNIT_SCALE, Type=244, Subtype=73, Switchtype=7, Used=1).Create()
if (self.UNIT_EFFECTS not in Devices):
fx_list = self.sendCommand("FX_GET")
Options = { "Effects": "|||||", "LevelNames": fx_list, "LevelOffHidden": "true", "SelectorStyle": "1" }
Domoticz.Device(Name="Effects", Unit=self.UNIT_EFFECTS, Type=244, Subtype=62 , Switchtype=18, Options = Options, Used=1).Create()
def sendCommand(self, command):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(4)
s.sendto(command.encode(), (self.IP, self.port))
data = s.recvfrom(1024)
s.close()
self.id += 1
Domoticz.Debug('Command sent: '+command)
Domoticz.Debug("Reply received: {}".format(data[0].decode()))
return data[0].decode()
except Exception as e:
Domoticz.Log('Could not send command {0} to {1} with IP {2} and port {3}. Device is not responding, check power/network connection. Errror: {4}'.format(command, Parameters['Name'], self.IP, self.port, e.__class__))
for x in Devices:
if Devices[x].TimedOut == False: Devices[x].Update(nValue=Devices[x].nValue, sValue=Devices[x].sValue, TimedOut = True)
self.discovered = False
return "Error"
def discover(self, deviceID = None):
if self.discovered: return True
if len(self.IP) > 0:
Domoticz.Debug('Loaded saved configuration.')
Domoticz.Log('Attempt to connect to device with ID: {0}, IP address: {1} '.format(self.deviceID, self.IP))
status = self.sendCommand("DEB").split()
if (status[0] != "OK"):
Domoticz.Log('Could not connect to {0} with IP {1}, starting discover.'.format(Parameters['Name'], self.IP))
self.IP = ''
else:
Domoticz.Log('Connected to device ID: {0} with IP address: {1}'.format(self.deviceID, self.IP))
self.discovered = True
for x in Devices:
if Devices[x].TimedOut == True: Devices[x].Update(nValue=Devices[x].nValue, sValue=Devices[x].sValue, TimedOut = False)
return self.discovered
Domoticz.Log('Starting discover with Device ID: {}.'.format(deviceID))
self.discovered = False
timeout = 5
addr = '<broadcast>'
discoveredDevice = None
helobytes = 'DISCOVER'.encode()
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.settimeout(timeout)
s.sendto(helobytes, (addr, 8888))
while True:
try:
data, addr = s.recvfrom(1024)
reply = json.loads(data.decode())
self.deviceID = reply["deviceID"]
self.IP = reply["IP"]
self.port = reply["port"]
if deviceID is None:
Domoticz.Log('Discovered. Device ID: {0}, IP: {1}, Port: {2}'.format(self.deviceID, self.IP, self.port))
else:
if self.deviceID == deviceID:
Domoticz.Log('Discovered. Connected to device ID: {0} with IP address: {1}'.format(self.deviceID, self.IP))
config = {
"DeviceID": self.deviceID,
"IP": self.IP,
"Port": self.port
}
config_Path = os.path.join(str(Parameters['HomeFolder']), "GyverLamp"+str(Parameters["HardwareID"])+'.json')
with open(config_Path, 'w') as outfile:
if json.dump(config, outfile, indent=4): Domoticz.Debug('Config file was saved.')
self.discovered = True
for x in Devices:
if Devices[x].TimedOut == True: Devices[x].Update(nValue=Devices[x].nValue, sValue=Devices[x].sValue, TimedOut = False)
return self.discovered
except socket.timeout:
Domoticz.Log('Discovery done')
if ((deviceID is not None) and (self.discovered == False)):
Domoticz.Error('Could not discover with Device ID = {0}. Check power/network/Device ID.'.format(deviceID))
self.IP = ''
for x in Devices:
if Devices[x].TimedOut == False: Devices[x].Update(nValue=Devices[x].nValue, sValue=Devices[x].sValue, TimedOut = True)
return self.discovered
except Exception as ex:
Domoticz.Error('Error while reading discover results: {0}'.format(ex))
break
return self.discovered
global _plugin
_plugin = BasePlugin()
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 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))
Domoticz.Debug("Device TimedOut: " + str(Devices[x].TimedOut))
return