From 75242e67a793156a768eed94634751d015db7686 Mon Sep 17 00:00:00 2001 From: Patrik Date: Sun, 16 Apr 2017 20:35:52 +0200 Subject: [PATCH] IKEA Tradfri Gateway: added support for RGB (#7115) * After rebase and all fixes * Added color_rgb_to_hex to util.color * Added test_color_rgb_to_hex * Changed reference to color_rgb_to_hex * Bumped to pytradfri 0.5, having support for retry * Bumped to pytradfri 0.5, having support for retry * Bumped to pytradfri 0.5, having support for retry * Bumped to pytradfri 0.5, having support for retry * Rolled back to 0.4 * Rolled back to 0.4 --- homeassistant/components/light/tradfri.py | 42 ++++++++++++++++------- homeassistant/util/color.py | 5 +++ tests/util/test_color.py | 5 +++ 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/light/tradfri.py b/homeassistant/components/light/tradfri.py index 1b2e42b5b99..4a2641dc338 100644 --- a/homeassistant/components/light/tradfri.py +++ b/homeassistant/components/light/tradfri.py @@ -5,16 +5,16 @@ import logging 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, Light, PLATFORM_SCHEMA -from homeassistant.const import CONF_HOST, CONF_API_KEY +import homeassistant.util.color as color_util +from homeassistant.components.light import ( + ATTR_BRIGHTNESS, ATTR_RGB_COLOR, Light, + PLATFORM_SCHEMA, SUPPORT_BRIGHTNESS, SUPPORT_RGB_COLOR) +from homeassistant.const import CONF_API_KEY, CONF_HOST import homeassistant.helpers.config_validation as cv +SUPPORTED_FEATURES = (SUPPORT_BRIGHTNESS | SUPPORT_RGB_COLOR) -SUPPORTED_FEATURES = (SUPPORT_BRIGHTNESS) - -# Home Assistant depends on 3rd party packages for API specific code. +# https://github.com/ggravlingen/pytradfri REQUIREMENTS = ['pytradfri==0.4'] _LOGGER = logging.getLogger(__name__) @@ -31,7 +31,6 @@ def setup_platform(hass, config, add_devices, discovery_info=None): import pytradfri # Assign configuration variables. - # The configuration check takes care they are present. host = config.get(CONF_HOST) securitycode = config.get(CONF_API_KEY) @@ -58,7 +57,9 @@ class IKEATradfri(Light): self._light_control = light.light_control self._light_data = light.light_control.lights[0] self._name = light.name + self._brightness = None + self._rgb_color = None @property def supported_features(self): @@ -80,21 +81,36 @@ class IKEATradfri(Light): """Brightness of the light (an integer in the range 1-255).""" return self._light_data.dimmer + @property + def rgb_color(self): + """RGB color of the light.""" + return self._rgb_color + def turn_off(self, **kwargs): """Instruct the light to turn off.""" return self._light_control.set_state(False) def turn_on(self, **kwargs): - """Instruct the light to turn on.""" + """ + Instruct the light to turn on. + + After adding "self._light_data.hexcolor is not None" + for ATTR_RGB_COLOR, this also supports Philips Hue bulbs. + """ if ATTR_BRIGHTNESS in kwargs: self._light.light_control.set_dimmer(kwargs.get(ATTR_BRIGHTNESS)) + if ATTR_RGB_COLOR in kwargs and self._light_data.hex_color is not None: + self._light.light_control.set_hex_color( + color_util.color_rgb_to_hex(*kwargs[ATTR_RGB_COLOR])) else: self._light.light_control.set_state(True) def update(self): - """Fetch new state data for this light. - - This is the only method that should fetch new data for Home Assistant. - """ + """Fetch new state data for this light.""" self._light.update() self._brightness = self._light_data.dimmer + + # Handle Hue lights paired with the gatway + if self._light_data.hex_color is not None: + self._rgb_color = color_util.rgb_hex_to_rgb_list( + self._light_data.hex_color) diff --git a/homeassistant/util/color.py b/homeassistant/util/color.py index 52d7a9f63aa..26a05bbc0bc 100644 --- a/homeassistant/util/color.py +++ b/homeassistant/util/color.py @@ -305,6 +305,11 @@ def color_rgbw_to_rgb(r, g, b, w): return _match_max_scale((r, g, b, w), rgb) +def color_rgb_to_hex(r, g, b): + """Return a RGB color from a hex color string.""" + return '{0:02x}{1:02x}{2:02x}'.format(r, g, b) + + def rgb_hex_to_rgb_list(hex_string): """Return an RGB color value list from a hex color string.""" return [int(hex_string[i:i + len(hex_string) // 3], 16) diff --git a/tests/util/test_color.py b/tests/util/test_color.py index d7560d4f7bf..97e5d0b7927 100644 --- a/tests/util/test_color.py +++ b/tests/util/test_color.py @@ -173,6 +173,11 @@ class TestColorUtil(unittest.TestCase): self.assertEqual((127, 127, 127), color_util.color_rgbw_to_rgb(0, 0, 0, 127)) + def test_color_rgb_to_hex(self): + """Test color_rgb_to_hex.""" + self.assertEqual('000000', + color_util.color_rgb_to_hex(0, 0, 0)) + class ColorTemperatureMiredToKelvinTests(unittest.TestCase): """Test color_temperature_mired_to_kelvin."""