2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Sense Hat LEDs."""
|
2017-04-30 04:34:31 +00:00
|
|
|
import logging
|
|
|
|
|
2019-12-01 05:23:09 +00:00
|
|
|
from sense_hat import SenseHat
|
2017-04-30 04:34:31 +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-01 05:23:09 +00:00
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
SUPPORT_BRIGHTNESS,
|
2019-07-31 19:25:30 +00:00
|
|
|
SUPPORT_COLOR,
|
|
|
|
Light,
|
|
|
|
)
|
2017-04-30 04:34:31 +00:00
|
|
|
from homeassistant.const import CONF_NAME
|
2019-12-01 05:23:09 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2018-03-18 22:00:29 +00:00
|
|
|
import homeassistant.util.color as color_util
|
2017-04-30 04:34:31 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SUPPORT_SENSEHAT = SUPPORT_BRIGHTNESS | SUPPORT_COLOR
|
2017-04-30 04:34:31 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "sensehat"
|
2017-04-30 04:34:31 +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 04:34:31 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-04-30 04:34:31 +00:00
|
|
|
"""Set up the Sense Hat Light platform."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2017-04-30 04:34:31 +00:00
|
|
|
sensehat = SenseHat()
|
|
|
|
|
|
|
|
name = config.get(CONF_NAME)
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([SenseHatLight(sensehat, name)])
|
2017-04-30 04:34:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SenseHatLight(Light):
|
|
|
|
"""Representation of an Sense Hat Light."""
|
|
|
|
|
|
|
|
def __init__(self, sensehat, name):
|
|
|
|
"""Initialize an Sense Hat Light.
|
|
|
|
|
|
|
|
Full brightness and white color.
|
|
|
|
"""
|
|
|
|
self._sensehat = sensehat
|
|
|
|
self._name = name
|
|
|
|
self._is_on = False
|
|
|
|
self._brightness = 255
|
2018-03-18 22:00:29 +00:00
|
|
|
self._hs_color = [0, 0]
|
2017-04-30 04:34:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the display name of this light."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Read back the brightness of the light."""
|
2017-04-30 04:34:31 +00:00
|
|
|
return self._brightness
|
|
|
|
|
|
|
|
@property
|
2018-03-18 22:00:29 +00:00
|
|
|
def hs_color(self):
|
|
|
|
"""Read back the color of the light."""
|
|
|
|
return self._hs_color
|
2017-04-30 04:34:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_SENSEHAT
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if light is on."""
|
|
|
|
return self._is_on
|
|
|
|
|
2017-04-30 04:39:27 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""Return if we should poll this device."""
|
|
|
|
return False
|
2017-04-30 05:42:12 +00:00
|
|
|
|
2017-04-30 04:39:27 +00:00
|
|
|
@property
|
|
|
|
def assumed_state(self) -> bool:
|
|
|
|
"""Return True if unable to access real state of the entity."""
|
|
|
|
return True
|
2017-04-30 05:42:12 +00:00
|
|
|
|
2017-04-30 04:34:31 +00:00
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Instruct the light to turn on and set correct brightness & color."""
|
2017-05-03 00:02:13 +00:00
|
|
|
if ATTR_BRIGHTNESS in kwargs:
|
|
|
|
self._brightness = kwargs[ATTR_BRIGHTNESS]
|
2017-04-30 04:34:31 +00:00
|
|
|
|
2018-03-18 22:00:29 +00:00
|
|
|
if ATTR_HS_COLOR in kwargs:
|
|
|
|
self._hs_color = kwargs[ATTR_HS_COLOR]
|
2017-04-30 04:34:31 +00:00
|
|
|
|
2018-03-18 22:00:29 +00:00
|
|
|
rgb = color_util.color_hsv_to_RGB(
|
2019-07-31 19:25:30 +00:00
|
|
|
self._hs_color[0], self._hs_color[1], self._brightness / 255 * 100
|
|
|
|
)
|
2018-03-18 22:00:29 +00:00
|
|
|
self._sensehat.clear(*rgb)
|
2017-04-30 04:34:31 +00:00
|
|
|
|
|
|
|
self._is_on = True
|
2017-04-30 04:40:10 +00:00
|
|
|
self.schedule_update_ha_state()
|
2017-04-30 04:34:31 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Instruct the light to turn off."""
|
|
|
|
self._sensehat.clear()
|
|
|
|
self._is_on = False
|
2017-04-30 04:40:10 +00:00
|
|
|
self.schedule_update_ha_state()
|