Philips Hue Scene Activation: Simplified scene lookup logic, improved error handling (#15175)

* Simplified scene lookup logic, improved error handling

* Lint
This commit is contained in:
MizterB 2018-06-27 15:22:29 -04:00 committed by Paulus Schoutsen
parent 742144f401
commit 9066ac44fe

View File

@ -124,24 +124,16 @@ class HueBridge(object):
(group for group in self.api.groups.values()
if group.name == group_name), None)
# The same scene name can exist in multiple groups.
# In this case, activate first scene that contains the
# the exact same light IDs as the group
scenes = []
for scene in self.api.scenes.values():
if scene.name == scene_name:
scenes.append(scene)
if len(scenes) == 1:
scene_id = scenes[0].id
else:
group_lights = sorted(group.lights)
for scene in scenes:
if group_lights == scene.lights:
scene_id = scene.id
break
# Additional scene logic to handle duplicate scene names across groups
scene = next(
(scene for scene in self.api.scenes.values()
if scene.name == scene_name
and group is not None
and sorted(scene.lights) == sorted(group.lights)),
None)
# If we can't find it, fetch latest info.
if not updated and (group is None or scene_id is None):
if not updated and (group is None or scene is None):
await self.api.groups.update()
await self.api.scenes.update()
await self.hue_activate_scene(call, updated=True)
@ -151,11 +143,11 @@ class HueBridge(object):
LOGGER.warning('Unable to find group %s', group_name)
return
if scene_id is None:
if scene is None:
LOGGER.warning('Unable to find scene %s', scene_name)
return
await group.set_action(scene=scene_id)
await group.set_action(scene=scene.id)
async def get_bridge(hass, host, username=None):