2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Blinkt! lights on Raspberry Pi."""
|
2022-01-05 14:04:17 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-03-17 03:44:05 +00:00
|
|
|
import importlib
|
2022-01-05 22:35:12 +00:00
|
|
|
import logging
|
2017-04-30 18:48:54 +00:00
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.light import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_BRIGHTNESS,
|
|
|
|
ATTR_HS_COLOR,
|
2019-12-09 12:52:18 +00:00
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
SUPPORT_BRIGHTNESS,
|
2019-07-31 19:25:30 +00:00
|
|
|
SUPPORT_COLOR,
|
2020-04-26 16:49:41 +00:00
|
|
|
LightEntity,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-04-30 18:48:54 +00:00
|
|
|
from homeassistant.const import CONF_NAME
|
2022-01-05 14:04:17 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-12-09 12:52:18 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-05 14:04:17 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2018-03-18 22:00:29 +00:00
|
|
|
import homeassistant.util.color as color_util
|
2017-04-30 18:48:54 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SUPPORT_BLINKT = SUPPORT_BRIGHTNESS | SUPPORT_COLOR
|
2017-04-30 18:48:54 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "blinkt"
|
2017-04-30 18:48:54 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string}
|
|
|
|
)
|
2017-04-30 18:48:54 +00:00
|
|
|
|
2022-01-05 22:35:12 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2017-04-30 18:48:54 +00:00
|
|
|
|
2022-01-05 14:04:17 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2017-04-30 18:48:54 +00:00
|
|
|
"""Set up the Blinkt Light platform."""
|
2022-01-05 22:35:12 +00:00
|
|
|
_LOGGER.warning(
|
|
|
|
"The Blinkt! integration is deprecated and will be removed "
|
|
|
|
"in Home Assistant Core 2022.4; this integration is removed under "
|
|
|
|
"Architectural Decision Record 0019, more information can be found here: "
|
|
|
|
"https://github.com/home-assistant/architecture/blob/master/adr/0019-GPIO.md"
|
|
|
|
)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
blinkt = importlib.import_module("blinkt")
|
2017-04-30 18:48:54 +00:00
|
|
|
|
|
|
|
# ensure that the lights are off when exiting
|
|
|
|
blinkt.set_clear_on_exit()
|
|
|
|
|
2020-04-09 07:26:06 +00:00
|
|
|
name = config[CONF_NAME]
|
2017-04-30 18:48:54 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
add_entities(
|
|
|
|
[BlinktLight(blinkt, name, index) for index in range(blinkt.NUM_PIXELS)]
|
|
|
|
)
|
2017-04-30 18:48:54 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:49:41 +00:00
|
|
|
class BlinktLight(LightEntity):
|
2017-04-30 18:48:54 +00:00
|
|
|
"""Representation of a Blinkt! Light."""
|
|
|
|
|
2021-07-18 21:21:40 +00:00
|
|
|
_attr_supported_features = SUPPORT_BLINKT
|
|
|
|
_attr_should_poll = False
|
|
|
|
_attr_assumed_state = True
|
|
|
|
|
2017-11-27 20:14:03 +00:00
|
|
|
def __init__(self, blinkt, name, index):
|
2017-04-30 18:48:54 +00:00
|
|
|
"""Initialize a Blinkt Light.
|
|
|
|
|
|
|
|
Default brightness and white color.
|
|
|
|
"""
|
|
|
|
self._blinkt = blinkt
|
2021-07-18 21:21:40 +00:00
|
|
|
self._attr_name = f"{name}_{index}"
|
2017-11-27 20:14:03 +00:00
|
|
|
self._index = index
|
2021-07-18 21:21:40 +00:00
|
|
|
self._attr_is_on = False
|
|
|
|
self._attr_brightness = 255
|
|
|
|
self._attr_hs_color = [0, 0]
|
2017-04-30 18:48:54 +00:00
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Instruct the light to turn on and set correct brightness & color."""
|
2018-03-18 22:00:29 +00:00
|
|
|
if ATTR_HS_COLOR in kwargs:
|
2021-07-18 21:21:40 +00:00
|
|
|
self._attr_hs_color = kwargs[ATTR_HS_COLOR]
|
2017-05-02 16:35:23 +00:00
|
|
|
if ATTR_BRIGHTNESS in kwargs:
|
2021-07-18 21:21:40 +00:00
|
|
|
self._attr_brightness = kwargs[ATTR_BRIGHTNESS]
|
2017-04-30 18:48:54 +00:00
|
|
|
|
2021-07-18 21:21:40 +00:00
|
|
|
percent_bright = self.brightness / 255
|
|
|
|
rgb_color = color_util.color_hs_to_RGB(*self.hs_color)
|
2019-07-31 19:25:30 +00:00
|
|
|
self._blinkt.set_pixel(
|
|
|
|
self._index, rgb_color[0], rgb_color[1], rgb_color[2], percent_bright
|
|
|
|
)
|
2017-04-30 18:48:54 +00:00
|
|
|
|
|
|
|
self._blinkt.show()
|
|
|
|
|
2021-07-18 21:21:40 +00:00
|
|
|
self._attr_is_on = True
|
2017-04-30 18:48:54 +00:00
|
|
|
self.schedule_update_ha_state()
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Instruct the light to turn off."""
|
2017-11-27 20:14:03 +00:00
|
|
|
self._blinkt.set_pixel(self._index, 0, 0, 0, 0)
|
2017-04-30 18:48:54 +00:00
|
|
|
self._blinkt.show()
|
2021-07-18 21:21:40 +00:00
|
|
|
self._attr_is_on = False
|
2017-04-30 18:48:54 +00:00
|
|
|
self.schedule_update_ha_state()
|