Fix emulated_hue brightness check (#106783)

This commit is contained in:
J. Nick Koston 2024-01-02 01:51:05 -10:00 committed by GitHub
parent 3f1263a533
commit 038e55a2cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,6 +2,7 @@
from __future__ import annotations
import asyncio
from collections.abc import Iterable
from functools import lru_cache
import hashlib
from http import HTTPStatus
@ -41,6 +42,7 @@ from homeassistant.components.light import (
ATTR_HS_COLOR,
ATTR_TRANSITION,
ATTR_XY_COLOR,
ColorMode,
LightEntityFeature,
)
from homeassistant.components.media_player import (
@ -115,12 +117,19 @@ UNAUTHORIZED_USER = [
{"error": {"address": "/", "description": "unauthorized user", "type": "1"}}
]
DIMMABLE_SUPPORT_FEATURES = (
CoverEntityFeature.SET_POSITION
| FanEntityFeature.SET_SPEED
| MediaPlayerEntityFeature.VOLUME_SET
| ClimateEntityFeature.TARGET_TEMPERATURE
)
DIMMABLE_SUPPORTED_FEATURES_BY_DOMAIN = {
cover.DOMAIN: CoverEntityFeature.SET_POSITION,
fan.DOMAIN: FanEntityFeature.SET_SPEED,
media_player.DOMAIN: MediaPlayerEntityFeature.VOLUME_SET,
climate.DOMAIN: ClimateEntityFeature.TARGET_TEMPERATURE,
}
ENTITY_FEATURES_BY_DOMAIN = {
cover.DOMAIN: CoverEntityFeature,
fan.DOMAIN: FanEntityFeature,
media_player.DOMAIN: MediaPlayerEntityFeature,
climate.DOMAIN: ClimateEntityFeature,
}
@lru_cache(maxsize=32)
@ -756,7 +765,6 @@ def _entity_unique_id(entity_id: str) -> str:
def state_to_json(config: Config, state: State) -> dict[str, Any]:
"""Convert an entity to its Hue bridge JSON representation."""
entity_features = state.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
color_modes = state.attributes.get(light.ATTR_SUPPORTED_COLOR_MODES, [])
unique_id = _entity_unique_id(state.entity_id)
state_dict = get_entity_state_dict(config, state)
@ -773,9 +781,9 @@ def state_to_json(config: Config, state: State) -> dict[str, Any]:
"manufacturername": "Home Assistant",
"swversion": "123",
}
color_supported = light.color_supported(color_modes)
color_temp_supported = light.color_temp_supported(color_modes)
is_light = state.domain == light.DOMAIN
color_supported = is_light and light.color_supported(color_modes)
color_temp_supported = is_light and light.color_temp_supported(color_modes)
if color_supported and color_temp_supported:
# Extended Color light (Zigbee Device ID: 0x0210)
# Same as Color light, but which supports additional setting of color temperature
@ -820,9 +828,7 @@ def state_to_json(config: Config, state: State) -> dict[str, Any]:
HUE_API_STATE_BRI: state_dict[STATE_BRIGHTNESS],
}
)
elif entity_features & DIMMABLE_SUPPORT_FEATURES or light.brightness_supported(
color_modes
):
elif state_supports_hue_brightness(state, color_modes):
# Dimmable light (Zigbee Device ID: 0x0100)
# Supports groups, scenes, on/off and dimming
retval["type"] = "Dimmable light"
@ -845,6 +851,21 @@ def state_to_json(config: Config, state: State) -> dict[str, Any]:
return retval
def state_supports_hue_brightness(
state: State, color_modes: Iterable[ColorMode]
) -> bool:
"""Return True if the state supports brightness."""
domain = state.domain
if domain == light.DOMAIN:
return light.brightness_supported(color_modes)
if not (required_feature := DIMMABLE_SUPPORTED_FEATURES_BY_DOMAIN.get(domain)):
return False
features = state.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
enum = ENTITY_FEATURES_BY_DOMAIN[domain]
features = enum(features) if type(features) is int else features # noqa: E721
return required_feature in features
def create_hue_success_response(
entity_number: str, attr: str, value: str
) -> dict[str, Any]: