Implemented Hue lights toggle command

This commit is contained in:
Fabio Manganiello 2018-08-16 19:24:15 +02:00
parent f2d077354c
commit fc1e15504d

View file

@ -243,19 +243,21 @@ class LightHuePlugin(LightPlugin):
if 'lights' in kwargs and kwargs['lights']: if 'lights' in kwargs and kwargs['lights']:
lights = kwargs.pop('lights').split(',').strip() \ lights = kwargs.pop('lights').split(',').strip() \
if isinstance(lights, str) else kwargs.pop('lights') if isinstance(lights, str) else kwargs.pop('lights')
elif 'groups' in kwargs and kwargs['groups']: if 'groups' in kwargs and kwargs['groups']:
groups = kwargs.pop('groups').split(',').strip() \ groups = kwargs.pop('groups').split(',').strip() \
if isinstance(groups, str) else kwargs.pop('groups') if isinstance(groups, str) else kwargs.pop('groups')
else:
if not lights and not groups:
lights = self.lights lights = self.lights
groups = self.groups groups = self.groups
try: try:
if attr == 'scene': if attr == 'scene':
self.bridge.run_scene(groups[0], kwargs.pop('name')) self.bridge.run_scene(groups[0], kwargs.pop('name'))
elif groups: else:
if groups:
self.bridge.set_group(groups, attr, *args) self.bridge.set_group(groups, attr, *args)
elif lights: if lights:
self.bridge.set_light(lights, attr, *args) self.bridge.set_light(lights, attr, *args)
except Exception as e: except Exception as e:
# Reset bridge connection # Reset bridge connection
@ -333,6 +335,55 @@ class LightHuePlugin(LightPlugin):
return self._exec('on', False, lights=lights, groups=groups) return self._exec('on', False, lights=lights, groups=groups)
@action
def toggle(self, lights=[], groups=[]):
"""
Toggle lights/groups on/off.
:param lights: Lights to turn off (names or light objects). Default: plugin default lights
:param groups: Groups to turn off (names or group objects). Default: plugin default groups
"""
lights_on = []
lights_off = []
groups_on = []
groups_off = []
if groups:
all_groups = self.bridge.get_group().values()
groups_on = [
group['name'] for group in all_groups
if group['name'] in groups and group['state']['any_on'] is True
]
groups_off = [
group['name'] for group in all_groups
if group['name'] in groups and group['state']['any_on'] is False
]
if not groups and not lights:
lights = self.lights
if lights:
all_lights = self.bridge.get_light().values()
lights_on = [
light['name'] for light in all_lights
if light['name'] in lights and light['state']['on'] is True
]
lights_off = [
light['name'] for light in all_lights
if light['name'] in lights and light['state']['on'] is False
]
if lights_on or groups_on:
self._exec('on', False, lights=lights_on, groups=groups_on)
if lights_off or groups_off:
self._exec('on', True, lights=lights_off, groups=groups_off)
@action @action
def bri(self, value, lights=[], groups=[]): def bri(self, value, lights=[], groups=[]):
""" """