core/homeassistant/components/light/limitlessled.py

192 lines
6.2 KiB
Python
Raw Normal View History

2015-05-18 23:27:09 +00:00
"""
homeassistant.components.light.limitlessled
2015-08-11 12:56:56 +00:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2015-05-18 23:27:09 +00:00
Support for LimitlessLED bulbs, also known as...
2015-08-11 12:56:56 +00:00
- EasyBulb
- AppLight
- AppLamp
- MiLight
- LEDme
- dekolight
- iLight
2015-10-26 06:12:00 +00:00
https://home-assistant.io/components/light.limitlessled.html
2015-05-18 23:27:09 +00:00
"""
import logging
2015-06-13 23:42:09 +00:00
from homeassistant.const import DEVICE_DEFAULT_NAME
from homeassistant.components.light import (Light, ATTR_BRIGHTNESS,
2015-11-01 15:04:23 +00:00
ATTR_XY_COLOR, ATTR_EFFECT,
EFFECT_COLORLOOP, EFFECT_WHITE)
from homeassistant.util.color import color_RGB_to_xy
2015-05-18 23:27:09 +00:00
_LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['ledcontroller==1.1.0']
2015-05-18 23:27:09 +00:00
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
2015-05-19 01:09:34 +00:00
""" Gets the LimitlessLED lights. """
2015-07-07 07:01:46 +00:00
import ledcontroller
2015-05-18 23:27:09 +00:00
# Handle old configuration format:
bridges = config.get('bridges', [config])
for bridge_id, bridge in enumerate(bridges):
bridge['id'] = bridge_id
pool = ledcontroller.LedControllerPool([x['host'] for x in bridges])
2015-05-18 23:27:09 +00:00
lights = []
for bridge in bridges:
for i in range(1, 5):
name_key = 'group_%d_name' % i
if name_key in bridge:
group_type = bridge.get('group_%d_type' % i, 'rgbw')
lights.append(LimitlessLED.factory(pool, bridge['id'], i,
bridge[name_key],
group_type))
2015-05-18 23:27:09 +00:00
add_devices_callback(lights)
2015-06-13 23:42:09 +00:00
class LimitlessLED(Light):
2015-05-19 01:09:34 +00:00
""" Represents a LimitlessLED light """
@staticmethod
def factory(pool, controller_id, group, name, group_type):
''' Construct a Limitless LED of the appropriate type '''
if group_type == 'white':
return WhiteLimitlessLED(pool, controller_id, group, name)
elif group_type == 'rgbw':
return RGBWLimitlessLED(pool, controller_id, group, name)
# pylint: disable=too-many-arguments
def __init__(self, pool, controller_id, group, name, group_type):
self.pool = pool
self.controller_id = controller_id
2015-05-18 23:27:09 +00:00
self.group = group
self.pool.execute(self.controller_id, "set_group_type", self.group,
group_type)
2015-05-18 23:27:09 +00:00
# LimitlessLEDs don't report state, we have track it ourselves.
self.pool.execute(self.controller_id, "off", self.group)
2015-05-18 23:27:09 +00:00
self._name = name or DEVICE_DEFAULT_NAME
2015-06-13 23:42:09 +00:00
self._state = False
@property
def should_poll(self):
""" No polling needed. """
return False
@property
def name(self):
""" Returns the name of the device if any. """
return self._name
@property
def is_on(self):
""" True if device is on. """
return self._state
def turn_off(self, **kwargs):
""" Turn the device off. """
self._state = False
self.pool.execute(self.controller_id, "off", self.group)
self.update_ha_state()
class RGBWLimitlessLED(LimitlessLED):
""" Represents a RGBW LimitlessLED light """
def __init__(self, pool, controller_id, group, name):
super().__init__(pool, controller_id, group, name, 'rgbw')
self._brightness = 100
self._xy_color = color_RGB_to_xy(255, 255, 255)
# Build a color table that maps an RGB color to a color string
# recognized by LedController's set_color method
self._color_table = [(color_RGB_to_xy(*x[0]), x[1]) for x in [
((0xFF, 0xFF, 0xFF), 'white'),
((0xEE, 0x82, 0xEE), 'violet'),
((0x41, 0x69, 0xE1), 'royal_blue'),
((0x87, 0xCE, 0xFA), 'baby_blue'),
((0x00, 0xFF, 0xFF), 'aqua'),
((0x7F, 0xFF, 0xD4), 'royal_mint'),
((0x2E, 0x8B, 0x57), 'seafoam_green'),
((0x00, 0x80, 0x00), 'green'),
((0x32, 0xCD, 0x32), 'lime_green'),
((0xFF, 0xFF, 0x00), 'yellow'),
((0xDA, 0xA5, 0x20), 'yellow_orange'),
((0xFF, 0xA5, 0x00), 'orange'),
((0xFF, 0x00, 0x00), 'red'),
((0xFF, 0xC0, 0xCB), 'pink'),
((0xFF, 0x00, 0xFF), 'fusia'),
((0xDA, 0x70, 0xD6), 'lilac'),
((0xE6, 0xE6, 0xFA), 'lavendar'),
]]
2015-05-18 23:27:09 +00:00
@property
2015-06-13 23:42:09 +00:00
def brightness(self):
return self._brightness
2015-05-18 23:27:09 +00:00
@property
def color_xy(self):
return self._xy_color
def _xy_to_led_color(self, xy_color):
""" Convert an XY color to the closest LedController color string. """
def abs_dist_squared(p_0, p_1):
2015-08-10 00:30:47 +00:00
""" Returns the absolute value of the squared distance """
return abs((p_0[0] - p_1[0])**2 + (p_0[1] - p_1[1])**2)
candidates = [(abs_dist_squared(xy_color, x[0]), x[1]) for x in
self._color_table]
# First candidate in the sorted list is closest to desired color:
return sorted(candidates)[0][1]
2015-05-18 23:27:09 +00:00
def turn_on(self, **kwargs):
""" Turn the device on. """
2015-06-13 23:42:09 +00:00
self._state = True
2015-05-18 23:27:09 +00:00
if ATTR_BRIGHTNESS in kwargs:
self._brightness = kwargs[ATTR_BRIGHTNESS]
if ATTR_XY_COLOR in kwargs:
self._xy_color = kwargs[ATTR_XY_COLOR]
2015-11-01 15:04:23 +00:00
effect = kwargs.get(ATTR_EFFECT)
2015-11-02 22:24:24 +00:00
if effect == EFFECT_COLORLOOP:
self.pool.execute(self.controller_id, "disco", self.group)
2015-11-02 23:11:58 +00:00
elif effect == EFFECT_WHITE:
2015-11-02 22:24:24 +00:00
self.pool.execute(self.controller_id, "white", self.group)
2015-11-01 15:04:23 +00:00
else:
self.pool.execute(self.controller_id, "set_color",
self._xy_to_led_color(self._xy_color),
self.group)
# Brightness can be set independently of color
self.pool.execute(self.controller_id, "set_brightness",
self._brightness / 255.0, self.group)
2015-11-01 15:04:23 +00:00
2015-06-13 23:42:09 +00:00
self.update_ha_state()
2015-05-18 23:27:09 +00:00
class WhiteLimitlessLED(LimitlessLED):
""" Represents a White LimitlessLED light """
def __init__(self, pool, controller_id, group, name):
super().__init__(pool, controller_id, group, name, 'white')
def turn_on(self, **kwargs):
""" Turn the device on. """
self._state = True
self.pool.execute(self.controller_id, "on", self.group)
2015-06-13 23:42:09 +00:00
self.update_ha_state()