Skip to content

Commit 8230ea5

Browse files
Merge pull request #1 from olimpiurob/master
Decode socket data for python3
2 parents b5d512d + 89df5d7 commit 8230ea5

File tree

1 file changed

+67
-34
lines changed

1 file changed

+67
-34
lines changed

lightify/__init__.py

Lines changed: 67 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,19 @@ def on(self):
109109
def set_onoff(self, on):
110110
self.__on = on
111111
super(Light, self).set_onoff(on)
112+
if self.lum() == 0 and on != 0:
113+
self.__lum = 1 # This seems to be the default
112114

113115
def lum(self):
114116
return self.__lum
115117

116118
def set_luminance(self, lum, time):
117119
self.__lum = lum
118120
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
119125

120126
def temp(self):
121127
return self.__temp
@@ -189,17 +195,8 @@ def __init__(self, host):
189195
self.__groups = {}
190196
self.__lights = {}
191197

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))
203200

204201
def groups(self):
205202
"""Dict from group name to Group object."""
@@ -224,31 +221,59 @@ def next_seq(self):
224221

225222
def build_global_command(self, command, data):
226223
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')
227247

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
238249

239250
def build_basic_command(self, flag, command, group_or_light, data):
240251
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')
241275

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
252277

253278
def build_command(self, command, group, data):
254279
# length = 14 + len(data)
@@ -377,8 +402,12 @@ def recv(self):
377402
)
378403
data = self.__sock.recv(expected)
379404
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)
382411
return data
383412

384413
def update_light_status(self, light):
@@ -417,7 +446,11 @@ def update_all_light_status(self):
417446
self.__logger.debug("%d %d %d", i, pos, len(payload))
418447

419448
(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', "")
421454

422455
self.__logger.debug('light: %x %x %s %x', a, addr, name, extra)
423456
if addr in old_lights:

0 commit comments

Comments
 (0)