core/homeassistant/components/light/blinksticklight.py

84 lines
2.2 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
from homeassistant.components.light import (ATTR_RGB_COLOR, SUPPORT_RGB_COLOR,
Light)
2015-09-29 20:32:28 +00:00
_LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ["blinkstick==1.1.7"]
SUPPORT_BLINKSTICK = SUPPORT_RGB_COLOR
2015-10-08 08:23:19 +00:00
# pylint: disable=unused-argument
2015-09-29 20:32:28 +00:00
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
2016-03-07 21:08:21 +00:00
"""Add device specified by serial number."""
2015-11-17 08:18:42 +00:00
from blinkstick import blinkstick
2015-09-29 20:32:28 +00:00
stick = blinkstick.find_by_serial(config['serial'])
add_devices_callback([BlinkStickLight(stick, config['name'])])
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):
2016-03-07 21:08:21 +00:00
"""Polling needed."""
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()