core/homeassistant/components/light/blinksticklight.py

98 lines
2.5 KiB
Python
Raw Normal View History

2015-09-29 20:32:28 +00:00
"""
Support for Blinkstick lights.
2015-10-08 08:23:19 +00:00
For more details about this platform, please refer to the documentation at
2015-11-09 12:12:18 +00:00
https://home-assistant.io/components/light.blinksticklight/
2015-09-29 20:32:28 +00:00
"""
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
from homeassistant.components.light import (
ATTR_RGB_COLOR, SUPPORT_RGB_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
2015-09-29 20:32:28 +00:00
REQUIREMENTS = ['blinkstick==1.1.8']
_LOGGER = logging.getLogger(__name__)
2016-08-20 22:41:14 +00:00
CONF_SERIAL = 'serial'
2016-08-20 22:41:14 +00:00
DEFAULT_NAME = 'Blinkstick'
2016-08-20 22:41:14 +00:00
SUPPORT_BLINKSTICK = SUPPORT_RGB_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
2015-10-08 08:23:19 +00:00
# pylint: disable=unused-argument
2016-08-20 22:41:14 +00:00
def setup_platform(hass, config, add_devices, discovery_info=None):
"""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
2016-08-20 22:41:14 +00:00
add_devices([BlinkStickLight(stick, name)])
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()
self._rgb_color = stick.get_color()
@property
def should_poll(self):
"""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
def rgb_color(self):
2016-03-07 21:08:21 +00:00
"""Read back the color of the light."""
2015-09-29 20:32:28 +00:00
return self._rgb_color
@property
def is_on(self):
2016-03-07 21:08:21 +00:00
"""Check whether any of the LEDs colors are non-zero."""
2015-09-29 20:32:28 +00:00
return sum(self._rgb_color) > 0
@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."""
2015-09-29 20:32:28 +00:00
self._rgb_color = self._stick.get_color()
def turn_on(self, **kwargs):
2016-03-07 21:08:21 +00:00
"""Turn the device on."""
2015-09-29 20:32:28 +00:00
if ATTR_RGB_COLOR in kwargs:
self._rgb_color = kwargs[ATTR_RGB_COLOR]
else:
self._rgb_color = [255, 255, 255]
self._stick.set_color(red=self._rgb_color[0],
green=self._rgb_color[1],
blue=self._rgb_color[2])
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()