platypush/platypush/plugins/light/hue/__init__.py

136 lines
3.9 KiB
Python
Raw Normal View History

2017-12-11 03:53:26 +01:00
import logging
2017-12-11 04:45:55 +01:00
import time
2017-12-11 03:53:26 +01:00
from phue import Bridge
from platypush.message.response import Response
2017-12-11 03:53:26 +01:00
from .. import LightPlugin
class LightHuePlugin(LightPlugin):
2017-12-18 01:10:51 +01:00
""" Philips Hue lights plugin """
2017-12-11 03:53:26 +01:00
MAX_BRI = 255
MAX_SAT = 255
MAX_HUE = 65535
2017-12-18 01:10:51 +01:00
def __init__(self, bridge, lights=None, groups=None):
"""
Constructor
Params:
bridge -- Bridge address or hostname
lights -- Lights to be controlled (default: all)
groups -- Groups to be controlled (default: all)
"""
super().__init__()
self.bridge_address = bridge
2017-12-11 03:53:26 +01:00
self.bridge = None
logging.info('Initializing Hue lights plugin - bridge: "{}"'.
format(self.bridge_address))
self.connect()
self.lights = []; self.groups = []
2017-12-18 01:10:51 +01:00
if lights:
self.lights = lights
elif groups:
self.groups = groups
self._expand_groups()
2017-12-11 03:53:26 +01:00
else:
self.lights = [l.name for l in self.bridge.lights]
logging.info('Configured lights: "{}"'. format(self.lights))
2017-12-18 01:10:51 +01:00
def _expand_groups(self):
groups = [g for g in self.bridge.groups if g.name in self.groups]
2017-12-11 03:53:26 +01:00
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')
2017-12-11 04:18:25 +01:00
self.get_scenes()
2017-12-11 03:53:26 +01:00
# 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')
2017-12-11 04:18:25 +01:00
def get_scenes(self):
2018-03-27 23:13:42 +02:00
return Response(output=self.bridge.get_scene())
def get_lights(self):
return Response(output=self.bridge.get_light())
def get_groups(self):
return Response(output=self.bridge.get_group())
2017-12-11 04:18:25 +01:00
def _exec(self, attr, *args, **kwargs):
2017-12-11 03:53:26 +01:00
try:
self.connect()
except Exception as e:
# Reset bridge connection
self.bridge = None
raise e
2017-12-11 03:53:26 +01:00
lights = []; groups = []
if 'lights' in kwargs and kwargs['lights']:
lights = kwargs['lights'].split(',') \
if isinstance(lights, str) else kwargs['lights']
2018-03-27 23:13:42 +02:00
elif 'groups' in kwargs and kwargs['groups']:
2017-12-11 03:53:26 +01:00
groups = kwargs['groups'].split(',') \
if isinstance(groups, str) else kwargs['groups']
else:
lights = self.lights
groups = self.groups
try:
2017-12-11 04:18:25 +01:00
if attr == 'scene':
self.bridge.run_scene(groups[0], kwargs['name'])
elif groups:
self.bridge.set_group(groups, attr, *args)
2017-12-11 03:53:26 +01:00
elif lights:
2017-12-11 04:18:25 +01:00
self.bridge.set_light(lights, attr, *args)
2017-12-11 03:53:26 +01:00
except Exception as e:
# Reset bridge connection
self.bridge = None
raise e
return Response(output='ok')
2017-12-11 03:53:26 +01:00
def on(self, lights=[], groups=[]):
return self._exec('on', True, lights=lights, groups=groups)
2017-12-11 03:53:26 +01:00
def off(self, lights=[], groups=[]):
2018-04-09 02:04:07 +02:00
return self._exec('on', False, lights=lights, groups=groups)
2017-12-11 03:53:26 +01:00
def bri(self, value, lights=[], groups=[]):
return self._exec('bri', int(value) % (self.MAX_BRI+1),
2017-12-11 04:18:25 +01:00
lights=lights, groups=groups)
2017-12-11 03:53:26 +01:00
def sat(self, value, lights=[], groups=[]):
return self._exec('sat', int(value) % (self.MAX_SAT+1),
2017-12-11 04:18:25 +01:00
lights=lights, groups=groups)
def hue(self, value, lights=[], groups=[]):
return self._exec('hue', int(value) % (self.MAX_HUE+1),
2017-12-11 04:18:25 +01:00
lights=lights, groups=groups)
def scene(self, name, lights=[], groups=[]):
return self._exec('scene', name=name, lights=lights, groups=groups)
2017-12-11 03:53:26 +01:00
# vim:sw=4:ts=4:et: