diff --git a/homeassistant/components/google_assistant/trait.py b/homeassistant/components/google_assistant/trait.py index 12464998211..f554f3375f2 100644 --- a/homeassistant/components/google_assistant/trait.py +++ b/homeassistant/components/google_assistant/trait.py @@ -300,17 +300,19 @@ class ColorSettingTrait(_Trait): response = {} if features & light.SUPPORT_COLOR: - response['colorModel'] = 'rgb' + response['colorModel'] = 'hsv' if features & light.SUPPORT_COLOR_TEMP: # Max Kelvin is Min Mireds K = 1000000 / mireds # Min Kevin is Max Mireds K = 1000000 / mireds - response['temperatureMaxK'] = \ + response['colorTemperatureRange'] = { + 'temperatureMaxK': color_util.color_temperature_mired_to_kelvin( - attrs.get(light.ATTR_MIN_MIREDS)) - response['temperatureMinK'] = \ + attrs.get(light.ATTR_MIN_MIREDS)), + 'temperatureMinK': color_util.color_temperature_mired_to_kelvin( - attrs.get(light.ATTR_MAX_MIREDS)) + attrs.get(light.ATTR_MAX_MIREDS)), + } return response @@ -321,12 +323,13 @@ class ColorSettingTrait(_Trait): if features & light.SUPPORT_COLOR: color_hs = self.state.attributes.get(light.ATTR_HS_COLOR) + brightness = self.state.attributes.get(light.ATTR_BRIGHTNESS, 1) if color_hs is not None: - color['spectrumRGB'] = int( - color_util.color_rgb_to_hex( - *color_util.color_hs_to_RGB(*color_hs)), - 16 - ) + color['spectrumHsv'] = { + 'hue': color_hs[0], + 'saturation': color_hs[1]/100, + 'value': brightness/255, + } if features & light.SUPPORT_COLOR_TEMP: temp = self.state.attributes.get(light.ATTR_COLOR_TEMP) @@ -335,7 +338,7 @@ class ColorSettingTrait(_Trait): _LOGGER.warning('Entity %s has incorrect color temperature %s', self.state.entity_id, temp) elif temp is not None: - color['temperature'] = \ + color['temperatureK'] = \ color_util.color_temperature_mired_to_kelvin(temp) response = {} @@ -377,6 +380,18 @@ class ColorSettingTrait(_Trait): light.ATTR_HS_COLOR: color }, blocking=True, context=data.context) + elif 'spectrumHSV' in params['color']: + color = params['color']['spectrumHSV'] + saturation = color['saturation'] * 100 + brightness = color['value'] * 255 + + await self.hass.services.async_call( + light.DOMAIN, SERVICE_TURN_ON, { + ATTR_ENTITY_ID: self.state.entity_id, + light.ATTR_HS_COLOR: [color['hue'], saturation], + light.ATTR_BRIGHTNESS: brightness + }, blocking=True, context=data.context) + @register_trait class SceneTrait(_Trait): diff --git a/tests/components/google_assistant/test_google_assistant.py b/tests/components/google_assistant/test_google_assistant.py index 60df4a8e79c..19e1858d4f5 100644 --- a/tests/components/google_assistant/test_google_assistant.py +++ b/tests/components/google_assistant/test_google_assistant.py @@ -174,8 +174,12 @@ def test_query_request(hass_fixture, assistant_client, auth_header): assert devices['light.bed_light']['on'] is False assert devices['light.ceiling_lights']['on'] is True assert devices['light.ceiling_lights']['brightness'] == 70 - assert devices['light.kitchen_lights']['color']['spectrumRGB'] == 16727919 - assert devices['light.kitchen_lights']['color']['temperature'] == 4166 + assert devices['light.kitchen_lights']['color']['spectrumHsv'] == { + 'hue': 345, + 'saturation': 0.75, + 'value': 0.7058823529411765, + } + assert devices['light.kitchen_lights']['color']['temperatureK'] == 4166 assert devices['media_player.lounge_room']['on'] is True diff --git a/tests/components/google_assistant/test_smart_home.py b/tests/components/google_assistant/test_smart_home.py index d33c1f1bc5b..be32d4cb5d9 100644 --- a/tests/components/google_assistant/test_smart_home.py +++ b/tests/components/google_assistant/test_smart_home.py @@ -98,9 +98,11 @@ async def test_sync_message(hass): 'type': sh.TYPE_LIGHT, 'willReportState': False, 'attributes': { - 'colorModel': 'rgb', - 'temperatureMinK': 2000, - 'temperatureMaxK': 6535, + 'colorModel': 'hsv', + 'colorTemperatureRange': { + 'temperatureMinK': 2000, + 'temperatureMaxK': 6535, + } }, 'roomHint': 'Living Room' }] @@ -176,9 +178,11 @@ async def test_sync_in_area(hass, registries): 'type': sh.TYPE_LIGHT, 'willReportState': False, 'attributes': { - 'colorModel': 'rgb', - 'temperatureMinK': 2000, - 'temperatureMaxK': 6535, + 'colorModel': 'hsv', + 'colorTemperatureRange': { + 'temperatureMinK': 2000, + 'temperatureMaxK': 6535, + } }, 'roomHint': 'Living Room' }] @@ -252,8 +256,12 @@ async def test_query_message(hass): 'online': True, 'brightness': 30, 'color': { - 'spectrumRGB': 4194303, - 'temperature': 2500, + 'spectrumHsv': { + 'hue': 180, + 'saturation': 0.75, + 'value': 0.3058823529411765, + }, + 'temperatureK': 2500, } }, } @@ -338,8 +346,12 @@ async def test_execute(hass): "online": True, 'brightness': 20, 'color': { - 'spectrumRGB': 16773155, - 'temperature': 2631, + 'spectrumHsv': { + 'hue': 56, + 'saturation': 0.86, + 'value': 0.2, + }, + 'temperatureK': 2631, }, } }] diff --git a/tests/components/google_assistant/test_trait.py b/tests/components/google_assistant/test_trait.py index 4a84d789068..9887c1da2cc 100644 --- a/tests/components/google_assistant/test_trait.py +++ b/tests/components/google_assistant/test_trait.py @@ -459,17 +459,22 @@ async def test_color_setting_color_light(hass): light.SUPPORT_COLOR, None) trt = trait.ColorSettingTrait(hass, State('light.bla', STATE_ON, { - light.ATTR_HS_COLOR: (0, 94), + light.ATTR_HS_COLOR: (20, 94), + light.ATTR_BRIGHTNESS: 200, ATTR_SUPPORTED_FEATURES: light.SUPPORT_COLOR, }), BASIC_CONFIG) assert trt.sync_attributes() == { - 'colorModel': 'rgb' + 'colorModel': 'hsv' } assert trt.query_attributes() == { 'color': { - 'spectrumRGB': 16715535 + 'spectrumHsv': { + 'hue': 20, + 'saturation': 0.94, + 'value': 200 / 255, + } } } @@ -491,6 +496,22 @@ async def test_color_setting_color_light(hass): light.ATTR_HS_COLOR: (240, 93.725), } + await trt.execute(trait.COMMAND_COLOR_ABSOLUTE, BASIC_DATA, { + 'color': { + 'spectrumHSV': { + 'hue': 100, + 'saturation': .50, + 'value': .20, + } + } + }) + assert len(calls) == 2 + assert calls[1].data == { + ATTR_ENTITY_ID: 'light.bla', + light.ATTR_HS_COLOR: [100, 50], + light.ATTR_BRIGHTNESS: .2 * 255, + } + async def test_color_setting_temperature_light(hass): """Test ColorTemperature trait support for light domain.""" @@ -506,13 +527,15 @@ async def test_color_setting_temperature_light(hass): }), BASIC_CONFIG) assert trt.sync_attributes() == { - 'temperatureMinK': 2000, - 'temperatureMaxK': 5000, + 'colorTemperatureRange': { + 'temperatureMinK': 2000, + 'temperatureMaxK': 5000, + } } assert trt.query_attributes() == { 'color': { - 'temperature': 3333 + 'temperatureK': 3333 } }