From 1e75d2bda13ecf199085059863e967d53d9be074 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Mon, 11 Dec 2017 03:53:26 +0100 Subject: [PATCH] Implemented Philips Hue plugin --- runbullet/plugins/__init__.py | 7 ++ runbullet/plugins/light/__init__.py | 10 --- runbullet/plugins/light/hue/__init__.py | 111 ++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 10 deletions(-) create mode 100644 runbullet/plugins/light/hue/__init__.py diff --git a/runbullet/plugins/__init__.py b/runbullet/plugins/__init__.py index cd9f3212e8..dfc1e2389e 100644 --- a/runbullet/plugins/__init__.py +++ b/runbullet/plugins/__init__.py @@ -1,9 +1,11 @@ import os import sys +import logging class Plugin(object): def __init__(self, config): self.config = config + self._set_logging() for cls in reversed(self.__class__.mro()): if cls is not object: @@ -12,6 +14,11 @@ class Plugin(object): except AttributeError as e: pass + def _set_logging(self): + if 'logging' in self.config: + logging.basicConfig(level=getattr(logging, self.config['logging'])) + else: + logging.basicConfig(level=logging.INFO) def run(self, method, *args, **kwargs): res = getattr(self, method)(*args, **kwargs) diff --git a/runbullet/plugins/light/__init__.py b/runbullet/plugins/light/__init__.py index bdb469a835..0dd02f42e2 100644 --- a/runbullet/plugins/light/__init__.py +++ b/runbullet/plugins/light/__init__.py @@ -1,16 +1,6 @@ from .. import Plugin class LightPlugin(Plugin): - def run(self, args): - if 'on' in args and args['on']: - self.on() - elif 'off' in args and args['off']: - self.off() - elif 'toggle' in args and args['toggle']: - self.toggle() - - return self.status() - def on(self): raise NotImplementedError() diff --git a/runbullet/plugins/light/hue/__init__.py b/runbullet/plugins/light/hue/__init__.py new file mode 100644 index 0000000000..2dff985ea2 --- /dev/null +++ b/runbullet/plugins/light/hue/__init__.py @@ -0,0 +1,111 @@ +import logging + +from phue import Bridge + +from .. import LightPlugin + +class LightHuePlugin(LightPlugin): + """ Python dependencies """ + _requires = [ + 'phue' + ] + + MAX_BRI = 255 + MAX_SAT = 255 + MAX_HUE = 65535 + + def _init(self): + self.bridge_address = self.config['bridge'] + self.bridge = None + logging.info('Initializing Hue lights plugin - bridge: "{}"'. + format(self.bridge_address)) + + self.connect() + self.lights = []; self.groups = [] + + if 'lights' in self.config: + self.lights = self.config['lights'] + elif 'groups' in self.config: + self.groups = self.config['groups'] + self._expand_groups(self.groups) + else: + self.lights = [l.name for l in self.bridge.lights] + + logging.info('Configured lights: "{}"'. format(self.lights)) + + def _expand_groups(self, group_names): + groups = [g for g in self.bridge.groups + if g.name in group_names] + + for g in groups: + self.lights.extend([l.name for l in g.lights]) + + def connect(self): + # Lazy init + if not self.bridge: + self.bridge = Bridge(self.bridge_address) + logging.info('Bridge connected') + + # uncomment these lines if you're running huectrl for + # the first time and you need to pair it to the switch + + # self.bridge.connect() + # self.bridge.get_api() + else: + logging.info('Bridge already connected') + + + def _execute(self, setting, *args, **kwargs): + try: + self.connect() + except Exception as e: + logging.exception(e) + # Reset bridge connection + self.bridge = None + return + + lights = []; groups = [] + if 'lights' in kwargs and kwargs['lights']: + lights = kwargs['lights'].split(',') \ + if isinstance(lights, str) else kwargs['lights'] + elif 'groups' in kwargs and kwargs['lights']: + groups = kwargs['groups'].split(',') \ + if isinstance(groups, str) else kwargs['groups'] + else: + lights = self.lights + groups = self.groups + + logging.info('[Setting: {}] [Values: {}] [Lights: {}] [Groups: {}]'. + format(setting, args, lights, groups)) + + try: + if groups: + self.bridge.set_group(groups, setting, *args) + elif lights: + self.bridge.set_light(lights, setting, *args) + except Exception as e: + print(e) + logging.exception(e) + # Reset bridge connection + self.bridge = None + + def on(self, lights=[], groups=[]): + self._execute('on', True, lights=lights, groups=groups) + + def off(self, lights=[], groups=[]): + self._execute('on', False, lights=lights, groups=groups) + + def bri(self, value, lights=[], groups=[]): + self._execute('bri', int(value) % (MAX_BRI+1), lights=lights, groups=groups) + + def sat(self, value, lights=[], groups=[]): + self._execute('sat', int(value) % (MAX_SAT+1), lights=lights, groups=groups) + + def hue(self, value, lights=[], groups=[]): + self._execute('hue', int(value) % (MAX_HUE+1), lights=lights, groups=groups) + + def status(self): + return [''] + +# vim:sw=4:ts=4:et: +