2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Blinkstick lights."""
|
2015-10-08 08:23:19 +00:00
|
|
|
import logging
|
2015-09-29 20:32:28 +00:00
|
|
|
|
2016-08-20 22:41:14 +00:00
|
|
|
import voluptuous as vol
|
2015-09-29 20:32:28 +00:00
|
|
|
|
2016-09-12 13:52:22 +00:00
|
|
|
from homeassistant.components.light import (
|
2018-03-18 22:00:29 +00:00
|
|
|
ATTR_BRIGHTNESS, ATTR_HS_COLOR, SUPPORT_BRIGHTNESS, SUPPORT_COLOR, Light,
|
|
|
|
PLATFORM_SCHEMA)
|
2016-08-20 22:41:14 +00:00
|
|
|
from homeassistant.const import CONF_NAME
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2018-03-18 22:00:29 +00:00
|
|
|
import homeassistant.util.color as color_util
|
2015-09-29 20:32:28 +00:00
|
|
|
|
2016-09-12 13:52:22 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-08-20 22:41:14 +00:00
|
|
|
CONF_SERIAL = 'serial'
|
2016-09-12 13:52:22 +00:00
|
|
|
|
2016-08-20 22:41:14 +00:00
|
|
|
DEFAULT_NAME = 'Blinkstick'
|
2016-09-12 13:52:22 +00:00
|
|
|
|
2018-03-18 22:00:29 +00:00
|
|
|
SUPPORT_BLINKSTICK = SUPPORT_BRIGHTNESS | SUPPORT_COLOR
|
2015-09-29 20:32:28 +00:00
|
|
|
|
2016-08-20 22:41:14 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_SERIAL): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
})
|
2015-09-29 20:32:28 +00:00
|
|
|
|
2016-08-16 06:07:07 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-10 10:06:57 +00:00
|
|
|
"""Set up Blinkstick device specified by serial number."""
|
2015-11-17 08:18:42 +00:00
|
|
|
from blinkstick import blinkstick
|
|
|
|
|
2016-08-20 22:41:14 +00:00
|
|
|
name = config.get(CONF_NAME)
|
|
|
|
serial = config.get(CONF_SERIAL)
|
|
|
|
|
|
|
|
stick = blinkstick.find_by_serial(serial)
|
2015-09-29 20:32:28 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([BlinkStickLight(stick, name)], True)
|
2015-09-29 20:32:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BlinkStickLight(Light):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Representation of a BlinkStick light."""
|
2015-09-29 20:32:28 +00:00
|
|
|
|
|
|
|
def __init__(self, stick, name):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Initialize the light."""
|
2015-09-29 20:32:28 +00:00
|
|
|
self._stick = stick
|
|
|
|
self._name = name
|
|
|
|
self._serial = stick.get_serial()
|
2018-03-18 22:00:29 +00:00
|
|
|
self._hs_color = None
|
|
|
|
self._brightness = None
|
2015-09-29 20:32:28 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2017-05-01 03:10:08 +00:00
|
|
|
"""Set up polling."""
|
2015-09-29 20:32:28 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Return the name of the light."""
|
2015-09-29 20:32:28 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
2018-03-18 22:00:29 +00:00
|
|
|
def brightness(self):
|
|
|
|
"""Read back the brightness of the light."""
|
|
|
|
return self._brightness
|
|
|
|
|
|
|
|
@property
|
|
|
|
def hs_color(self):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Read back the color of the light."""
|
2018-03-18 22:00:29 +00:00
|
|
|
return self._hs_color
|
2015-09-29 20:32:28 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2018-03-18 22:00:29 +00:00
|
|
|
"""Return True if entity is on."""
|
|
|
|
return self._brightness > 0
|
2015-09-29 20:32:28 +00:00
|
|
|
|
2016-08-16 06:07:07 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_BLINKSTICK
|
|
|
|
|
2015-09-29 20:32:28 +00:00
|
|
|
def update(self):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Read back the device state."""
|
2018-03-18 22:00:29 +00:00
|
|
|
rgb_color = self._stick.get_color()
|
|
|
|
hsv = color_util.color_RGB_to_hsv(*rgb_color)
|
|
|
|
self._hs_color = hsv[:2]
|
|
|
|
self._brightness = hsv[2]
|
2015-09-29 20:32:28 +00:00
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Turn the device on."""
|
2018-03-18 22:00:29 +00:00
|
|
|
if ATTR_HS_COLOR in kwargs:
|
|
|
|
self._hs_color = kwargs[ATTR_HS_COLOR]
|
|
|
|
if ATTR_BRIGHTNESS in kwargs:
|
|
|
|
self._brightness = kwargs[ATTR_BRIGHTNESS]
|
2015-09-29 20:32:28 +00:00
|
|
|
else:
|
2018-03-18 22:00:29 +00:00
|
|
|
self._brightness = 255
|
2015-09-29 20:32:28 +00:00
|
|
|
|
2018-03-18 22:00:29 +00:00
|
|
|
rgb_color = color_util.color_hsv_to_RGB(
|
|
|
|
self._hs_color[0], self._hs_color[1], self._brightness / 255 * 100)
|
|
|
|
self._stick.set_color(
|
|
|
|
red=rgb_color[0], green=rgb_color[1], blue=rgb_color[2])
|
2015-09-29 20:32:28 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Turn the device off."""
|
2015-09-29 20:32:28 +00:00
|
|
|
self._stick.turn_off()
|