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