forked from moroen/IKEA-Tradfri-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.py
323 lines (253 loc) · 12.1 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
# IKEA Tradfri Python Plugin
#
# Author: moroen
#
"""
<plugin key="IKEA-Tradfri" name="IKEA Tradfri" author="moroen" version="1.0.6" externallink="https://github.com/moroen/IKEA-Tradfri-plugin">
<params>
<param field="Address" label="Adaptor IP Address" width="200px" required="true" default="127.0.0.1"/>
<param field="Mode2" label="Observe changes" width="75px">
<options>
<option label="Yes" value="True"/>
<option label="No" value="False" default="true" />
</options>
</param>
<param field="Mode4" label="Polling interval (seconds)" width="75px" required="true" default="30"/>
<param field="Mode3" label="Add groups as devices" width="75px">
<options>
<option label="Yes" value="True"/>
<option label="No" value="False" default="true" />
</options>
</param>
<param field="Mode6" label="Debug" width="75px">
<options>
<option label="True" value="Debug"/>
<option label="False" value="Normal" default="true" />
</options>
</param>
</params>
</plugin>
"""
import Domoticz
import json
import colors
colorOption=""
class BasePlugin:
#enabled = False
#api = None
#gateway = None
pluginStatus = 0
# Dict of registered lights
lights = {}
CoapAdapter = None
outstandingPings = 0
nextConnect = 3
def __init__(self):
self.pluginStatus = 0
return
def unitOfUnit(self, i):
return Devices[i]
def registerDevices(self, ikeaDevices):
i = 1
if (len(Devices) == 0):
i=1
else:
i=max(Devices)+1
whiteLevelNames, whiteLevelActions = colors.wbLevelDefinitions()
WhiteOptions = {"LevelActions": whiteLevelActions, "LevelNames": whiteLevelNames, "LevelOffHidden": "true","SelectorStyle": "0"}
colorLevelNames, colorLevelActions = colors.colorLevelDefinitions()
colorOptions = {"LevelActions": colorLevelActions, "LevelNames": colorLevelNames, "LevelOffHidden": "false", "SelectorStyle": "1"}
ikeaIds = []
# Add unregistred lights
for aLight in ikeaDevices:
Domoticz.Debug ("Registering: {0}".format(json.dumps(aLight)))
devID = str(aLight['DeviceID'])
ikeaIds.append(devID)
if not devID in self.lights:
if aLight["Type"] == "Outlet":
Domoticz.Device(Name=aLight['Name'], Unit=i, Type=244, Subtype=73, Switchtype=0, Image=1, DeviceID=devID).Create()
self.lights[devID] = {"DeviceID": aLight['DeviceID'], "Unit": i}
i=i+1
if aLight["Type"] == "Light" or aLight["Type"] == "Group":
deviceType = 244
subType = 73
if not "HasRGB" in aLight:
aLight["HasRGB"] = "false"
if aLight['Dimmable']:
switchType=7
else:
switchType=0
#Basic device
Domoticz.Device(Name=aLight['Name'], Unit=i, Type=deviceType, Subtype=subType, Switchtype=switchType, DeviceID=devID).Create()
self.lights[devID] = {"DeviceID": aLight['DeviceID'], "Unit": i}
i=i+1
if str(aLight["HasRGB"]).lower() == "true":
Domoticz.Device(Name=aLight['Name'] + " - Color", Unit=i, TypeName="Selector Switch", Switchtype=18, Options=colorOptions, DeviceID=devID+":CWS").Create()
self.lights[devID+":CWS"] = {"DeviceID": devID+":CWS", "Unit": i}
i=i+1
if str(aLight['HasWB']).lower() == "true":
Domoticz.Device(Name=aLight['Name'] + " - WB", Unit=i, TypeName="Selector Switch", Switchtype=18, Options=WhiteOptions, DeviceID=devID+":WB").Create()
self.lights[devID+":WB"] = {"DeviceID": devID+":WB", "Unit": i}
i=i+1
#Remove registered lights no longer found on the gateway
for aUnit in list(Devices.keys()):
devID = str(Devices[aUnit].DeviceID)
if devID[-3:] == ":WB":
devID = devID[:-3]
if devID[-4:] == ":CWS":
devID = devID[:-4]
if not devID in ikeaIds:
Devices[aUnit].Delete()
def updateDeviceState(self, deviceState):
for aDev in deviceState:
Domoticz.Debug(str(aDev))
devID = str(aDev["DeviceID"])
targetUnit = self.lights[devID]['Unit']
nVal = 0
sVal = "0"
if str(aDev["State"]).lower() == "true":
nVal = 1
sVal = "1"
if str(aDev["State"]).lower() == "false":
nVal = 0
sVal = "0"
if "Level" in aDev:
sValInt = int((aDev["Level"]/250)*100)
if sValInt == 0:
sValInt = 1
sVal = str(sValInt)
Devices[targetUnit].Update(nValue=nVal, sValue=sVal)
if "Hex" in aDev:
if aDev["Hex"] != None:
if devID+":WB" in self.lights:
wbdevID = devID+":WB"
targetUnit = self.lights[wbdevID]['Unit']
Devices[targetUnit].Update(nValue=nVal, sValue=str(colors.wbLevelForHex(aDev['Hex'])))
if devID+":CWS" in self.lights:
wbdevID = devID+":CWS"
targetUnit = self.lights[wbdevID]['Unit']
Devices[targetUnit].Update(nValue=nVal, sValue=str(colors.colorLevelForHex(aDev['Hex'])))
def connectToAdaptor(self):
self.CoapAdapter = Domoticz.Connection(Name="Main", Transport="TCP/IP", Protocol="JSON", Address=Parameters["Address"], Port="1234")
self.CoapAdapter.Connect()
def onStart(self):
# Domoticz.Log("onStart called")
if Parameters["Mode6"] == "Debug":
Domoticz.Debugging(1)
Domoticz.Heartbeat(15)
if len(Devices) > 0:
# Some devices are already defined
for aUnit in Devices:
self.lights[Devices[aUnit].DeviceID] = {"DeviceID": Devices[aUnit].DeviceID, "Unit": aUnit}
self.connectToAdaptor()
def onStop(self):
#Domoticz.Log("onStop called")
return True
def onConnect(self, Connection, Status, Description):
#Domoticz.Log("onConnect called")
if (Status==0):
Domoticz.Log("Connected successfully to: "+Parameters["Address"])
Connection.Send(Message=json.dumps({"action":"initGateway", "observe": Parameters["Mode2"], "pollinterval": Parameters['Mode4'], "groups": Parameters["Mode3"]}).encode(encoding='utf_8'), Delay=1)
else:
Domoticz.Log("Failed to connect to IKEA tradfri COAP-adapter! Status: {0} Description: {1}".format(Status, Description))
return True
def onMessage(self, Connection, Data):
if hasattr(Data, "decode"):
# Stable API
command = json.loads(Data.decode("utf-8"))
else:
# Beta APi
command = Data
if command['status'] == "Ok":
action = command['action']
if action == "initGateway":
# Config set
Connection.Send(Message=json.dumps({"action":"getLights"}).encode(encoding='utf_8'), Delay=1)
if action == "getLights":
self.registerDevices(command['result'])
if action == "deviceUpdate":
self.updateDeviceState(command['result'])
if command['status'] == "Failed":
Domoticz.Log("Command {0} failed with error: {1}.".format(command['action'],command['error']))
Domoticz.Log(str(command))
def onCommand(self, Unit, Command, Level, Hue):
Domoticz.Debug("Command: " + str(Command)+" Level: "+str(Level)+" Type: "+str(Devices[Unit].Type)+" SubType: "+str(Devices[Unit].SubType)+" Hue: "+str(Hue))
if (Devices[Unit].Type == 244) and (Devices[Unit].SubType == 73):
if Command=="On":
self.CoapAdapter.Send(Message=json.dumps({"action": "setState", "state": "On", "deviceID": Devices[Unit].DeviceID}).encode(encoding='utf_8'))
if Command=="Off":
self.CoapAdapter.Send(Message=json.dumps({"action":"setState", "state": "Off", "deviceID": Devices[Unit].DeviceID}).encode(encoding='utf_8'))
if Command=="Set Level":
targetLevel = int(int(Level)*250/100)
self.CoapAdapter.Send(Message=json.dumps({"action":"setLevel", "deviceID": Devices[Unit].DeviceID, "level": targetLevel }).encode(encoding='utf_8'))
if (Devices[Unit].Type == 244) and (Devices[Unit].SubType == 62):
# This is a WB-device
hex = None
# [0] is the DeviceID [1] is the subType (WB/CWS)
devId = Devices[Unit].DeviceID.split(':')
if Level==0:
#Off
self.CoapAdapter.Send(Message=json.dumps({"action":"setState", "state": "Off", "deviceID": devId[0]}).encode(encoding='utf_8'))
else:
if devId[1] == "WB":
self.CoapAdapter.Send(Message=json.dumps({"action":"setHex", "deviceID": devId[0], "hex": colors.wb(Level)["Hex"]}).encode(encoding='utf_8'))
if devId[1] == "CWS":
self.CoapAdapter.Send(Message=json.dumps({"action":"setHex", "deviceID": devId[0], "hex": colors.color(Level)["Hex"]}).encode(encoding='utf_8'))
# if (Devices[Unit].Type == 241) and (Devices[Unit].SubType == 1):
# # This is a RGB-Device
# Domoticz.Debug("RGB - Command: {0} Level: {1} Hue: {2}".format(Command, Level, Hue))
def onNotification(self, Name, Subject, Text, Status, Priority, Sound, ImageFile):
Domoticz.Log("Notification: " + Name + "," + Subject + "," + Text + "," + Status + "," + str(Priority) + "," + Sound + "," + ImageFile)
def onDisconnect(self, Connection):
self.isConnected = False
Domoticz.Log("Device has disconnected")
return
def onHeartbeat(self):
if (self.CoapAdapter.Connected() == True):
pass
else:
Domoticz.Debug("Not connected - nextConnect: {0}".format(self.nextConnect))
self.nextConnect = self.nextConnect -1
if self.nextConnect <=0:
self.nextConnect = 3
self.CoapAdapter.Connect()
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))
return