Style changes & Improvements

This commit is contained in:
Otto Winter
2018-02-27 21:34:05 +01:00
parent 2b20e7261a
commit 72bd1fc5d3

View File

@@ -48,17 +48,15 @@ SUPPORT_GROUP_LIGHT = (SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP | SUPPORT_EFFECT
async def async_setup_platform(hass: HomeAssistantType, config: ConfigType, async def async_setup_platform(hass: HomeAssistantType, config: ConfigType,
async_add_devices, discovery_info=None) -> None: async_add_devices, discovery_info=None) -> None:
"""Initialize light.group platform.""" """Initialize light.group platform."""
async_add_devices( async_add_devices([GroupLight(config.get(CONF_NAME),
[GroupLight(hass, config.get(CONF_NAME), config[CONF_ENTITIES])], True) config[CONF_ENTITIES])])
class GroupLight(light.Light): class GroupLight(light.Light):
"""Representation of a group light.""" """Representation of a group light."""
def __init__(self, hass: HomeAssistantType, name: str, def __init__(self, name: str, entity_ids: List[str]) -> None:
entity_ids: List[str]) -> None:
"""Initialize a group light.""" """Initialize a group light."""
self.hass = hass # type: HomeAssistantType
self._name = name # type: str self._name = name # type: str
self._entity_ids = entity_ids # type: List[str] self._entity_ids = entity_ids # type: List[str]
self._state = STATE_UNAVAILABLE # type: str self._state = STATE_UNAVAILABLE # type: str
@@ -163,15 +161,10 @@ class GroupLight(light.Light):
async def _async_send_message(self, service, **kwargs): async def _async_send_message(self, service, **kwargs):
"""Send a message to all entities in the group.""" """Send a message to all entities in the group."""
tasks = []
for entity_id in self._entity_ids: for entity_id in self._entity_ids:
payload = deepcopy(kwargs) payload = deepcopy(kwargs)
payload[ATTR_ENTITY_ID] = entity_id payload[ATTR_ENTITY_ID] = entity_id
tasks.append(self.hass.services.async_call( await self.hass.services.async_call(light.DOMAIN, service, payload)
light.DOMAIN, service, payload, blocking=True))
if tasks:
await asyncio.wait(tasks, loop=self.hass.loop)
async def async_turn_on(self, **kwargs): async def async_turn_on(self, **kwargs):
"""Forward the turn_on command to all lights in the group.""" """Forward the turn_on command to all lights in the group."""
@@ -191,10 +184,10 @@ class GroupLight(light.Light):
self._brightness = _reduce_attribute(on_states, ATTR_BRIGHTNESS) self._brightness = _reduce_attribute(on_states, ATTR_BRIGHTNESS)
self._xy_color = _reduce_attribute( self._xy_color = _reduce_attribute(
on_states, ATTR_XY_COLOR, reduce=_average_tuple) on_states, ATTR_XY_COLOR, reduce=_mean_tuple)
self._rgb_color = _reduce_attribute( self._rgb_color = _reduce_attribute(
on_states, ATTR_RGB_COLOR, reduce=_average_tuple) on_states, ATTR_RGB_COLOR, reduce=_mean_tuple)
if self._rgb_color is not None: if self._rgb_color is not None:
self._rgb_color = tuple(map(int, self._rgb_color)) self._rgb_color = tuple(map(int, self._rgb_color))
@@ -244,13 +237,13 @@ def _find_state_attributes(states: List[State],
yield value yield value
def _average_int(*args): def _mean_int(*args):
"""Return the average of the supplied values.""" """Return the mean of the supplied values."""
return int(sum(args) / len(args)) return int(sum(args) / len(args))
def _average_tuple(*args): def _mean_tuple(*args):
"""Return the average values along the columns of the supplied values.""" """Return the mean values along the columns of the supplied values."""
return tuple(sum(l) / len(l) for l in zip(*args)) return tuple(sum(l) / len(l) for l in zip(*args))
@@ -259,7 +252,7 @@ def _average_tuple(*args):
def _reduce_attribute(states: List[State], def _reduce_attribute(states: List[State],
key: str, key: str,
default: Optional[Any] = None, default: Optional[Any] = None,
reduce: Callable[..., Any] = _average_int) -> Any: reduce: Callable[..., Any] = _mean_int) -> Any:
"""Find the first attribute matching key from states. """Find the first attribute matching key from states.
If none are found, return default. If none are found, return default.