From dfe3219f3f39254b30468dc273285ce936c952e3 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 26 Mar 2018 14:00:56 -0700 Subject: [PATCH] Hue: Convert XY to HS color if HS not present (#13465) * Hue: Convert XY to HS color if HS not present * Revert change to test * Address comments * Lint --- homeassistant/components/light/hue.py | 30 ++++++++------ tests/components/light/test_hue.py | 57 +++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/light/hue.py b/homeassistant/components/light/hue.py index 71e3d4fa30b..4a54f0a337d 100644 --- a/homeassistant/components/light/hue.py +++ b/homeassistant/components/light/hue.py @@ -18,6 +18,7 @@ from homeassistant.components.light import ( FLASH_LONG, FLASH_SHORT, SUPPORT_BRIGHTNESS, SUPPORT_COLOR_TEMP, SUPPORT_EFFECT, SUPPORT_FLASH, SUPPORT_COLOR, SUPPORT_TRANSITION, Light) +from homeassistant.util import color DEPENDENCIES = ['hue'] SCAN_INTERVAL = timedelta(seconds=5) @@ -235,19 +236,26 @@ class HueLight(Light): @property def hs_color(self): """Return the hs color value.""" - # Don't return hue/sat if in color temperature mode - if self._color_mode == "ct": + # pylint: disable=redefined-outer-name + mode = self._color_mode + + if mode not in ('hs', 'xy'): + return + + source = self.light.action if self.is_group else self.light.state + + hue = source.get('hue') + sat = source.get('sat') + + # Sometimes the state will not include valid hue/sat values. + # Reported as issue 13434 + if hue is not None and sat is not None: + return hue / 65535 * 360, sat / 255 * 100 + + if 'xy' not in source: return None - if self.is_group: - return ( - self.light.action.get('hue') / 65535 * 360, - self.light.action.get('sat') / 255 * 100, - ) - return ( - self.light.state.get('hue') / 65535 * 360, - self.light.state.get('sat') / 255 * 100, - ) + return color.color_xy_to_hs(*source['xy']) @property def color_temp(self): diff --git a/tests/components/light/test_hue.py b/tests/components/light/test_hue.py index 54bb2184a64..d73531b1b9a 100644 --- a/tests/components/light/test_hue.py +++ b/tests/components/light/test_hue.py @@ -11,6 +11,7 @@ import pytest from homeassistant.components import hue import homeassistant.components.light.hue as hue_light +from homeassistant.util import color _LOGGER = logging.getLogger(__name__) @@ -623,3 +624,59 @@ def test_available(): ) assert light.available is True + + +def test_hs_color(): + """Test hs_color property.""" + light = hue_light.HueLight( + light=Mock(state={ + 'colormode': 'ct', + 'hue': 1234, + 'sat': 123, + }), + request_bridge_update=None, + bridge=Mock(), + is_group=False, + ) + + assert light.hs_color is None + + light = hue_light.HueLight( + light=Mock(state={ + 'colormode': 'hs', + 'hue': 1234, + 'sat': 123, + }), + request_bridge_update=None, + bridge=Mock(), + is_group=False, + ) + + assert light.hs_color == (1234 / 65535 * 360, 123 / 255 * 100) + + light = hue_light.HueLight( + light=Mock(state={ + 'colormode': 'xy', + 'hue': 1234, + 'sat': 123, + }), + request_bridge_update=None, + bridge=Mock(), + is_group=False, + ) + + assert light.hs_color == (1234 / 65535 * 360, 123 / 255 * 100) + + light = hue_light.HueLight( + light=Mock(state={ + 'colormode': 'xy', + 'hue': None, + 'sat': 123, + 'xy': [0.4, 0.5] + }), + request_bridge_update=None, + bridge=Mock(), + is_group=False, + ) + + assert light.hs_color == color.color_xy_to_hs(0.4, 0.5)