From 80aac284b36bc945cf424ebc38b513c2b92b4e34 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 1 Dec 2026 11:24:50 +0100 Subject: [PATCH] Use the action field enums in hue (#195771) --- homeassistant/components/hue/v1/light.py | 54 ++++++++++++++---------- homeassistant/components/hue/v2/group.py | 26 ++++++------ homeassistant/components/hue/v2/light.py | 43 +++++++++++-------- 3 files changed, 71 insertions(+), 52 deletions(-) diff --git a/homeassistant/components/hue/v1/light.py b/homeassistant/components/hue/v1/light.py index 5b0f6bbb283e..ee957142aba1 100644 --- a/homeassistant/components/hue/v1/light.py +++ b/homeassistant/components/hue/v1/light.py @@ -10,12 +10,6 @@ from typing import Any, override import aiohue from homeassistant.components.light import ( - ATTR_BRIGHTNESS, - ATTR_COLOR_TEMP_KELVIN, - ATTR_EFFECT, - ATTR_FLASH, - ATTR_HS_COLOR, - ATTR_TRANSITION, DEFAULT_MAX_KELVIN, DEFAULT_MIN_KELVIN, EFFECT_COLORLOOP, @@ -25,6 +19,7 @@ from homeassistant.components.light import ( ColorMode, LightEntity, LightEntityFeature, + LightServiceArgument, filter_supported_color_modes, ) from homeassistant.core import HomeAssistant, callback @@ -550,30 +545,43 @@ class HueLight(CoordinatorEntity[DataUpdateCoordinator[None]], LightEntity): """Turn the specified or all lights on.""" command = {"on": True} - if ATTR_TRANSITION in kwargs: - command["transitiontime"] = int(kwargs[ATTR_TRANSITION] * 10) + if LightServiceArgument.TRANSITION in kwargs: + command["transitiontime"] = int( + kwargs[LightServiceArgument.TRANSITION] * 10 + ) - if ATTR_HS_COLOR in kwargs: + if LightServiceArgument.HS_COLOR in kwargs: if self.is_osram: - command["hue"] = int(kwargs[ATTR_HS_COLOR][0] / 360 * 65535) - command["sat"] = int(kwargs[ATTR_HS_COLOR][1] / 100 * 255) + command["hue"] = int( + kwargs[LightServiceArgument.HS_COLOR][0] / 360 * 65535 + ) + command["sat"] = int( + kwargs[LightServiceArgument.HS_COLOR][1] / 100 * 255 + ) else: # Philips hue bulb models respond differently to hue/sat # requests, so we convert to XY first to ensure a consistent # color. - xy_color = color_util.color_hs_to_xy(*kwargs[ATTR_HS_COLOR], self.gamut) + xy_color = color_util.color_hs_to_xy( + *kwargs[LightServiceArgument.HS_COLOR], self.gamut + ) command["xy"] = xy_color - elif ATTR_COLOR_TEMP_KELVIN in kwargs: + elif LightServiceArgument.COLOR_TEMP_KELVIN in kwargs: temp_k = max( self.min_color_temp_kelvin, - min(self.max_color_temp_kelvin, kwargs[ATTR_COLOR_TEMP_KELVIN]), + min( + self.max_color_temp_kelvin, + kwargs[LightServiceArgument.COLOR_TEMP_KELVIN], + ), ) command["ct"] = color_util.color_temperature_kelvin_to_mired(temp_k) - if ATTR_BRIGHTNESS in kwargs: - command["bri"] = hass_to_hue_brightness(kwargs[ATTR_BRIGHTNESS]) + if LightServiceArgument.BRIGHTNESS in kwargs: + command["bri"] = hass_to_hue_brightness( + kwargs[LightServiceArgument.BRIGHTNESS] + ) - flash = kwargs.get(ATTR_FLASH) + flash = kwargs.get(LightServiceArgument.FLASH) if flash == FLASH_LONG: command["alert"] = "lselect" @@ -589,8 +597,8 @@ class HueLight(CoordinatorEntity[DataUpdateCoordinator[None]], LightEntity): ): command["alert"] = "none" - if ATTR_EFFECT in kwargs: - effect = kwargs[ATTR_EFFECT] + if LightServiceArgument.EFFECT in kwargs: + effect = kwargs[LightServiceArgument.EFFECT] if effect == EFFECT_COLORLOOP: command["effect"] = "colorloop" elif effect == EFFECT_RANDOM: @@ -611,10 +619,12 @@ class HueLight(CoordinatorEntity[DataUpdateCoordinator[None]], LightEntity): """Turn the specified or all lights off.""" command = {"on": False} - if ATTR_TRANSITION in kwargs: - command["transitiontime"] = int(kwargs[ATTR_TRANSITION] * 10) + if LightServiceArgument.TRANSITION in kwargs: + command["transitiontime"] = int( + kwargs[LightServiceArgument.TRANSITION] * 10 + ) - flash = kwargs.get(ATTR_FLASH) + flash = kwargs.get(LightServiceArgument.FLASH) if flash == FLASH_LONG: command["alert"] = "lselect" diff --git a/homeassistant/components/hue/v2/group.py b/homeassistant/components/hue/v2/group.py index c2c1ebbde3aa..141afe999315 100644 --- a/homeassistant/components/hue/v2/group.py +++ b/homeassistant/components/hue/v2/group.py @@ -10,16 +10,12 @@ from aiohue.v2.models.feature import DynamicStatus from aiohue.v2.models.resource import ResourceTypes from homeassistant.components.light import ( - ATTR_BRIGHTNESS, - ATTR_COLOR_TEMP_KELVIN, - ATTR_FLASH, - ATTR_TRANSITION, - ATTR_XY_COLOR, FLASH_SHORT, ColorMode, LightEntity, LightEntityDescription, LightEntityFeature, + LightServiceArgument, ) from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import entity_registry as er @@ -163,15 +159,19 @@ class GroupedHueLight(HueBaseEntity, LightEntity): @override async def async_turn_on(self, **kwargs: Any) -> None: """Turn the grouped_light on.""" - transition = normalize_hue_transition(kwargs.get(ATTR_TRANSITION)) - xy_color = kwargs.get(ATTR_XY_COLOR) + transition = normalize_hue_transition( + kwargs.get(LightServiceArgument.TRANSITION) + ) + xy_color = kwargs.get(LightServiceArgument.XY_COLOR) color_temp = normalize_hue_colortemp( - kwargs.get(ATTR_COLOR_TEMP_KELVIN), + kwargs.get(LightServiceArgument.COLOR_TEMP_KELVIN), color_util.color_temperature_kelvin_to_mired(self.max_color_temp_kelvin), color_util.color_temperature_kelvin_to_mired(self.min_color_temp_kelvin), ) - brightness = normalize_hue_brightness(kwargs.get(ATTR_BRIGHTNESS)) - flash = kwargs.get(ATTR_FLASH) + brightness = normalize_hue_brightness( + kwargs.get(LightServiceArgument.BRIGHTNESS) + ) + flash = kwargs.get(LightServiceArgument.FLASH) if self._restore_brightness and brightness is None: # The Hue bridge sets the brightness to 1% when turning on a bulb @@ -202,10 +202,12 @@ class GroupedHueLight(HueBaseEntity, LightEntity): @override async def async_turn_off(self, **kwargs: Any) -> None: """Turn the light off.""" - transition = normalize_hue_transition(kwargs.get(ATTR_TRANSITION)) + transition = normalize_hue_transition( + kwargs.get(LightServiceArgument.TRANSITION) + ) if transition is not None: self._restore_brightness = self._brightness_pct - flash = kwargs.get(ATTR_FLASH) + flash = kwargs.get(LightServiceArgument.FLASH) if flash is not None: await self.async_set_flash(flash) diff --git a/homeassistant/components/hue/v2/light.py b/homeassistant/components/hue/v2/light.py index b81e54c85a7d..edfd41549030 100644 --- a/homeassistant/components/hue/v2/light.py +++ b/homeassistant/components/hue/v2/light.py @@ -10,18 +10,13 @@ from aiohue.v2.models.feature import EffectStatus, TimedEffectStatus from aiohue.v2.models.light import Light from homeassistant.components.light import ( - ATTR_BRIGHTNESS, - ATTR_COLOR_TEMP_KELVIN, - ATTR_EFFECT, - ATTR_FLASH, - ATTR_TRANSITION, - ATTR_XY_COLOR, EFFECT_OFF, FLASH_SHORT, ColorMode, LightEntity, LightEntityDescription, LightEntityFeature, + LightServiceArgument, filter_supported_color_modes, ) from homeassistant.core import HomeAssistant, callback @@ -270,14 +265,18 @@ class HueLight(HueBaseEntity, LightEntity): @override async def async_turn_on(self, **kwargs: Any) -> None: """Turn the device on.""" - transition = normalize_hue_transition(kwargs.get(ATTR_TRANSITION)) - xy_color = kwargs.get(ATTR_XY_COLOR) + transition = normalize_hue_transition( + kwargs.get(LightServiceArgument.TRANSITION) + ) + xy_color = kwargs.get(LightServiceArgument.XY_COLOR) color_temp = normalize_hue_colortemp( - kwargs.get(ATTR_COLOR_TEMP_KELVIN), + kwargs.get(LightServiceArgument.COLOR_TEMP_KELVIN), self.min_color_temp_mireds, self.max_color_temp_mireds, ) - brightness = normalize_hue_brightness(kwargs.get(ATTR_BRIGHTNESS)) + brightness = normalize_hue_brightness( + kwargs.get(LightServiceArgument.BRIGHTNESS) + ) if self._last_brightness and brightness is None: # The Hue bridge sets the brightness to 1% when turning on a bulb # when a transition was used to turn off the bulb. @@ -290,8 +289,8 @@ class HueLight(HueBaseEntity, LightEntity): brightness = self._last_brightness self._last_brightness = None self._color_temp_active = color_temp is not None - flash = kwargs.get(ATTR_FLASH) - effect = effect_str = kwargs.get(ATTR_EFFECT) + flash = kwargs.get(LightServiceArgument.FLASH) + effect = effect_str = kwargs.get(LightServiceArgument.EFFECT) if effect_str == EFFECT_OFF: # ignore effect if set to "off" and we have no effect active # the special effect "off" is only used to stop an active effect @@ -333,10 +332,12 @@ class HueLight(HueBaseEntity, LightEntity): @override async def async_turn_off(self, **kwargs: Any) -> None: """Turn the light off.""" - transition = normalize_hue_transition(kwargs.get(ATTR_TRANSITION)) + transition = normalize_hue_transition( + kwargs.get(LightServiceArgument.TRANSITION) + ) if transition is not None and self.resource.dimming: self._last_brightness = self.resource.dimming.brightness - flash = kwargs.get(ATTR_FLASH) + flash = kwargs.get(LightServiceArgument.FLASH) if flash is not None: await self.async_set_flash(flash) @@ -423,12 +424,16 @@ class HueGradientSegmentLight(HueBaseEntity, LightEntity): async def async_turn_on(self, **kwargs: Any) -> None: """Turn the light on, setting the color of this segment when given.""" point = None - if (xy_color := kwargs.get(ATTR_XY_COLOR)) is not None: + if (xy_color := kwargs.get(LightServiceArgument.XY_COLOR)) is not None: point = (self._index, xy_color) await self._writer.async_set_state( on=True, - brightness=normalize_hue_brightness(kwargs.get(ATTR_BRIGHTNESS)), - transition=normalize_hue_transition(kwargs.get(ATTR_TRANSITION)), + brightness=normalize_hue_brightness( + kwargs.get(LightServiceArgument.BRIGHTNESS) + ), + transition=normalize_hue_transition( + kwargs.get(LightServiceArgument.TRANSITION) + ), point=point, ) @@ -438,7 +443,9 @@ class HueGradientSegmentLight(HueBaseEntity, LightEntity): await self._writer.async_set_state( on=False, brightness=None, - transition=normalize_hue_transition(kwargs.get(ATTR_TRANSITION)), + transition=normalize_hue_transition( + kwargs.get(LightServiceArgument.TRANSITION) + ), ) @callback