Skip to content
This repository was archived by the owner on Aug 6, 2025. It is now read-only.

Commit 46034d6

Browse files
committed
Don't error if device switched off
Previously get_device or the related functions would retrieve a list of devices that it contacted in the last x seconds. Then we would contact each light in turn to try and apply some sort of filtering. This would error out if the light had been switched off. This patch attempts to contact each light and skips it if it cannot be contacted. Closes #6
1 parent b8e5c1c commit 46034d6

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

lifx/client.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,12 @@ def poll_devices(self):
169169
for device in filter(lambda x:x.seen_ago > poll_delta, self._devices.values()):
170170
device.send_poll_packet()
171171

172-
def get_devices(self, max_seen=None):
172+
def get_devices(self, max_seen=None, filter=None):
173173
"""
174174
Get a list of all responding devices.
175175
176176
:param max_seen: The number of seconds since the device was last seen, defaults to 3 times the devicepoll interval.
177+
:param filter: Filter list of devices using this function
177178
"""
178179
if max_seen is None:
179180
max_seen = self._devicepolltime * MISSED_POLLS
@@ -182,6 +183,19 @@ def get_devices(self, max_seen=None):
182183

183184
devices = filter(lambda x:x.seen_ago < seen_delta, self._devices.values())
184185

186+
alive = []
187+
for d in devices:
188+
try:
189+
if filter is not None:
190+
if filter(d):
191+
alive.append(d)
192+
else:
193+
d.latency
194+
alive.append(d)
195+
except device.DeviceTimeoutError:
196+
pass
197+
devices = alive
198+
185199
# Sort by device id to ensure consistent ordering
186200
return sorted(devices, key=lambda k:k.id)
187201

@@ -227,7 +241,7 @@ def by_label(self, label):
227241
:param by_label: The label we are looking for.
228242
:returns: list -- The devices that match criteria
229243
"""
230-
return filter(lambda d: d.label == label, self.get_devices())
244+
return self.get_devices(filter=lambda d: d.label == label)
231245

232246
def by_id(self, id):
233247
"""
@@ -236,7 +250,7 @@ def by_id(self, id):
236250
:param id: The device id
237251
:returns: Device -- The device with the matching id.
238252
"""
239-
return filter(lambda d: d.id == id, self.get_devices())[0]
253+
return self.get_devices(filter=lambda d: d.id == id)
240254

241255
def by_power(self, power):
242256
"""
@@ -245,7 +259,7 @@ def by_power(self, power):
245259
:param power: True returns all devices that are on, False returns ones that are off.
246260
:returns: list -- The devices that match criteria
247261
"""
248-
return filter(lambda d: d.power == power, self.get_devices())
262+
return self.get_devices(filter=lambda d: d.power == power)
249263

250264
def by_group_id(self, group_id):
251265
"""
@@ -254,7 +268,7 @@ def by_group_id(self, group_id):
254268
:param group_id: The group id to match on each light.
255269
:returns: list -- The devices that match criteria
256270
"""
257-
return filter(lambda d: d.group_id == group_id, self.get_devices())
271+
return self.get_devices(filter=lambda d: d.group_id == group_id)
258272

259273
def by_location_id(self, location_id):
260274
"""
@@ -263,7 +277,7 @@ def by_location_id(self, location_id):
263277
:param group_id: The group id to match on each light.
264278
:returns: list -- The devices that match criteria
265279
"""
266-
return filter(lambda d: d.location_id == location_id, self.get_devices())
280+
return self.get_devices(filter=lambda d: d.location_id == location_id)
267281

268282
def __getitem__(self, key):
269283
return self.get_devices()[key]

0 commit comments

Comments
 (0)