Skip to content

Commit

Permalink
Remove sending HS commands to lights
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJulianJES committed Apr 5, 2024
1 parent 8fc3aba commit 82332f2
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 151 deletions.
9 changes: 4 additions & 5 deletions tests/test_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

from zha.application import Platform
from zha.application.const import (
CONF_ALWAYS_PREFER_XY_COLOR_MODE,
CONF_GROUP_MEMBERS_ASSUME_STATE,
CUSTOM_CONFIGURATION,
ZHA_OPTIONS,
Expand Down Expand Up @@ -958,7 +957,7 @@ async def test_zha_group_light_entity(
lighting.Color.ColorCapabilities.Hue_and_saturation
),
},
{CONF_ALWAYS_PREFER_XY_COLOR_MODE: False},
{},
{},
),
# HS light with cached hue
Expand All @@ -969,7 +968,7 @@ async def test_zha_group_light_entity(
),
"current_hue": 100,
},
{CONF_ALWAYS_PREFER_XY_COLOR_MODE: False},
{},
{},
),
# HS light with cached saturation
Expand All @@ -980,7 +979,7 @@ async def test_zha_group_light_entity(
),
"current_saturation": 100,
},
{CONF_ALWAYS_PREFER_XY_COLOR_MODE: False},
{},
{},
),
# HS light with both
Expand All @@ -992,7 +991,7 @@ async def test_zha_group_light_entity(
"current_hue": 100,
"current_saturation": 100,
},
{CONF_ALWAYS_PREFER_XY_COLOR_MODE: False},
{},
{},
),
],
Expand Down
2 changes: 0 additions & 2 deletions zha/application/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
CONF_DEVICE_CONFIG = "device_config"
CONF_ENABLE_ENHANCED_LIGHT_TRANSITION = "enhanced_light_transition"
CONF_ENABLE_LIGHT_TRANSITIONING_FLAG = "light_transitioning_flag"
CONF_ALWAYS_PREFER_XY_COLOR_MODE = "always_prefer_xy_color_mode"
CONF_GROUP_MEMBERS_ASSUME_STATE = "group_members_assume_state"
CONF_ENABLE_IDENTIFY_ON_JOIN = "enable_identify_on_join"
CONF_ENABLE_QUIRKS = "enable_quirks"
Expand Down Expand Up @@ -156,7 +155,6 @@ def __voluptuous_compile__(self, schema: vol.Schema) -> Any: # pylint: disable=
),
vol.Required(CONF_ENABLE_ENHANCED_LIGHT_TRANSITION, default=False): boolean,
vol.Required(CONF_ENABLE_LIGHT_TRANSITIONING_FLAG, default=True): boolean,
vol.Required(CONF_ALWAYS_PREFER_XY_COLOR_MODE, default=True): boolean,
vol.Required(CONF_GROUP_MEMBERS_ASSUME_STATE, default=True): boolean,
vol.Required(CONF_ENABLE_IDENTIFY_ON_JOIN, default=True): boolean,
vol.Optional(
Expand Down
145 changes: 1 addition & 144 deletions zha/application/platforms/light/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

from zha.application import Platform
from zha.application.const import (
CONF_ALWAYS_PREFER_XY_COLOR_MODE,
CONF_DEFAULT_LIGHT_TRANSITION,
CONF_ENABLE_ENHANCED_LIGHT_TRANSITION,
CONF_ENABLE_LIGHT_TRANSITIONING_FLAG,
Expand All @@ -49,7 +48,6 @@
ATTR_EFFECT,
ATTR_EFFECT_LIST,
ATTR_FLASH,
ATTR_HS_COLOR,
ATTR_MAX_MIREDS,
ATTR_MIN_MIREDS,
ATTR_SUPPORTED_COLOR_MODES,
Expand Down Expand Up @@ -132,7 +130,6 @@ def __init__(self, *args, **kwargs):
self._available: bool = False
self._min_mireds: int | None = 153
self._max_mireds: int | None = 500
self._hs_color: tuple[float, float] | None = None
self._xy_color: tuple[float, float] | None = None
self._color_mode = ColorMode.UNKNOWN # Set by subclasses
self._color_temp: int | None = None
Expand All @@ -147,7 +144,6 @@ def __init__(self, *args, **kwargs):
self._zha_config_transition: int = self._DEFAULT_MIN_TRANSITION_TIME
self._zha_config_enhanced_light_transition: bool = False
self._zha_config_enable_light_transitioning_flag: bool = True
self._zha_config_always_prefer_xy_color_mode: bool = True
self._on_off_cluster_handler: ClusterHandler = None
self._level_cluster_handler: ClusterHandler = None
self._color_cluster_handler: ClusterHandler = None
Expand All @@ -162,7 +158,6 @@ def state(self) -> dict[str, Any]:
response = super().state
response["on"] = self.is_on
response["brightness"] = self.brightness
response["hs_color"] = self.hs_color
response["xy_color"] = self.xy_color
response["color_temp"] = self.color_temp
response["effect"] = self.effect
Expand All @@ -173,11 +168,6 @@ def state(self) -> dict[str, Any]:
response["supported_color_modes"] = self._supported_color_modes
return response

@property
def hs_color(self) -> tuple[float, float] | None:
"""Return the hs color value [int, int]."""
return self._hs_color

@property
def xy_color(self) -> tuple[float, float] | None:
"""Return the xy color value [float, float]."""
Expand Down Expand Up @@ -283,7 +273,6 @@ async def async_turn_on(self, **kwargs: Any) -> None:
flash = kwargs.get(ATTR_FLASH)
temperature = kwargs.get(ATTR_COLOR_TEMP)
xy_color = kwargs.get(ATTR_XY_COLOR)
hs_color = kwargs.get(ATTR_HS_COLOR)

execute_if_off_supported = (
self._GROUP_SUPPORTS_EXECUTE_IF_OFF
Expand All @@ -296,7 +285,6 @@ async def async_turn_on(self, **kwargs: Any) -> None:
brightness_supported(self._supported_color_modes)
or temperature is not None
or xy_color is not None
or hs_color is not None
) and self._zha_config_enable_light_transitioning_flag
transition_time = (
(
Expand All @@ -307,7 +295,6 @@ async def async_turn_on(self, **kwargs: Any) -> None:
or (self._off_with_transition and self._off_brightness is not None)
or temperature is not None
or xy_color is not None
or hs_color is not None
)
else DEFAULT_ON_OFF_TRANSITION + DEFAULT_EXTRA_TRANSITION_DELAY_SHORT
)
Expand Down Expand Up @@ -352,10 +339,6 @@ async def async_turn_on(self, **kwargs: Any) -> None:
xy_color is not None
and (self._xy_color != xy_color or self._color_mode != ColorMode.XY)
)
or (
hs_color is not None
and (self._hs_color != hs_color or self._color_mode != ColorMode.HS)
)
)
and brightness_supported(self._supported_color_modes)
and not execute_if_off_supported
Expand Down Expand Up @@ -401,7 +384,6 @@ async def async_turn_on(self, **kwargs: Any) -> None:
if not await self.async_handle_color_commands(
temperature,
duration, # duration is ignored by lights when off
hs_color,
xy_color,
new_color_provided_while_off,
t_log,
Expand Down Expand Up @@ -459,7 +441,6 @@ async def async_turn_on(self, **kwargs: Any) -> None:
if not await self.async_handle_color_commands(
temperature,
duration,
hs_color,
xy_color,
new_color_provided_while_off,
t_log,
Expand Down Expand Up @@ -578,7 +559,6 @@ async def async_handle_color_commands(
self,
temperature,
duration,
hs_color,
xy_color,
new_color_provided_while_off,
t_log,
Expand All @@ -602,33 +582,6 @@ async def async_handle_color_commands(
self._color_mode = ColorMode.COLOR_TEMP
self._color_temp = temperature
self._xy_color = None
self._hs_color = None

if hs_color is not None:
if (
not isinstance(self, LightGroup)
and self._color_cluster_handler.enhanced_hue_supported
):
result = await self._color_cluster_handler.enhanced_move_to_hue_and_saturation(
enhanced_hue=int(hs_color[0] * 65535 / 360),
saturation=int(hs_color[1] * 2.54),
transition_time=int(10 * transition_time),
)
t_log["enhanced_move_to_hue_and_saturation"] = result
else:
result = await self._color_cluster_handler.move_to_hue_and_saturation(
hue=int(hs_color[0] * 254 / 360),
saturation=int(hs_color[1] * 2.54),
transition_time=int(10 * transition_time),
)
t_log["move_to_hue_and_saturation"] = result
if result[1] is not Status.SUCCESS:
return False
self._color_mode = ColorMode.HS
self._hs_color = hs_color
self._xy_color = None
self._color_temp = None
xy_color = None # don't set xy_color if it is also present

if xy_color is not None:
result = await self._color_cluster_handler.move_to_color(
Expand All @@ -642,7 +595,6 @@ async def async_handle_color_commands(
self._color_mode = ColorMode.XY
self._xy_color = xy_color
self._color_temp = None
self._hs_color = None

return True

Expand Down Expand Up @@ -741,13 +693,6 @@ def __init__(
self._cancel_refresh_handle: Callable | None = None
effect_list = []

self._zha_config_always_prefer_xy_color_mode = async_get_zha_config_value(
device.gateway.config,
ZHA_OPTIONS,
CONF_ALWAYS_PREFER_XY_COLOR_MODE,
True,
)

self._supported_color_modes = {ColorMode.ONOFF}
if self._level_cluster_handler:
self._supported_color_modes.add(ColorMode.BRIGHTNESS)
Expand All @@ -759,10 +704,7 @@ def __init__(
self._supported_color_modes.add(ColorMode.COLOR_TEMP)
self._color_temp = self._color_cluster_handler.color_temperature

if self._color_cluster_handler.xy_supported and (
self._zha_config_always_prefer_xy_color_mode
or not self._color_cluster_handler.hs_supported
):
if self._color_cluster_handler.xy_supported:
self._supported_color_modes.add(ColorMode.XY)
curr_x = self._color_cluster_handler.current_x
curr_y = self._color_cluster_handler.current_y
Expand All @@ -771,33 +713,6 @@ def __init__(
else:
self._xy_color = (0, 0)

if (
self._color_cluster_handler.hs_supported
and not self._zha_config_always_prefer_xy_color_mode
):
self._supported_color_modes.add(ColorMode.HS)
if (
self._color_cluster_handler.enhanced_hue_supported
and self._color_cluster_handler.enhanced_current_hue is not None
):
curr_hue = (
self._color_cluster_handler.enhanced_current_hue * 65535 / 360
)
elif self._color_cluster_handler.current_hue is not None:
curr_hue = self._color_cluster_handler.current_hue * 254 / 360
else:
curr_hue = 0

if (
curr_saturation := self._color_cluster_handler.current_saturation
) is None:
curr_saturation = 0

self._hs_color = (
int(curr_hue),
int(curr_saturation * 2.54),
)

if self._color_cluster_handler.color_loop_supported:
self._supported_features |= LightEntityFeature.EFFECT
effect_list.append(EFFECT_COLORLOOP)
Expand Down Expand Up @@ -957,19 +872,6 @@ async def async_update(self) -> None:
"current_x",
"current_y",
]
if (
not self._zha_config_always_prefer_xy_color_mode
and self._color_cluster_handler.enhanced_hue_supported
):
attributes.append("enhanced_current_hue")
attributes.append("current_saturation")
if (
self._color_cluster_handler.hs_supported
and not self._color_cluster_handler.enhanced_hue_supported
and not self._zha_config_always_prefer_xy_color_mode
):
attributes.append("current_hue")
attributes.append("current_saturation")
if self._color_cluster_handler.color_temp_supported:
attributes.append("color_temperature")
if self._color_cluster_handler.color_loop_supported:
Expand All @@ -992,34 +894,13 @@ async def async_update(self) -> None:
if color_temp is not None and color_mode:
self._color_temp = color_temp
self._xy_color = None
self._hs_color = None
elif (
color_mode == Color.ColorMode.Hue_and_saturation
and not self._zha_config_always_prefer_xy_color_mode
):
self._color_mode = ColorMode.HS
if self._color_cluster_handler.enhanced_hue_supported:
current_hue = results.get("enhanced_current_hue")
else:
current_hue = results.get("current_hue")
current_saturation = results.get("current_saturation")
if current_hue is not None and current_saturation is not None:
self._hs_color = (
int(current_hue * 360 / 65535)
if self._color_cluster_handler.enhanced_hue_supported
else int(current_hue * 360 / 254),
int(current_saturation / 2.54),
)
self._xy_color = None
self._color_temp = None
else:
self._color_mode = ColorMode.XY
color_x = results.get("current_x")
color_y = results.get("current_y")
if color_x is not None and color_y is not None:
self._xy_color = (color_x / 65535, color_y / 65535)
self._color_temp = None
self._hs_color = None

color_loop_active = results.get("color_loop_active")
if color_loop_active is not None:
Expand All @@ -1039,7 +920,6 @@ def _assume_group_state(self, update_params) -> None:
color_mode = update_params.get(ATTR_COLOR_MODE)
color_temp = update_params.get(ATTR_COLOR_TEMP)
xy_color = update_params.get(ATTR_XY_COLOR)
hs_color = update_params.get(ATTR_HS_COLOR)
effect = update_params.get(ATTR_EFFECT)

supported_modes = self._supported_color_modes
Expand Down Expand Up @@ -1081,8 +961,6 @@ def _assume_group_state(self, update_params) -> None:
self._color_temp = color_temp
if xy_color is not None and ColorMode.XY in supported_modes:
self._xy_color = xy_color
if hs_color is not None and ColorMode.HS in supported_modes:
self._hs_color = hs_color
# the effect is always deactivated in async_turn_on if not provided
if effect is None:
self._effect = None
Expand Down Expand Up @@ -1182,12 +1060,6 @@ def __init__(self, group: Group):
CONF_ENABLE_LIGHT_TRANSITIONING_FLAG,
True,
)
self._zha_config_always_prefer_xy_color_mode = async_get_zha_config_value(
group.gateway.config,
ZHA_OPTIONS,
CONF_ALWAYS_PREFER_XY_COLOR_MODE,
True,
)
self._zha_config_group_members_assume_state = async_get_zha_config_value(
group.gateway.config,
ZHA_OPTIONS,
Expand Down Expand Up @@ -1286,11 +1158,6 @@ def update(self, _: Any = None) -> None:

self._xy_color = reduce_attribute(on_states, ATTR_XY_COLOR, reduce=mean_tuple)

if not self._zha_config_always_prefer_xy_color_mode:
self._hs_color = reduce_attribute(
on_states, ATTR_HS_COLOR, reduce=mean_tuple
)

self._color_temp = reduce_attribute(on_states, ATTR_COLOR_TEMP)
self._min_mireds = reduce_attribute(
states, ATTR_MIN_MIREDS, default=153, reduce=min
Expand Down Expand Up @@ -1344,12 +1211,6 @@ def update(self, _: Any = None) -> None:
else:
self._color_mode = next(iter(supported_color_modes))

if self._color_mode == ColorMode.HS and (
color_mode_count[ColorMode.HS] != len(self._group.members)
or self._zha_config_always_prefer_xy_color_mode
): # switch to XY if all members do not support HS
self._color_mode = ColorMode.XY

self._supported_features = LightEntityFeature(0)
for support in find_state_attributes(states, ATTR_SUPPORTED_FEATURES):
# Merge supported features by emulating support for every feature
Expand Down Expand Up @@ -1391,10 +1252,6 @@ def _make_members_assume_group_state(
update_params[ATTR_COLOR_MODE] = self._color_mode
update_params[ATTR_XY_COLOR] = self._xy_color

if ATTR_HS_COLOR in service_kwargs:
update_params[ATTR_COLOR_MODE] = self._color_mode
update_params[ATTR_HS_COLOR] = self._hs_color

if ATTR_EFFECT in service_kwargs:
update_params[ATTR_EFFECT] = self._effect

Expand Down

0 comments on commit 82332f2

Please sign in to comment.