From fc1e15504dcc6d24e29a9cc5da7c6136dded1dad Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 16 Aug 2018 19:24:15 +0200 Subject: [PATCH] Implemented Hue lights toggle command --- platypush/plugins/light/hue/__init__.py | 63 ++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 6 deletions(-) diff --git a/platypush/plugins/light/hue/__init__.py b/platypush/plugins/light/hue/__init__.py index d99dcd9c5..8536e70d8 100644 --- a/platypush/plugins/light/hue/__init__.py +++ b/platypush/plugins/light/hue/__init__.py @@ -243,20 +243,22 @@ class LightHuePlugin(LightPlugin): if 'lights' in kwargs and kwargs['lights']: lights = kwargs.pop('lights').split(',').strip() \ 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() \ if isinstance(groups, str) else kwargs.pop('groups') - else: + + if not lights and not groups: lights = self.lights groups = self.groups try: if attr == 'scene': self.bridge.run_scene(groups[0], kwargs.pop('name')) - elif groups: - self.bridge.set_group(groups, attr, *args) - elif lights: - self.bridge.set_light(lights, attr, *args) + else: + if groups: + self.bridge.set_group(groups, attr, *args) + if lights: + self.bridge.set_light(lights, attr, *args) except Exception as e: # Reset bridge connection self.bridge = None @@ -333,6 +335,55 @@ class LightHuePlugin(LightPlugin): 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 def bri(self, value, lights=[], groups=[]): """