core/homeassistant/components/light/limitlessled.py

140 lines
4.0 KiB
Python
Raw Normal View History

2015-05-18 23:27:09 +00:00
"""
homeassistant.components.light.limitlessled
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Support for LimitlessLED bulbs, also known as...
EasyBulb
AppLight
AppLamp
MiLight
LEDme
dekolight
iLight
light:
platform: limitlessled
host: 192.168.1.10
group_1_name: Living Room
group_2_name: Bedroom
group_3_name: Office
group_4_name: Kitchen
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,
ATTR_XY_COLOR)
from homeassistant.util.color import color_RGB_to_xy
2015-05-18 23:27:09 +00:00
_LOGGER = logging.getLogger(__name__)
2015-07-07 07:01:46 +00:00
REQUIREMENTS = ['ledcontroller>=1.0.7']
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
led = ledcontroller.LedController(config['host'])
lights = []
for i in range(1, 5):
if 'group_%d_name' % (i) in config:
2015-06-13 23:42:09 +00:00
lights.append(LimitlessLED(led, i, config['group_%d_name' % (i)]))
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 """
2015-05-19 01:31:30 +00:00
def __init__(self, led, group, name):
2015-05-18 23:27:09 +00:00
self.led = led
self.group = group
# LimitlessLEDs don't report state, we have track it ourselves.
self.led.off(self.group)
self._name = name or DEVICE_DEFAULT_NAME
2015-06-13 23:42:09 +00:00
self._state = False
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
def should_poll(self):
""" No polling needed for a demo light. """
return False
@property
def name(self):
""" Returns the name of the device if any. """
return self._name
@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
@property
def is_on(self):
""" True if device is on. """
2015-06-13 23:42:09 +00:00
return self._state
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]
self.led.set_color(self._xy_to_led_color(self._xy_color), self.group)
self.led.set_brightness(self._brightness / 255.0, self.group)
2015-06-13 23:42:09 +00:00
self.update_ha_state()
2015-05-18 23:27:09 +00:00
def turn_off(self, **kwargs):
""" Turn the device off. """
2015-06-13 23:42:09 +00:00
self._state = False
2015-05-18 23:27:09 +00:00
self.led.off(self.group)
2015-06-13 23:42:09 +00:00
self.update_ha_state()