-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathlightify.py
executable file
·402 lines (306 loc) · 11.7 KB
/
lightify.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
#!/usr/bin/python
#
# Copyright 2014 Mikael Magnusson
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# WIP Python module for Osram lightify
# Communicates with a gateway connected to the same LAN via TCP port 4000
# using a binary protocol
#
import binascii
import socket
import sys
import struct
import time
import logging
MODULE = __name__
PORT = 4000
COMMAND_ALL_LIGHT_STATUS = 0x13
COMMAND_GROUP_LIST = 0x1e
COMMAND_GROUP_INFO = 0x26
COMMAND_LUMINANCE = 0x31
COMMAND_ONOFF = 0x32
COMMAND_TEMP = 0x33
COMMAND_COLOUR = 0x36
COMMAND_LIGHT_STATUS = 0x68
# Commands
# 13 all light status (returns list of light address, light status, light name)
# 1e group list (returns list of group id, and group name)
# 26 group status (returns group id, group name, and list of light addresses)
# 31 set group luminance
# 32 set group onoff
# 33 set group temp
# 36 set group colour
# 68 light status (returns light address and light status (?))
class Luminary(object):
def __init__(self, conn, logger, name):
self.__logger = logger
self.__conn = conn
self.__name = name
def name(self):
return self.__name
def set_onoff(self, on):
data = self.__conn.build_onoff(self, on)
self.__conn.send(data)
self.__conn.recv()
def set_luminance(self, lum, time):
data = self.__conn.build_luminance(self, lum, time)
self.__conn.send(data)
self.__conn.recv()
def set_temperature(self, temp, time):
data = self.__conn.build_temp(self, temp, time)
self.__conn.send(data)
self.__conn.recv()
def set_rgb(self, r, g, b, time):
data = self.__conn.build_colour(self, r, g, b, time)
self.__conn.send(data)
self.__conn.recv()
class Light(Luminary):
def __init__(self, conn, logger, addr, name):
super(Light, self).__init__(conn, logger, name)
self.__logger = logger
self.__conn = conn
self.__addr = addr
def addr(self):
return self.__addr
def __str__(self):
return "<light: %s>" % self.name()
def update_status(self, on, lum, temp, r, g, b):
self.__on = on
self.__lum = lum
self.__temp = temp
self.__r = r
self.__g = g
self.__b = b
def on(self):
return self.__on
def set_onoff(self, on):
self.__on = on
super(Light, self).set_onoff(on)
def lum(self):
return self.__lum
def set_luminance(self, lum, time):
self.__lum = lum
super(Light, self).set_luminance(lum, time)
def temp(self):
return self.__temp
def set_temperature(self, temp, time):
self.__temp = temp
super(Light, self).set_temperature(temp, time)
def rgb(self):
return (self.red(), self.green(), self.blue())
def set_rgb(self, r, g, b, time):
self.__r = r
self.__g = g
self.__b = b
super(Light, self).set_rgb(r, g, b, time)
def red(self):
return self.__r
def green(self):
return self.__g
def blue(self):
return self.__b
def build_command(self, command, data):
return self.__conn.build_light_command(command, self, data)
class Group(Luminary):
def __init__(self, conn, logger, idx, name):
super(Group, self).__init__(conn, logger, name)
self.__conn = conn
self.__logger = logger
self.__idx = idx
self.__lights = []
def idx(self):
return self.__idx
def lights(self):
return self.__lights
def set_lights(self, lights):
self.__lights = lights
def __str__(self):
s = ""
for light_addr in self.lights():
if light_addr in self.__conn.lights():
light = self.__conn.lights()[light_addr]
else:
light = "%x" % light_addr
s = s + str(light) + " "
return "<group: %s, lights: %s>" % (self.name(), s)
def build_command(self, command, data):
return self.__conn.build_command(command, self, data)
class Lightify:
def __init__(self, host):
self.__logger = logging.getLogger(MODULE)
self.__logger.addHandler(logging.NullHandler())
self.__logger.info("Logging %s", MODULE)
self.__seq = 1
self.__groups = {}
self.__lights = {}
try:
self.__sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
sys.stderr.write("[ERROR] %s\n" % msg[1])
sys.exit(1)
try:
self.__sock.connect((host, PORT))
except socket.error, msg:
sys.stderr.write("[ERROR] %s\n" % msg[1])
sys.exit(2)
def groups(self):
"""Dict from group name to Group object."""
return self.__groups
def lights(self):
"""Dict from light addr to Light object."""
return self.__lights
def light_byname(self, name):
self.__logger.debug(len(self.lights()))
for light in self.lights().itervalues():
if light.name() == name:
return light
return None
def next_seq(self):
self.__seq = self.__seq + 1
return self.__seq
def build_global_command(self, command, data):
length = 6 + len(data)
return struct.pack("<H6B", length, 0x02, command, 0, 0, 0x7, self.next_seq()) + data
def build_basic_command(self, flag, command, group_or_light, data):
length = 14 + len(data)
return struct.pack("<H6B", length, flag, command, 0, 0, 0x7, self.next_seq()) + group_or_light + data
def build_command(self, command, group, data):
length = 14 + len(data)
return self.build_basic_command(0x02, command, struct.pack("<8B", group.idx(), 0, 0, 0, 0, 0, 0, 0), data)
def build_light_command(self, command, light, data):
length = 6 + 8 + len(data)
return self.build_basic_command(0x00, command, struct.pack("<Q", light.addr()), data)
def build_onoff(self, item, on):
return item.build_command(COMMAND_ONOFF, struct.pack("<B", on))
def build_temp(self, item, temp, time):
return item.build_command(COMMAND_TEMP, struct.pack("<HH", temp, time))
def build_luminance(self, item, luminance, time):
return item.build_command(COMMAND_LUMINANCE, struct.pack("<BH", luminance, time))
def build_colour(self, item, red, green, blue, time):
return item.build_command(COMMAND_COLOUR, struct.pack("<BBBBH", red, green, blue, 0xff, time))
def build_group_info(self, group):
return self.build_command(COMMAND_GROUP_INFO, group, "")
def build_all_light_status(self, flag):
return self.build_global_command(COMMAND_ALL_LIGHT_STATUS, struct.pack("<B", flag))
def build_light_status(self, light):
return light.build_command(COMMAND_LIGHT_STATUS, "")
def build_group_list(self):
return self.build_global_command(COMMAND_GROUP_LIST, "")
def group_list(self):
groups = {}
data = self.build_group_list()
self.send(data)
data = self.recv()
(num,) = struct.unpack("<H", data[7:9])
self.__logger.debug('Num %d', num)
for i in range(0, num):
pos = 9+i*18
payload = data[pos:pos+18]
(idx, name) = struct.unpack("<H16s", payload)
name = name.replace('\0', "")
groups[idx] = name
self.__logger.debug("Idx %d: '%s'", idx, name)
return groups
def update_group_list(self):
lst = self.group_list()
groups = {}
for (idx, name) in lst.iteritems():
group = Group(self, self.__logger, idx, name)
group.set_lights(self.group_info(group))
groups[name] = group
self.__groups = groups
def group_info(self, group):
lights = []
data = self.build_group_info(group)
self.send(data)
data = self.recv()
payload = data[7:]
(idx, name, num) = struct.unpack("<H16sB", payload[:19])
name = name.replace('\0', "")
self.__logger.debug("Idx %d: '%s' %d", idx, name, num)
for i in range(0,num):
pos = 7 + 19 + i * 8
payload = data[pos:pos+8]
(addr,) = struct.unpack("<Q", payload[:8])
self.__logger.debug("%d: %x", i, addr)
lights.append(addr)
#self.read_light_status(addr)
return lights
def send(self, data):
self.__logger.debug('sending "%s"', binascii.hexlify(data))
return self.__sock.sendall(data)
def recv(self):
lengthsize = 2
data = self.__sock.recv(lengthsize)
(length,) = struct.unpack("<H", data[:lengthsize])
self.__logger.debug(len(data))
string = ""
expected = length + 2 - len(data)
self.__logger.debug("Length %d", length)
self.__logger.debug("Expected %d", expected)
while expected > 0:
self.__logger.debug('received "%d %s"', length, binascii.hexlify(data))
data = self.__sock.recv(expected)
expected = expected - len(data)
string = string + data
self.__logger.debug('received "%s"', binascii.hexlify(string))
return data
def update_light_status(self, light):
data = self.build_light_status(light)
self.send(data)
data = self.recv()
return
(on,lum,temp,red,green,blue,h) = struct.unpack("<27x2BH4B16x", data)
self.__logger.debug('status: %0x %0x %d %0x %0x %0x %0x', on,lum,temp,red,green,blue,h)
self.__logger.debug( 'onoff: %d', on)
self.__logger.debug('temp: %d', temp)
self.__logger.debug('lum: %d', lum)
self.__logger.debug('red: %d', red)
self.__logger.debug('green: %d', green)
self.__logger.debug('blue: %d', blue)
return (on, lum, temp, red, green, blue)
def update_all_light_status(self):
data = self.build_all_light_status(1)
self.send(data)
data = self.recv()
(num,) = struct.unpack("<H", data[7:9])
self.__logger.debug('num: %d', num)
old_lights = self.__lights
new_lights = {}
status_len = 50
for i in range(0, num):
pos = 9 + i * status_len
payload = data[pos:pos+status_len]
self.__logger.debug("%d %d %d", i, pos, len(payload))
(a,addr,status,name,extra) = struct.unpack("<HQ16s16sQ", payload)
name = name.replace('\0', "")
self.__logger.debug('light: %x %x %s %x', a, addr, name, extra)
if addr in old_lights:
light = old_lights[addr]
else:
light = Light(self, self.__logger, addr, name)
(b,on,lum,temp,red,green,blue,h) = struct.unpack("<Q2BH4B", status)
self.__logger.debug('status: %x %0x', b, h)
self.__logger.debug('onoff: %d', on)
self.__logger.debug('temp: %d', temp)
self.__logger.debug('lum: %d', lum)
self.__logger.debug('red: %d', red)
self.__logger.debug('green: %d', green)
self.__logger.debug('blue: %d', blue)
light.update_status(on, lum, temp, red, green, blue)
new_lights[addr] = light
#return (on, lum, temp, red, green, blue)
self.__lights = new_lights