Python 3.5 magic

This commit is contained in:
Otto Winter
2018-02-20 11:30:07 +01:00
parent 21bf83e63a
commit f1a8fa63bd

View File

@@ -4,10 +4,10 @@ This component allows several lights to be grouped into one light.
For more details about this platform, please refer to the documentation at For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/light.group/ https://home-assistant.io/components/light.group/
""" """
import asyncio
import logging import logging
import itertools import itertools
from typing import List, Tuple, Optional, Iterator, Any from typing import List, Tuple, Optional, Iterator, Any
from collections import Counter
import voluptuous as vol import voluptuous as vol
@@ -41,11 +41,8 @@ SUPPORT_GROUP_LIGHT = (SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP | SUPPORT_EFFECT
| SUPPORT_XY_COLOR | SUPPORT_WHITE_VALUE) | SUPPORT_XY_COLOR | SUPPORT_WHITE_VALUE)
@asyncio.coroutine async def async_setup_platform(hass: HomeAssistantType, config: ConfigType,
def async_setup_platform(hass: HomeAssistantType, async_add_devices, discovery_info=None) -> None:
config: ConfigType,
async_add_devices,
discovery_info=None) -> None:
"""Initialize light.group platform.""" """Initialize light.group platform."""
async_add_devices( async_add_devices(
[GroupLight(hass, config.get(CONF_NAME), config[CONF_ENTITIES])]) [GroupLight(hass, config.get(CONF_NAME), config[CONF_ENTITIES])])
@@ -72,8 +69,7 @@ class GroupLight(light.Light):
self._effect = None # type: Optional[str] self._effect = None # type: Optional[str]
self._supported_features = 0 # type: int self._supported_features = 0 # type: int
@asyncio.coroutine async def async_added_to_hass(self) -> None:
def async_added_to_hass(self) -> None:
"""Register callbacks""" """Register callbacks"""
@callback @callback
@@ -106,7 +102,7 @@ class GroupLight(light.Light):
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return True if entity is on.""" """Return True if entity is on."""
return self.state == STATE_ON return self._state == STATE_ON
@property @property
def brightness(self) -> Optional[int]: def brightness(self) -> Optional[int]:
@@ -163,24 +159,21 @@ class GroupLight(light.Light):
"""No polling needed for a group light.""" """No polling needed for a group light."""
return False return False
@asyncio.coroutine async def async_turn_on(self, **kwargs):
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."""
for entity_id in self._entity_ids: for entity_id in self._entity_ids:
kwargs[ATTR_ENTITY_ID] = entity_id kwargs[ATTR_ENTITY_ID] = entity_id
yield from self.hass.services.async_call( await self.hass.services.async_call(
'light', SERVICE_TURN_ON, kwargs, blocking=True) 'light', SERVICE_TURN_ON, kwargs, blocking=True)
@asyncio.coroutine async def async_turn_off(self, **kwargs):
def async_turn_off(self, **kwargs):
"""Forward the turn_off command to all lights in the group.""" """Forward the turn_off command to all lights in the group."""
for entity_id in self._entity_ids: for entity_id in self._entity_ids:
kwargs[ATTR_ENTITY_ID] = entity_id kwargs[ATTR_ENTITY_ID] = entity_id
yield from self.hass.services.async_call( await self.hass.services.async_call(
'light', SERVICE_TURN_OFF, kwargs, blocking=True) 'light', SERVICE_TURN_OFF, kwargs, blocking=True)
@asyncio.coroutine async def async_update(self):
def async_update(self):
"""Query all members and determine the group state.""" """Query all members and determine the group state."""
states = self._child_states() states = self._child_states()
@@ -207,7 +200,8 @@ class GroupLight(light.Light):
if all_effects: if all_effects:
flat_effects = list(itertools.chain(*all_effect_lists)) flat_effects = list(itertools.chain(*all_effect_lists))
# Report the most common effect. # Report the most common effect.
self._effect = max(set(flat_effects), key=flat_effects.count) effects_count = Counter(flat_effects)
self._effect = effects_count.most_common(1)[0][0]
self._supported_features = 0 self._supported_features = 0
for support in _find_state_attributes( for support in _find_state_attributes(
@@ -230,7 +224,7 @@ def _find_state_attributes(states: List[State],
force_on: bool = True) -> Iterator[Any]: force_on: bool = True) -> Iterator[Any]:
"""Find attributes with matching key from states. """Find attributes with matching key from states.
Only returns attributes of enabled lights when force_on is True. Only return attributes of enabled lights when force_on is True.
""" """
for state in states: for state in states:
assume_on = (not force_on) or state.state == STATE_ON assume_on = (not force_on) or state.state == STATE_ON
@@ -244,7 +238,7 @@ def _reduce_attribute(states: List[State],
force_on: bool = True) -> Any: force_on: bool = True) -> Any:
"""Find the first attribute matching key from states. """Find the first attribute matching key from states.
If none are found, returns default. If none are found, return default.
""" """
return next(_find_state_attributes(states, key, force_on), default) return next(_find_state_attributes(states, key, force_on), default)