core/homeassistant/components/blinksticklight/light.py

86 lines
2.4 KiB
Python
Raw Normal View History

"""Support for Blinkstick lights."""
from __future__ import annotations
from blinkstick import blinkstick
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 (
2019-07-31 19:25:30 +00:00
ATTR_BRIGHTNESS,
ATTR_HS_COLOR,
PLATFORM_SCHEMA,
ColorMode,
2020-04-26 16:49:41 +00:00
LightEntity,
2019-07-31 19:25:30 +00:00
)
2016-08-20 22:41:14 +00:00
from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant
2016-08-20 22:41:14 +00:00
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
import homeassistant.util.color as color_util
2015-09-29 20:32:28 +00:00
2019-07-31 19:25:30 +00:00
CONF_SERIAL = "serial"
2019-07-31 19:25:30 +00:00
DEFAULT_NAME = "Blinkstick"
2019-07-31 19:25:30 +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
def setup_platform(
hass: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up Blinkstick device specified by serial number."""
2015-11-17 08:18:42 +00:00
name = config[CONF_NAME]
serial = config[CONF_SERIAL]
2016-08-20 22:41:14 +00:00
stick = blinkstick.find_by_serial(serial)
2015-09-29 20:32:28 +00:00
add_entities([BlinkStickLight(stick, name)], True)
2015-09-29 20:32:28 +00:00
2020-04-26 16:49:41 +00:00
class BlinkStickLight(LightEntity):
2016-03-07 21:08:21 +00:00
"""Representation of a BlinkStick light."""
2015-09-29 20:32:28 +00:00
_attr_color_mode = ColorMode.HS
_attr_supported_color_modes = {ColorMode.HS}
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._attr_name = name
2015-09-29 20:32:28 +00:00
def update(self):
2016-03-07 21:08:21 +00:00
"""Read back the device state."""
rgb_color = self._stick.get_color()
hsv = color_util.color_RGB_to_hsv(*rgb_color)
self._attr_hs_color = hsv[:2]
self._attr_brightness = hsv[2]
self._attr_is_on = self.brightness > 0
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."""
if ATTR_HS_COLOR in kwargs:
self._attr_hs_color = kwargs[ATTR_HS_COLOR]
if ATTR_BRIGHTNESS in kwargs:
self._attr_brightness = kwargs[ATTR_BRIGHTNESS]
2015-09-29 20:32:28 +00:00
else:
self._attr_brightness = 255
self._attr_is_on = self.brightness > 0
2015-09-29 20:32:28 +00:00
rgb_color = color_util.color_hsv_to_RGB(
self.hs_color[0], self.hs_color[1], self.brightness / 255 * 100
2019-07-31 19:25:30 +00:00
)
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()