core/homeassistant/components/light/blinksticklight.py

76 lines
2.0 KiB
Python
Raw Normal View History

2015-09-29 20:32:28 +00:00
"""
homeassistant.components.light.blinksticklight
2015-10-08 08:23:19 +00:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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-02-19 05:27:50 +00:00
from homeassistant.components.light import ATTR_RGB_COLOR, Light
2015-09-29 20:32:28 +00:00
_LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ["blinkstick==1.1.7"]
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):
2015-10-08 08:23:19 +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):
2015-10-08 08:23:19 +00:00
""" Represents a BlinkStick light. """
2015-09-29 20:32:28 +00:00
def __init__(self, stick, name):
self._stick = stick
self._name = name
self._serial = stick.get_serial()
self._rgb_color = stick.get_color()
@property
def should_poll(self):
2015-10-08 08:23:19 +00:00
""" Polling needed. """
2015-09-29 20:32:28 +00:00
return True
@property
def name(self):
2015-10-08 08:23:19 +00:00
""" The name of the light. """
2015-09-29 20:32:28 +00:00
return self._name
@property
def rgb_color(self):
2015-10-08 08:23:19 +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):
2015-10-08 08:23:19 +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
def update(self):
""" Read back the device state """
self._rgb_color = self._stick.get_color()
def turn_on(self, **kwargs):
""" Turn the device on. """
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):
""" Turn the device off """
self._stick.turn_off()