From 38d9dc996ba1bf0386b7db0672d1d44f7a6d9f0f Mon Sep 17 00:00:00 2001 From: Aaron Polley Date: Tue, 24 Jan 2017 08:41:33 +0000 Subject: [PATCH] Piglow support (#5302) * Add support for Piglow * Updated coverage and requirements * Add support for Piglow * Updated coverage and requirements * Fix linting errors * Fix linting errors * Remove trailing whitespace * Shorter lines * Remove trailing whitespace * Update piglow.py * Pinned piglow version * Remove unused method * Remove unused imports * Fix lint errors * Update requirements all * Updated Piglow to allow the component name to be changed * Fix imports * Pass in name * The piglow platform now fails if it cannot detect the piglow device * Tidy subprocess import --- .coveragerc | 1 + homeassistant/components/light/piglow.py | 104 +++++++++++++++++++++++ requirements_all.txt | 3 + 3 files changed, 108 insertions(+) create mode 100644 homeassistant/components/light/piglow.py diff --git a/.coveragerc b/.coveragerc index 47f602072c0..ac97522450f 100644 --- a/.coveragerc +++ b/.coveragerc @@ -201,6 +201,7 @@ omit = homeassistant/components/light/tikteck.py homeassistant/components/light/x10.py homeassistant/components/light/yeelight.py + homeassistant/components/light/piglow.py homeassistant/components/light/zengge.py homeassistant/components/lirc.py homeassistant/components/media_player/anthemav.py diff --git a/homeassistant/components/light/piglow.py b/homeassistant/components/light/piglow.py new file mode 100644 index 00000000000..d4e9c9ed106 --- /dev/null +++ b/homeassistant/components/light/piglow.py @@ -0,0 +1,104 @@ +""" +Support for Piglow LED's. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/light.piglow/ +""" +import logging +import subprocess + +import voluptuous as vol + +# Import the device class from the component that you want to support +from homeassistant.components.light import ( + ATTR_BRIGHTNESS, SUPPORT_BRIGHTNESS, + ATTR_RGB_COLOR, SUPPORT_RGB_COLOR, + Light, PLATFORM_SCHEMA) +from homeassistant.const import CONF_NAME +import homeassistant.helpers.config_validation as cv + +# Home Assistant depends on 3rd party packages for API specific code. +REQUIREMENTS = ['piglow==1.2.4'] + +_LOGGER = logging.getLogger(__name__) + +SUPPORT_PIGLOW = (SUPPORT_BRIGHTNESS | SUPPORT_RGB_COLOR) + +DEFAULT_NAME = 'Piglow' + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, +}) + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Setup the Piglow Light platform.""" + import piglow + + if subprocess.getoutput("i2cdetect -q -y 1 | grep -o 54") != '54': + _LOGGER.error("A Piglow device was not found") + return False + + name = config.get(CONF_NAME) + + # Add devices + add_devices([PiglowLight(piglow, name)]) + + +class PiglowLight(Light): + """Representation of an Piglow Light.""" + + def __init__(self, piglow, name): + """Initialize an PiglowLight.""" + self._piglow = piglow + 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): + """Brightness of the light (an integer in the range 1-255).""" + return self._brightness + + @property + def rgb_color(self): + """Read back the color of the light.""" + return self._rgb_color + + @property + def supported_features(self): + """Flag supported features.""" + return SUPPORT_PIGLOW + + @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.""" + self._piglow.clear() + 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._piglow.red(int(self._rgb_color[0] * percent_bright)) + self._piglow.green(int(self._rgb_color[1] * percent_bright)) + self._piglow.blue(int(self._rgb_color[2] * percent_bright)) + else: + self._piglow.all(self._brightness) + self._piglow.show() + self._is_on = True + + def turn_off(self, **kwargs): + """Instruct the light to turn off.""" + self._piglow.clear() + self._piglow.show() + self._is_on = False diff --git a/requirements_all.txt b/requirements_all.txt index 272fbfd8751..7ba42c209f4 100755 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -348,6 +348,9 @@ pexpect==4.0.1 # homeassistant.components.light.hue phue==0.9 +# homeassistant.components.light.piglow +piglow==1.2.4 + # homeassistant.components.pilight pilight==0.1.1