@@ -109,13 +109,19 @@ def on(self):
109
109
def set_onoff (self , on ):
110
110
self .__on = on
111
111
super (Light , self ).set_onoff (on )
112
+ if self .lum () == 0 and on != 0 :
113
+ self .__lum = 1 # This seems to be the default
112
114
113
115
def lum (self ):
114
116
return self .__lum
115
117
116
118
def set_luminance (self , lum , time ):
117
119
self .__lum = lum
118
120
super (Light , self ).set_luminance (lum , time )
121
+ if lum > 0 and self .__on == 0 :
122
+ self .__on = 1
123
+ elif lum == 0 and self .__on != 0 :
124
+ self .__on = 0
119
125
120
126
def temp (self ):
121
127
return self .__temp
@@ -189,17 +195,8 @@ def __init__(self, host):
189
195
self .__groups = {}
190
196
self .__lights = {}
191
197
192
- try :
193
- self .__sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
194
- except socket .error as msg :
195
- sys .stderr .write ("[ERROR] %s\n " % msg [1 ])
196
- sys .exit (1 )
197
-
198
- try :
199
- self .__sock .connect ((host , PORT ))
200
- except socket .error as msg :
201
- sys .stderr .write ("[ERROR] %s\n " % msg [1 ])
202
- sys .exit (2 )
198
+ self .__sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
199
+ self .__sock .connect ((host , PORT ))
203
200
204
201
def groups (self ):
205
202
"""Dict from group name to Group object."""
@@ -224,31 +221,59 @@ def next_seq(self):
224
221
225
222
def build_global_command (self , command , data ):
226
223
length = 6 + len (data )
224
+ try :
225
+ result = struct .pack (
226
+ "<H6B" ,
227
+ length ,
228
+ 0x02 ,
229
+ command ,
230
+ 0 ,
231
+ 0 ,
232
+ 0x7 ,
233
+ self .next_seq ()
234
+ ) + data
235
+ except TypeError :
236
+ # Decode using cp437 for python3. This is not UTF-8
237
+ result = struct .pack (
238
+ "<H6B" ,
239
+ length ,
240
+ 0x02 ,
241
+ command ,
242
+ 0 ,
243
+ 0 ,
244
+ 0x7 ,
245
+ self .next_seq ()
246
+ ) + data .decode ('cp437' )
227
247
228
- return struct .pack (
229
- "<H6B" ,
230
- length ,
231
- 0x02 ,
232
- command ,
233
- 0 ,
234
- 0 ,
235
- 0x7 ,
236
- self .next_seq ()
237
- ) + data
248
+ return result
238
249
239
250
def build_basic_command (self , flag , command , group_or_light , data ):
240
251
length = 14 + len (data )
252
+ try :
253
+ result = struct .pack (
254
+ "<H6B" ,
255
+ length ,
256
+ flag ,
257
+ command ,
258
+ 0 ,
259
+ 0 ,
260
+ 0x7 ,
261
+ self .next_seq ()
262
+ ) + group_or_light + data
263
+ except TypeError :
264
+ # Decode using cp437 for python3. This is not UTF-8
265
+ result = struct .pack (
266
+ "<H6B" ,
267
+ length ,
268
+ flag ,
269
+ command ,
270
+ 0 ,
271
+ 0 ,
272
+ 0x7 ,
273
+ self .next_seq ()
274
+ ) + group_or_light + data .decode ('cp437' )
241
275
242
- return struct .pack (
243
- "<H6B" ,
244
- length ,
245
- flag ,
246
- command ,
247
- 0 ,
248
- 0 ,
249
- 0x7 ,
250
- self .next_seq ()
251
- ) + group_or_light + data
276
+ return result
252
277
253
278
def build_command (self , command , group , data ):
254
279
# length = 14 + len(data)
@@ -377,8 +402,12 @@ def recv(self):
377
402
)
378
403
data = self .__sock .recv (expected )
379
404
expected = expected - len (data )
380
- string = string + data
381
- self .__logger .debug ('received "%s"' , binascii .hexlify (string ))
405
+ try :
406
+ string = string + data
407
+ except TypeError :
408
+ # Decode using cp437 for python3. This is not UTF-8
409
+ string = string + data .decode ('cp437' )
410
+ self .__logger .debug ('received "%s"' , string )
382
411
return data
383
412
384
413
def update_light_status (self , light ):
@@ -417,7 +446,11 @@ def update_all_light_status(self):
417
446
self .__logger .debug ("%d %d %d" , i , pos , len (payload ))
418
447
419
448
(a , addr , stat , name , extra ) = struct .unpack ("<HQ16s16sQ" , payload )
420
- name = name .replace ('\0 ' , "" )
449
+ try :
450
+ name = name .replace ('\0 ' , "" )
451
+ except TypeError :
452
+ # Decode using cp437 for python3. This is not UTF-8
453
+ name = name .decode ('cp437' ).replace ('\0 ' , "" )
421
454
422
455
self .__logger .debug ('light: %x %x %s %x' , a , addr , name , extra )
423
456
if addr in old_lights :
0 commit comments