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
This commit is contained in:
parent
7b1e948e8d
commit
b815ccc3b8
3 changed files with 104 additions and 0 deletions
|
@ -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
|
||||
|
|
102
homeassistant/components/light/sensehat.py
Normal file
102
homeassistant/components/light/sensehat.py
Normal file
|
@ -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
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue