Google Assistant: Migrate light setting trait to use HSV color spectrum (#22980)

* Migrate light setting trait to use HSV

* Fix tests

* Fix all the typos
This commit is contained in:
Paulus Schoutsen 2019-04-10 21:35:37 -07:00 committed by GitHub
parent c2cfc4a813
commit f33bf718c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 83 additions and 29 deletions

View file

@ -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):

View file

@ -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

View file

@ -98,9 +98,11 @@ async def test_sync_message(hass):
'type': sh.TYPE_LIGHT,
'willReportState': False,
'attributes': {
'colorModel': 'rgb',
'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',
'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,
},
}
}]

View file

@ -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() == {
'colorTemperatureRange': {
'temperatureMinK': 2000,
'temperatureMaxK': 5000,
}
}
assert trt.query_attributes() == {
'color': {
'temperature': 3333
'temperatureK': 3333
}
}