Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

Commit

Permalink
Don't error if device switched off
Browse files Browse the repository at this point in the history
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 smarthall#6
  • Loading branch information
brianmay committed Mar 24, 2016
1 parent b8e5c1c commit 46034d6
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions lifx/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,12 @@ def poll_devices(self):
for device in filter(lambda x:x.seen_ago > poll_delta, self._devices.values()):
device.send_poll_packet()

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

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

alive = []
for d in devices:
try:
if filter is not None:
if filter(d):
alive.append(d)
else:
d.latency
alive.append(d)
except device.DeviceTimeoutError:
pass
devices = alive

# Sort by device id to ensure consistent ordering
return sorted(devices, key=lambda k:k.id)

Expand Down Expand Up @@ -227,7 +241,7 @@ def by_label(self, label):
:param by_label: The label we are looking for.
:returns: list -- The devices that match criteria
"""
return filter(lambda d: d.label == label, self.get_devices())
return self.get_devices(filter=lambda d: d.label == label)

def by_id(self, id):
"""
Expand All @@ -236,7 +250,7 @@ def by_id(self, id):
:param id: The device id
:returns: Device -- The device with the matching id.
"""
return filter(lambda d: d.id == id, self.get_devices())[0]
return self.get_devices(filter=lambda d: d.id == id)

def by_power(self, power):
"""
Expand All @@ -245,7 +259,7 @@ def by_power(self, power):
:param power: True returns all devices that are on, False returns ones that are off.
:returns: list -- The devices that match criteria
"""
return filter(lambda d: d.power == power, self.get_devices())
return self.get_devices(filter=lambda d: d.power == power)

def by_group_id(self, group_id):
"""
Expand All @@ -254,7 +268,7 @@ def by_group_id(self, group_id):
:param group_id: The group id to match on each light.
:returns: list -- The devices that match criteria
"""
return filter(lambda d: d.group_id == group_id, self.get_devices())
return self.get_devices(filter=lambda d: d.group_id == group_id)

def by_location_id(self, location_id):
"""
Expand All @@ -263,7 +277,7 @@ def by_location_id(self, location_id):
:param group_id: The group id to match on each light.
:returns: list -- The devices that match criteria
"""
return filter(lambda d: d.location_id == location_id, self.get_devices())
return self.get_devices(filter=lambda d: d.location_id == location_id)

def __getitem__(self, key):
return self.get_devices()[key]
Expand Down

0 comments on commit 46034d6

Please sign in to comment.