Skip to content

Commit

Permalink
Use a static collection of forwarded attributes (home-assistant#54870)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
samueltardieu authored Aug 20, 2021
1 parent 152f799 commit 71b8409
Showing 1 changed file with 21 additions and 34 deletions.
55 changes: 21 additions & 34 deletions homeassistant/components/group/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down Expand Up @@ -128,40 +145,10 @@ def white_value(self) -> int | None:

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,
Expand Down

0 comments on commit 71b8409

Please sign in to comment.