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.
This commit is contained in:
Samuel Tardieu 2021-08-20 23:24:16 +02:00 committed by GitHub
parent 152f799d0e
commit 71b8409c0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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): class LightGroup(GroupEntity, light.LightEntity):
"""Representation of a light group.""" """Representation of a light group."""
@ -128,40 +145,10 @@ class LightGroup(GroupEntity, light.LightEntity):
async def async_turn_on(self, **kwargs: Any) -> None: async def async_turn_on(self, **kwargs: Any) -> None:
"""Forward the turn_on command to all lights in the light group.""" """Forward the turn_on command to all lights in the light group."""
data = {ATTR_ENTITY_ID: self._entity_ids} data = {
key: value for key, value in kwargs.items() if key in FORWARDED_ATTRIBUTES
if ATTR_BRIGHTNESS in kwargs: }
data[ATTR_BRIGHTNESS] = kwargs[ATTR_BRIGHTNESS] data[ATTR_ENTITY_ID] = self._entity_ids
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]
await self.hass.services.async_call( await self.hass.services.async_call(
light.DOMAIN, light.DOMAIN,