Fix emulated_hue brightness check (#106783)
This commit is contained in:
parent
16d3d88fa3
commit
b1a55e9b19
1 changed files with 34 additions and 13 deletions
|
@ -2,6 +2,7 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from collections.abc import Iterable
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
import hashlib
|
import hashlib
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
|
@ -41,6 +42,7 @@ from homeassistant.components.light import (
|
||||||
ATTR_HS_COLOR,
|
ATTR_HS_COLOR,
|
||||||
ATTR_TRANSITION,
|
ATTR_TRANSITION,
|
||||||
ATTR_XY_COLOR,
|
ATTR_XY_COLOR,
|
||||||
|
ColorMode,
|
||||||
LightEntityFeature,
|
LightEntityFeature,
|
||||||
)
|
)
|
||||||
from homeassistant.components.media_player import (
|
from homeassistant.components.media_player import (
|
||||||
|
@ -115,12 +117,19 @@ UNAUTHORIZED_USER = [
|
||||||
{"error": {"address": "/", "description": "unauthorized user", "type": "1"}}
|
{"error": {"address": "/", "description": "unauthorized user", "type": "1"}}
|
||||||
]
|
]
|
||||||
|
|
||||||
DIMMABLE_SUPPORT_FEATURES = (
|
DIMMABLE_SUPPORTED_FEATURES_BY_DOMAIN = {
|
||||||
CoverEntityFeature.SET_POSITION
|
cover.DOMAIN: CoverEntityFeature.SET_POSITION,
|
||||||
| FanEntityFeature.SET_SPEED
|
fan.DOMAIN: FanEntityFeature.SET_SPEED,
|
||||||
| MediaPlayerEntityFeature.VOLUME_SET
|
media_player.DOMAIN: MediaPlayerEntityFeature.VOLUME_SET,
|
||||||
| ClimateEntityFeature.TARGET_TEMPERATURE
|
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)
|
@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]:
|
def state_to_json(config: Config, state: State) -> dict[str, Any]:
|
||||||
"""Convert an entity to its Hue bridge JSON representation."""
|
"""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, [])
|
color_modes = state.attributes.get(light.ATTR_SUPPORTED_COLOR_MODES, [])
|
||||||
unique_id = _entity_unique_id(state.entity_id)
|
unique_id = _entity_unique_id(state.entity_id)
|
||||||
state_dict = get_entity_state_dict(config, state)
|
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",
|
"manufacturername": "Home Assistant",
|
||||||
"swversion": "123",
|
"swversion": "123",
|
||||||
}
|
}
|
||||||
|
is_light = state.domain == light.DOMAIN
|
||||||
color_supported = light.color_supported(color_modes)
|
color_supported = is_light and light.color_supported(color_modes)
|
||||||
color_temp_supported = light.color_temp_supported(color_modes)
|
color_temp_supported = is_light and light.color_temp_supported(color_modes)
|
||||||
if color_supported and color_temp_supported:
|
if color_supported and color_temp_supported:
|
||||||
# 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
|
||||||
|
@ -820,9 +828,7 @@ def state_to_json(config: Config, state: State) -> dict[str, Any]:
|
||||||
HUE_API_STATE_BRI: state_dict[STATE_BRIGHTNESS],
|
HUE_API_STATE_BRI: state_dict[STATE_BRIGHTNESS],
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
elif entity_features & DIMMABLE_SUPPORT_FEATURES or light.brightness_supported(
|
elif state_supports_hue_brightness(state, color_modes):
|
||||||
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"
|
||||||
|
@ -845,6 +851,21 @@ def state_to_json(config: Config, state: State) -> dict[str, Any]:
|
||||||
return retval
|
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(
|
def create_hue_success_response(
|
||||||
entity_number: str, attr: str, value: str
|
entity_number: str, attr: str, value: str
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
|
|
Loading…
Add table
Reference in a new issue