diff --git a/.gitignore b/.gitignore index 33a1f4f9a4b3de..bf49a1b61c1fea 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,7 @@ Icon # pytest .pytest_cache +.cache # GITHUB Proposed Python stuff: *.py[cod] diff --git a/homeassistant/components/light/__init__.py b/homeassistant/components/light/__init__.py index f03521947b77d2..eea6c821fc064f 100644 --- a/homeassistant/components/light/__init__.py +++ b/homeassistant/components/light/__init__.py @@ -501,6 +501,10 @@ def state_attributes(self): *data[ATTR_HS_COLOR]) data[ATTR_XY_COLOR] = color_util.color_hs_to_xy( *data[ATTR_HS_COLOR]) + data[ATTR_HS_COLOR] = ( + round(data[ATTR_HS_COLOR][0], 3), + round(data[ATTR_HS_COLOR][1], 3), + ) return data diff --git a/homeassistant/components/light/hue.py b/homeassistant/components/light/hue.py index b1562aaba8f36f..71e3d4fa30b86a 100644 --- a/homeassistant/components/light/hue.py +++ b/homeassistant/components/light/hue.py @@ -225,9 +225,20 @@ def brightness(self): return self.light.action.get('bri') return self.light.state.get('bri') + @property + def _color_mode(self): + """Return the hue color mode.""" + if self.is_group: + return self.light.action.get('colormode') + return self.light.state.get('colormode') + @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": + return None + if self.is_group: return ( self.light.action.get('hue') / 65535 * 360, @@ -241,6 +252,10 @@ def hs_color(self): @property def color_temp(self): """Return the CT color value.""" + # Don't return color temperature unless in color temperature mode + if self._color_mode != "ct": + return None + if self.is_group: return self.light.action.get('ct') return self.light.state.get('ct') diff --git a/tests/components/light/test_hue.py b/tests/components/light/test_hue.py index 8abf51fdf0ca2e..54bb2184a64d0c 100644 --- a/tests/components/light/test_hue.py +++ b/tests/components/light/test_hue.py @@ -227,13 +227,48 @@ async def test_lights(hass, mock_bridge): assert lamp_1 is not None assert lamp_1.state == 'on' assert lamp_1.attributes['brightness'] == 144 - assert lamp_1.attributes['color_temp'] == 467 + assert lamp_1.attributes['hs_color'] == (71.896, 83.137) lamp_2 = hass.states.get('light.hue_lamp_2') assert lamp_2 is not None assert lamp_2.state == 'off' +async def test_lights_color_mode(hass, mock_bridge): + """Test that lights only report appropriate color mode.""" + mock_bridge.mock_light_responses.append(LIGHT_RESPONSE) + await setup_bridge(hass, mock_bridge) + + lamp_1 = hass.states.get('light.hue_lamp_1') + assert lamp_1 is not None + assert lamp_1.state == 'on' + assert lamp_1.attributes['brightness'] == 144 + assert lamp_1.attributes['hs_color'] == (71.896, 83.137) + assert 'color_temp' not in lamp_1.attributes + + new_light1_on = LIGHT_1_ON.copy() + new_light1_on['state'] = new_light1_on['state'].copy() + new_light1_on['state']['colormode'] = 'ct' + mock_bridge.mock_light_responses.append({ + "1": new_light1_on, + }) + mock_bridge.mock_group_responses.append({}) + + # Calling a service will trigger the updates to run + await hass.services.async_call('light', 'turn_on', { + 'entity_id': 'light.hue_lamp_2' + }, blocking=True) + # 2x light update, 1 turn on request + assert len(mock_bridge.mock_requests) == 3 + + lamp_1 = hass.states.get('light.hue_lamp_1') + assert lamp_1 is not None + assert lamp_1.state == 'on' + assert lamp_1.attributes['brightness'] == 144 + assert lamp_1.attributes['color_temp'] == 467 + assert 'hs_color' not in lamp_1.attributes + + async def test_groups(hass, mock_bridge): """Test the update_lights function with some lights.""" mock_bridge.allow_groups = True