Use supported_color_modes in emulated_hue (#49175)

This commit is contained in:
Erik Montnemery 2021-04-23 01:42:28 +02:00 committed by GitHub
parent 1016d4ea28
commit a9065f381d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 29 deletions

View File

@ -45,9 +45,6 @@ from homeassistant.components.light import (
ATTR_HS_COLOR, ATTR_HS_COLOR,
ATTR_TRANSITION, ATTR_TRANSITION,
ATTR_XY_COLOR, ATTR_XY_COLOR,
SUPPORT_BRIGHTNESS,
SUPPORT_COLOR,
SUPPORT_COLOR_TEMP,
SUPPORT_TRANSITION, SUPPORT_TRANSITION,
) )
from homeassistant.components.media_player.const import ( from homeassistant.components.media_player.const import (
@ -356,6 +353,8 @@ class HueOneLightChangeView(HomeAssistantView):
# Get the entity's supported features # Get the entity's supported features
entity_features = entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0) entity_features = entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
if entity.domain == light.DOMAIN:
color_modes = entity.attributes.get(light.ATTR_SUPPORTED_COLOR_MODES, [])
# Parse the request # Parse the request
parsed = { parsed = {
@ -401,7 +400,7 @@ class HueOneLightChangeView(HomeAssistantView):
if HUE_API_STATE_BRI in request_json: if HUE_API_STATE_BRI in request_json:
if entity.domain == light.DOMAIN: if entity.domain == light.DOMAIN:
if entity_features & SUPPORT_BRIGHTNESS: if light.brightness_supported(color_modes):
parsed[STATE_ON] = parsed[STATE_BRIGHTNESS] > 0 parsed[STATE_ON] = parsed[STATE_BRIGHTNESS] > 0
else: else:
parsed[STATE_BRIGHTNESS] = None parsed[STATE_BRIGHTNESS] = None
@ -440,14 +439,14 @@ class HueOneLightChangeView(HomeAssistantView):
if entity.domain == light.DOMAIN: if entity.domain == light.DOMAIN:
if parsed[STATE_ON]: if parsed[STATE_ON]:
if ( if (
entity_features & SUPPORT_BRIGHTNESS light.brightness_supported(color_modes)
and parsed[STATE_BRIGHTNESS] is not None and parsed[STATE_BRIGHTNESS] is not None
): ):
data[ATTR_BRIGHTNESS] = hue_brightness_to_hass( data[ATTR_BRIGHTNESS] = hue_brightness_to_hass(
parsed[STATE_BRIGHTNESS] parsed[STATE_BRIGHTNESS]
) )
if entity_features & SUPPORT_COLOR: if light.color_supported(color_modes):
if any((parsed[STATE_HUE], parsed[STATE_SATURATION])): if any((parsed[STATE_HUE], parsed[STATE_SATURATION])):
if parsed[STATE_HUE] is not None: if parsed[STATE_HUE] is not None:
hue = parsed[STATE_HUE] hue = parsed[STATE_HUE]
@ -469,7 +468,7 @@ class HueOneLightChangeView(HomeAssistantView):
data[ATTR_XY_COLOR] = parsed[STATE_XY] data[ATTR_XY_COLOR] = parsed[STATE_XY]
if ( if (
entity_features & SUPPORT_COLOR_TEMP light.color_temp_supported(color_modes)
and parsed[STATE_COLOR_TEMP] is not None and parsed[STATE_COLOR_TEMP] is not None
): ):
data[ATTR_COLOR_TEMP] = parsed[STATE_COLOR_TEMP] data[ATTR_COLOR_TEMP] = parsed[STATE_COLOR_TEMP]
@ -671,13 +670,7 @@ def get_entity_state(config, entity):
data[STATE_SATURATION] = 0 data[STATE_SATURATION] = 0
data[STATE_COLOR_TEMP] = 0 data[STATE_COLOR_TEMP] = 0
# Get the entity's supported features if entity.domain == climate.DOMAIN:
entity_features = entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
if entity.domain == light.DOMAIN:
if entity_features & SUPPORT_BRIGHTNESS:
pass
elif entity.domain == climate.DOMAIN:
temperature = entity.attributes.get(ATTR_TEMPERATURE, 0) temperature = entity.attributes.get(ATTR_TEMPERATURE, 0)
# Convert 0-100 to 0-254 # Convert 0-100 to 0-254
data[STATE_BRIGHTNESS] = round(temperature * HUE_API_STATE_BRI_MAX / 100) data[STATE_BRIGHTNESS] = round(temperature * HUE_API_STATE_BRI_MAX / 100)
@ -736,6 +729,7 @@ def get_entity_state(config, entity):
def entity_to_json(config, entity): def entity_to_json(config, entity):
"""Convert an entity to its Hue bridge JSON representation.""" """Convert an entity to its Hue bridge JSON representation."""
entity_features = entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0) entity_features = entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
color_modes = entity.attributes.get(light.ATTR_SUPPORTED_COLOR_MODES, [])
unique_id = hashlib.md5(entity.entity_id.encode()).hexdigest() unique_id = hashlib.md5(entity.entity_id.encode()).hexdigest()
unique_id = f"00:{unique_id[0:2]}:{unique_id[2:4]}:{unique_id[4:6]}:{unique_id[6:8]}:{unique_id[8:10]}:{unique_id[10:12]}:{unique_id[12:14]}-{unique_id[14:16]}" unique_id = f"00:{unique_id[0:2]}:{unique_id[2:4]}:{unique_id[4:6]}:{unique_id[6:8]}:{unique_id[8:10]}:{unique_id[10:12]}:{unique_id[12:14]}-{unique_id[14:16]}"
@ -753,11 +747,7 @@ def entity_to_json(config, entity):
"swversion": "123", "swversion": "123",
} }
if ( if light.color_supported(color_modes) and light.color_temp_supported(color_modes):
(entity_features & SUPPORT_BRIGHTNESS)
and (entity_features & SUPPORT_COLOR)
and (entity_features & SUPPORT_COLOR_TEMP)
):
# Extended Color light (Zigbee Device ID: 0x0210) # Extended Color light (Zigbee Device ID: 0x0210)
# Same as Color light, but which supports additional setting of color temperature # Same as Color light, but which supports additional setting of color temperature
retval["type"] = "Extended color light" retval["type"] = "Extended color light"
@ -775,7 +765,7 @@ def entity_to_json(config, entity):
retval["state"][HUE_API_STATE_COLORMODE] = "hs" retval["state"][HUE_API_STATE_COLORMODE] = "hs"
else: else:
retval["state"][HUE_API_STATE_COLORMODE] = "ct" retval["state"][HUE_API_STATE_COLORMODE] = "ct"
elif (entity_features & SUPPORT_BRIGHTNESS) and (entity_features & SUPPORT_COLOR): elif light.color_supported(color_modes):
# Color light (Zigbee Device ID: 0x0200) # Color light (Zigbee Device ID: 0x0200)
# Supports on/off, dimming and color control (hue/saturation, enhanced hue, color loop and XY) # Supports on/off, dimming and color control (hue/saturation, enhanced hue, color loop and XY)
retval["type"] = "Color light" retval["type"] = "Color light"
@ -789,9 +779,7 @@ def entity_to_json(config, entity):
HUE_API_STATE_EFFECT: "none", HUE_API_STATE_EFFECT: "none",
} }
) )
elif (entity_features & SUPPORT_BRIGHTNESS) and ( elif light.color_temp_supported(color_modes):
entity_features & SUPPORT_COLOR_TEMP
):
# Color temperature light (Zigbee Device ID: 0x0220) # Color temperature light (Zigbee Device ID: 0x0220)
# Supports groups, scenes, on/off, dimming, and setting of a color temperature # Supports groups, scenes, on/off, dimming, and setting of a color temperature
retval["type"] = "Color temperature light" retval["type"] = "Color temperature light"
@ -804,12 +792,11 @@ def entity_to_json(config, entity):
} }
) )
elif entity_features & ( elif entity_features & (
SUPPORT_BRIGHTNESS SUPPORT_SET_POSITION
| SUPPORT_SET_POSITION
| SUPPORT_SET_SPEED | SUPPORT_SET_SPEED
| SUPPORT_VOLUME_SET | SUPPORT_VOLUME_SET
| SUPPORT_TARGET_TEMPERATURE | SUPPORT_TARGET_TEMPERATURE
): ) or light.brightness_supported(color_modes):
# Dimmable light (Zigbee Device ID: 0x0100) # Dimmable light (Zigbee Device ID: 0x0100)
# Supports groups, scenes, on/off and dimming # Supports groups, scenes, on/off and dimming
retval["type"] = "Dimmable light" retval["type"] = "Dimmable light"

View File

@ -742,9 +742,10 @@ async def test_put_light_state(hass, hass_hue, hue_client):
) )
# mock light.turn_on call # mock light.turn_on call
hass.states.async_set( attributes = hass.states.get("light.ceiling_lights").attributes
"light.ceiling_lights", STATE_ON, {ATTR_SUPPORTED_FEATURES: 55} supported_features = attributes[ATTR_SUPPORTED_FEATURES] | light.SUPPORT_TRANSITION
) attributes = {**attributes, ATTR_SUPPORTED_FEATURES: supported_features}
hass.states.async_set("light.ceiling_lights", STATE_ON, attributes)
call_turn_on = async_mock_service(hass, "light", "turn_on") call_turn_on = async_mock_service(hass, "light", "turn_on")
# update light state through api # update light state through api