From b815ccc3b8dad09f5b18e545952d717caffdf61f Mon Sep 17 00:00:00 2001 From: Gergely Imreh Date: Sun, 30 Apr 2017 05:34:31 +0100 Subject: [PATCH] light.sensehat: plugin to control the 8x8 LED matrix on a Sense hat (#7365) * light.sensehat: adding plugin to control the 8x8 LED matrix on a Sense Hat * add new .coveragerc entry * light.sensehat: formatting and removing unused import * light.sensehat: add to requirements list * light.sensehat: update docstrings to the linter's specs * light.sensehat: add a bit more docstring --- .coveragerc | 1 + homeassistant/components/light/sensehat.py | 102 +++++++++++++++++++++ requirements_all.txt | 1 + 3 files changed, 104 insertions(+) create mode 100644 homeassistant/components/light/sensehat.py diff --git a/.coveragerc b/.coveragerc index 90caadc23b5..ed81479a645 100644 --- a/.coveragerc +++ b/.coveragerc @@ -243,6 +243,7 @@ omit = homeassistant/components/light/osramlightify.py homeassistant/components/light/rpi_gpio_pwm.py homeassistant/components/light/piglow.py + homeassistant/components/light/sensehat.py homeassistant/components/light/tikteck.py homeassistant/components/light/tradfri.py homeassistant/components/light/x10.py diff --git a/homeassistant/components/light/sensehat.py b/homeassistant/components/light/sensehat.py new file mode 100644 index 00000000000..acda0dc206c --- /dev/null +++ b/homeassistant/components/light/sensehat.py @@ -0,0 +1,102 @@ +""" +Support for Sense Hat LEDs. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/light.sensehat/ +""" +import logging + +import voluptuous as vol + +import homeassistant.helpers.config_validation as cv +from homeassistant.components.light import ( + ATTR_BRIGHTNESS, SUPPORT_BRIGHTNESS, ATTR_RGB_COLOR, SUPPORT_RGB_COLOR, + Light, PLATFORM_SCHEMA) +from homeassistant.const import CONF_NAME + +REQUIREMENTS = ['sense-hat==2.2.0'] + +_LOGGER = logging.getLogger(__name__) + +SUPPORT_SENSEHAT = (SUPPORT_BRIGHTNESS | SUPPORT_RGB_COLOR) + +DEFAULT_NAME = 'sensehat' + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, +}) + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Set up the Sense Hat Light platform.""" + from sense_hat import SenseHat + sensehat = SenseHat() + + name = config.get(CONF_NAME) + + add_devices([SenseHatLight(sensehat, name)]) + + +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 + self._rgb_color = [255, 255, 255] + + @property + def name(self): + """Return the display name of this light.""" + return self._name + + @property + def brightness(self): + """Read back the brightness of the light. + + Returns integer in the range of 1-255. + """ + return self._brightness + + @property + def rgb_color(self): + """Read back the color of the light. + + Returns [r, g, b] list with values in range of 0-255. + """ + return self._rgb_color + + @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 + + def turn_on(self, **kwargs): + """Instruct the light to turn on and set correct brightness & color.""" + self._brightness = kwargs.get(ATTR_BRIGHTNESS, 255) + percent_bright = (self._brightness / 255) + + if ATTR_RGB_COLOR in kwargs: + self._rgb_color = kwargs[ATTR_RGB_COLOR] + + self._sensehat.clear(int(self._rgb_color[0] * percent_bright), + int(self._rgb_color[1] * percent_bright), + int(self._rgb_color[2] * percent_bright)) + + self._is_on = True + + def turn_off(self, **kwargs): + """Instruct the light to turn off.""" + self._sensehat.clear() + self._is_on = False diff --git a/requirements_all.txt b/requirements_all.txt index 8b5ad9bc775..2cb86b3435c 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -723,6 +723,7 @@ scsgate==0.1.0 # homeassistant.components.notify.sendgrid sendgrid==4.0.0 +# homeassistant.components.light.sensehat # homeassistant.components.sensor.sensehat sense-hat==2.2.0