From 71b8409c0d382fab0f3b7369b6a21632d04a03ca Mon Sep 17 00:00:00 2001 From: Samuel Tardieu Date: Fri, 20 Aug 2021 23:24:16 +0200 Subject: [PATCH] Use a static collection of forwarded attributes (#54870) Not repeating each attribute name three times lowers the risk of a typo. Also, only one lookup is done during the kwargs traversal instead of two. --- homeassistant/components/group/light.py | 55 ++++++++++--------------- 1 file changed, 21 insertions(+), 34 deletions(-) diff --git a/homeassistant/components/group/light.py b/homeassistant/components/group/light.py index bb0762d2278..671a471318a 100644 --- a/homeassistant/components/group/light.py +++ b/homeassistant/components/group/light.py @@ -82,6 +82,23 @@ async def async_setup_platform( ) +FORWARDED_ATTRIBUTES = frozenset( + { + ATTR_BRIGHTNESS, + ATTR_COLOR_TEMP, + ATTR_EFFECT, + ATTR_FLASH, + ATTR_HS_COLOR, + ATTR_RGB_COLOR, + ATTR_RGBW_COLOR, + ATTR_RGBWW_COLOR, + ATTR_TRANSITION, + ATTR_WHITE_VALUE, + ATTR_XY_COLOR, + } +) + + class LightGroup(GroupEntity, light.LightEntity): """Representation of a light group.""" @@ -128,40 +145,10 @@ class LightGroup(GroupEntity, light.LightEntity): async def async_turn_on(self, **kwargs: Any) -> None: """Forward the turn_on command to all lights in the light group.""" - data = {ATTR_ENTITY_ID: self._entity_ids} - - if ATTR_BRIGHTNESS in kwargs: - data[ATTR_BRIGHTNESS] = kwargs[ATTR_BRIGHTNESS] - - if ATTR_HS_COLOR in kwargs: - data[ATTR_HS_COLOR] = kwargs[ATTR_HS_COLOR] - - if ATTR_RGB_COLOR in kwargs: - data[ATTR_RGB_COLOR] = kwargs[ATTR_RGB_COLOR] - - if ATTR_RGBW_COLOR in kwargs: - data[ATTR_RGBW_COLOR] = kwargs[ATTR_RGBW_COLOR] - - if ATTR_RGBWW_COLOR in kwargs: - data[ATTR_RGBWW_COLOR] = kwargs[ATTR_RGBWW_COLOR] - - if ATTR_XY_COLOR in kwargs: - data[ATTR_XY_COLOR] = kwargs[ATTR_XY_COLOR] - - if ATTR_COLOR_TEMP in kwargs: - data[ATTR_COLOR_TEMP] = kwargs[ATTR_COLOR_TEMP] - - if ATTR_WHITE_VALUE in kwargs: - data[ATTR_WHITE_VALUE] = kwargs[ATTR_WHITE_VALUE] - - if ATTR_EFFECT in kwargs: - data[ATTR_EFFECT] = kwargs[ATTR_EFFECT] - - if ATTR_TRANSITION in kwargs: - data[ATTR_TRANSITION] = kwargs[ATTR_TRANSITION] - - if ATTR_FLASH in kwargs: - data[ATTR_FLASH] = kwargs[ATTR_FLASH] + data = { + key: value for key, value in kwargs.items() if key in FORWARDED_ATTRIBUTES + } + data[ATTR_ENTITY_ID] = self._entity_ids await self.hass.services.async_call( light.DOMAIN,