2019-04-03 15:40:03 +00:00
|
|
|
"""This platform allows several lights to be grouped into one light."""
|
2019-09-15 18:53:05 +00:00
|
|
|
import asyncio
|
2018-03-02 01:14:26 +00:00
|
|
|
from collections import Counter
|
2019-03-21 05:56:46 +00:00
|
|
|
import itertools
|
|
|
|
import logging
|
2019-09-29 17:07:49 +00:00
|
|
|
from typing import Any, Callable, Iterator, List, Optional, Tuple, cast
|
2018-03-02 01:14:26 +00:00
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components import light
|
2019-03-24 03:22:35 +00:00
|
|
|
from homeassistant.components.light import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_BRIGHTNESS,
|
|
|
|
ATTR_COLOR_TEMP,
|
|
|
|
ATTR_EFFECT,
|
|
|
|
ATTR_EFFECT_LIST,
|
|
|
|
ATTR_FLASH,
|
|
|
|
ATTR_HS_COLOR,
|
|
|
|
ATTR_MAX_MIREDS,
|
|
|
|
ATTR_MIN_MIREDS,
|
|
|
|
ATTR_TRANSITION,
|
|
|
|
ATTR_WHITE_VALUE,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
SUPPORT_BRIGHTNESS,
|
|
|
|
SUPPORT_COLOR,
|
|
|
|
SUPPORT_COLOR_TEMP,
|
|
|
|
SUPPORT_EFFECT,
|
|
|
|
SUPPORT_FLASH,
|
|
|
|
SUPPORT_TRANSITION,
|
|
|
|
SUPPORT_WHITE_VALUE,
|
|
|
|
)
|
2019-12-05 12:44:11 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
ATTR_SUPPORTED_FEATURES,
|
|
|
|
CONF_ENTITIES,
|
|
|
|
CONF_NAME,
|
|
|
|
STATE_ON,
|
|
|
|
STATE_UNAVAILABLE,
|
|
|
|
)
|
|
|
|
from homeassistant.core import CALLBACK_TYPE, State, callback
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2020-07-15 05:30:47 +00:00
|
|
|
from homeassistant.helpers.event import async_track_state_change_event
|
2019-12-05 12:44:11 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
|
|
|
|
from homeassistant.util import color as color_util
|
2019-09-29 17:07:49 +00:00
|
|
|
|
|
|
|
# mypy: allow-incomplete-defs, allow-untyped-calls, allow-untyped-defs
|
2019-10-19 18:35:57 +00:00
|
|
|
# mypy: no-check-untyped-defs
|
2019-09-29 17:07:49 +00:00
|
|
|
|
2018-03-02 01:14:26 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "Light Group"
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Required(CONF_ENTITIES): cv.entities_domain(light.DOMAIN),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
SUPPORT_GROUP_LIGHT = (
|
|
|
|
SUPPORT_BRIGHTNESS
|
|
|
|
| SUPPORT_COLOR_TEMP
|
|
|
|
| SUPPORT_EFFECT
|
|
|
|
| SUPPORT_FLASH
|
|
|
|
| SUPPORT_COLOR
|
|
|
|
| SUPPORT_TRANSITION
|
|
|
|
| SUPPORT_WHITE_VALUE
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistantType, config: ConfigType, async_add_entities, discovery_info=None
|
|
|
|
) -> None:
|
2018-03-02 01:14:26 +00:00
|
|
|
"""Initialize light.group platform."""
|
2019-09-29 17:07:49 +00:00
|
|
|
async_add_entities(
|
|
|
|
[LightGroup(cast(str, config.get(CONF_NAME)), config[CONF_ENTITIES])]
|
|
|
|
)
|
2018-03-02 01:14:26 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:49:41 +00:00
|
|
|
class LightGroup(light.LightEntity):
|
2018-03-09 14:15:39 +00:00
|
|
|
"""Representation of a light group."""
|
2018-03-02 01:14:26 +00:00
|
|
|
|
|
|
|
def __init__(self, name: str, entity_ids: List[str]) -> None:
|
2018-03-09 14:15:39 +00:00
|
|
|
"""Initialize a light group."""
|
2019-09-07 06:48:58 +00:00
|
|
|
self._name = name
|
|
|
|
self._entity_ids = entity_ids
|
|
|
|
self._is_on = False
|
|
|
|
self._available = False
|
|
|
|
self._brightness: Optional[int] = None
|
|
|
|
self._hs_color: Optional[Tuple[float, float]] = None
|
|
|
|
self._color_temp: Optional[int] = None
|
|
|
|
self._min_mireds: Optional[int] = 154
|
|
|
|
self._max_mireds: Optional[int] = 500
|
|
|
|
self._white_value: Optional[int] = None
|
|
|
|
self._effect_list: Optional[List[str]] = None
|
|
|
|
self._effect: Optional[str] = None
|
|
|
|
self._supported_features: int = 0
|
2019-10-28 20:36:26 +00:00
|
|
|
self._async_unsub_state_changed: Optional[CALLBACK_TYPE] = None
|
2018-03-02 01:14:26 +00:00
|
|
|
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""Register callbacks."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-03-02 01:14:26 +00:00
|
|
|
@callback
|
2020-07-15 05:30:47 +00:00
|
|
|
def async_state_changed_listener(*_):
|
2018-03-02 01:14:26 +00:00
|
|
|
"""Handle child updates."""
|
|
|
|
self.async_schedule_update_ha_state(True)
|
|
|
|
|
2019-10-28 20:36:26 +00:00
|
|
|
assert self.hass is not None
|
2020-07-15 05:30:47 +00:00
|
|
|
self._async_unsub_state_changed = async_track_state_change_event(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass, self._entity_ids, async_state_changed_listener
|
|
|
|
)
|
2018-03-09 14:15:39 +00:00
|
|
|
await self.async_update()
|
2018-03-02 01:14:26 +00:00
|
|
|
|
|
|
|
async def async_will_remove_from_hass(self):
|
2020-01-05 12:09:17 +00:00
|
|
|
"""Handle removal from Home Assistant."""
|
2018-03-09 14:15:39 +00:00
|
|
|
if self._async_unsub_state_changed is not None:
|
2018-03-02 01:14:26 +00:00
|
|
|
self._async_unsub_state_changed()
|
|
|
|
self._async_unsub_state_changed = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
|
|
|
"""Return the name of the entity."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
2018-03-09 14:15:39 +00:00
|
|
|
"""Return the on/off state of the light group."""
|
2018-03-02 01:14:26 +00:00
|
|
|
return self._is_on
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
2018-03-09 14:15:39 +00:00
|
|
|
"""Return whether the light group is available."""
|
2018-03-02 01:14:26 +00:00
|
|
|
return self._available
|
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self) -> Optional[int]:
|
2018-03-09 14:15:39 +00:00
|
|
|
"""Return the brightness of this light group between 0..255."""
|
2018-03-02 01:14:26 +00:00
|
|
|
return self._brightness
|
|
|
|
|
|
|
|
@property
|
2018-03-18 22:00:29 +00:00
|
|
|
def hs_color(self) -> Optional[Tuple[float, float]]:
|
|
|
|
"""Return the HS color value [float, float]."""
|
|
|
|
return self._hs_color
|
2018-03-02 01:14:26 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def color_temp(self) -> Optional[int]:
|
|
|
|
"""Return the CT color value in mireds."""
|
|
|
|
return self._color_temp
|
|
|
|
|
|
|
|
@property
|
|
|
|
def min_mireds(self) -> Optional[int]:
|
2018-03-09 14:15:39 +00:00
|
|
|
"""Return the coldest color_temp that this light group supports."""
|
2018-03-02 01:14:26 +00:00
|
|
|
return self._min_mireds
|
|
|
|
|
|
|
|
@property
|
|
|
|
def max_mireds(self) -> Optional[int]:
|
2018-03-09 14:15:39 +00:00
|
|
|
"""Return the warmest color_temp that this light group supports."""
|
2018-03-02 01:14:26 +00:00
|
|
|
return self._max_mireds
|
|
|
|
|
|
|
|
@property
|
|
|
|
def white_value(self) -> Optional[int]:
|
2018-03-09 14:15:39 +00:00
|
|
|
"""Return the white value of this light group between 0..255."""
|
2018-03-02 01:14:26 +00:00
|
|
|
return self._white_value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def effect_list(self) -> Optional[List[str]]:
|
|
|
|
"""Return the list of supported effects."""
|
|
|
|
return self._effect_list
|
|
|
|
|
|
|
|
@property
|
|
|
|
def effect(self) -> Optional[str]:
|
|
|
|
"""Return the current effect."""
|
|
|
|
return self._effect
|
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self) -> int:
|
|
|
|
"""Flag supported features."""
|
|
|
|
return self._supported_features
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self) -> bool:
|
2018-03-09 14:15:39 +00:00
|
|
|
"""No polling needed for a light group."""
|
2018-03-02 01:14:26 +00:00
|
|
|
return False
|
|
|
|
|
2020-06-06 04:23:52 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes for the light group."""
|
|
|
|
return {ATTR_ENTITY_ID: self._entity_ids}
|
|
|
|
|
2018-03-02 01:14:26 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2018-03-09 14:15:39 +00:00
|
|
|
"""Forward the turn_on command to all lights in the light group."""
|
2018-03-02 01:14:26 +00:00
|
|
|
data = {ATTR_ENTITY_ID: self._entity_ids}
|
2019-09-15 18:53:05 +00:00
|
|
|
emulate_color_temp_entity_ids = []
|
2018-03-02 01:14:26 +00:00
|
|
|
|
|
|
|
if ATTR_BRIGHTNESS in kwargs:
|
|
|
|
data[ATTR_BRIGHTNESS] = kwargs[ATTR_BRIGHTNESS]
|
|
|
|
|
2018-03-18 22:00:29 +00:00
|
|
|
if ATTR_HS_COLOR in kwargs:
|
|
|
|
data[ATTR_HS_COLOR] = kwargs[ATTR_HS_COLOR]
|
2018-03-02 01:14:26 +00:00
|
|
|
|
|
|
|
if ATTR_COLOR_TEMP in kwargs:
|
|
|
|
data[ATTR_COLOR_TEMP] = kwargs[ATTR_COLOR_TEMP]
|
|
|
|
|
2019-09-15 18:53:05 +00:00
|
|
|
# Create a new entity list to mutate
|
|
|
|
updated_entities = list(self._entity_ids)
|
|
|
|
|
|
|
|
# Walk through initial entity ids, split entity lists by support
|
|
|
|
for entity_id in self._entity_ids:
|
|
|
|
state = self.hass.states.get(entity_id)
|
|
|
|
if not state:
|
|
|
|
continue
|
|
|
|
support = state.attributes.get(ATTR_SUPPORTED_FEATURES)
|
|
|
|
# Only pass color temperature to supported entity_ids
|
|
|
|
if bool(support & SUPPORT_COLOR) and not bool(
|
|
|
|
support & SUPPORT_COLOR_TEMP
|
|
|
|
):
|
|
|
|
emulate_color_temp_entity_ids.append(entity_id)
|
|
|
|
updated_entities.remove(entity_id)
|
|
|
|
data[ATTR_ENTITY_ID] = updated_entities
|
|
|
|
|
2018-03-02 01:14:26 +00:00
|
|
|
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]
|
|
|
|
|
2019-09-15 18:53:05 +00:00
|
|
|
if not emulate_color_temp_entity_ids:
|
|
|
|
await self.hass.services.async_call(
|
2020-06-26 00:09:52 +00:00
|
|
|
light.DOMAIN,
|
|
|
|
light.SERVICE_TURN_ON,
|
|
|
|
data,
|
|
|
|
blocking=True,
|
|
|
|
context=self._context,
|
2019-09-15 18:53:05 +00:00
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
emulate_color_temp_data = data.copy()
|
|
|
|
temp_k = color_util.color_temperature_mired_to_kelvin(
|
|
|
|
emulate_color_temp_data[ATTR_COLOR_TEMP]
|
|
|
|
)
|
|
|
|
hs_color = color_util.color_temperature_to_hs(temp_k)
|
|
|
|
emulate_color_temp_data[ATTR_HS_COLOR] = hs_color
|
|
|
|
del emulate_color_temp_data[ATTR_COLOR_TEMP]
|
|
|
|
|
|
|
|
emulate_color_temp_data[ATTR_ENTITY_ID] = emulate_color_temp_entity_ids
|
|
|
|
|
|
|
|
await asyncio.gather(
|
|
|
|
self.hass.services.async_call(
|
2020-06-26 00:09:52 +00:00
|
|
|
light.DOMAIN,
|
|
|
|
light.SERVICE_TURN_ON,
|
|
|
|
data,
|
|
|
|
blocking=True,
|
|
|
|
context=self._context,
|
2019-09-15 18:53:05 +00:00
|
|
|
),
|
|
|
|
self.hass.services.async_call(
|
|
|
|
light.DOMAIN,
|
|
|
|
light.SERVICE_TURN_ON,
|
|
|
|
emulate_color_temp_data,
|
|
|
|
blocking=True,
|
2020-06-26 00:09:52 +00:00
|
|
|
context=self._context,
|
2019-09-15 18:53:05 +00:00
|
|
|
),
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-03-02 01:14:26 +00:00
|
|
|
|
|
|
|
async def async_turn_off(self, **kwargs):
|
2018-03-09 14:15:39 +00:00
|
|
|
"""Forward the turn_off command to all lights in the light group."""
|
2018-03-02 01:14:26 +00:00
|
|
|
data = {ATTR_ENTITY_ID: self._entity_ids}
|
|
|
|
|
|
|
|
if ATTR_TRANSITION in kwargs:
|
|
|
|
data[ATTR_TRANSITION] = kwargs[ATTR_TRANSITION]
|
|
|
|
|
|
|
|
await self.hass.services.async_call(
|
2020-06-26 00:09:52 +00:00
|
|
|
light.DOMAIN,
|
|
|
|
light.SERVICE_TURN_OFF,
|
|
|
|
data,
|
|
|
|
blocking=True,
|
|
|
|
context=self._context,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-03-02 01:14:26 +00:00
|
|
|
|
|
|
|
async def async_update(self):
|
2018-03-09 14:15:39 +00:00
|
|
|
"""Query all members and determine the light group state."""
|
2018-03-02 01:14:26 +00:00
|
|
|
all_states = [self.hass.states.get(x) for x in self._entity_ids]
|
2019-09-29 17:07:49 +00:00
|
|
|
states: List[State] = list(filter(None, all_states))
|
2018-03-02 01:14:26 +00:00
|
|
|
on_states = [state for state in states if state.state == STATE_ON]
|
|
|
|
|
|
|
|
self._is_on = len(on_states) > 0
|
2019-07-31 19:25:30 +00:00
|
|
|
self._available = any(state.state != STATE_UNAVAILABLE for state in states)
|
2018-03-02 01:14:26 +00:00
|
|
|
|
|
|
|
self._brightness = _reduce_attribute(on_states, ATTR_BRIGHTNESS)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self._hs_color = _reduce_attribute(on_states, ATTR_HS_COLOR, reduce=_mean_tuple)
|
2018-03-02 01:14:26 +00:00
|
|
|
|
|
|
|
self._white_value = _reduce_attribute(on_states, ATTR_WHITE_VALUE)
|
|
|
|
|
|
|
|
self._color_temp = _reduce_attribute(on_states, ATTR_COLOR_TEMP)
|
|
|
|
self._min_mireds = _reduce_attribute(
|
2019-07-31 19:25:30 +00:00
|
|
|
states, ATTR_MIN_MIREDS, default=154, reduce=min
|
|
|
|
)
|
2018-03-02 01:14:26 +00:00
|
|
|
self._max_mireds = _reduce_attribute(
|
2019-07-31 19:25:30 +00:00
|
|
|
states, ATTR_MAX_MIREDS, default=500, reduce=max
|
|
|
|
)
|
2018-03-02 01:14:26 +00:00
|
|
|
|
|
|
|
self._effect_list = None
|
2019-07-31 19:25:30 +00:00
|
|
|
all_effect_lists = list(_find_state_attributes(states, ATTR_EFFECT_LIST))
|
2018-03-02 01:14:26 +00:00
|
|
|
if all_effect_lists:
|
|
|
|
# Merge all effects from all effect_lists with a union merge.
|
|
|
|
self._effect_list = list(set().union(*all_effect_lists))
|
|
|
|
|
|
|
|
self._effect = None
|
|
|
|
all_effects = list(_find_state_attributes(on_states, ATTR_EFFECT))
|
|
|
|
if all_effects:
|
|
|
|
# Report the most common effect.
|
|
|
|
effects_count = Counter(itertools.chain(all_effects))
|
|
|
|
self._effect = effects_count.most_common(1)[0][0]
|
|
|
|
|
|
|
|
self._supported_features = 0
|
|
|
|
for support in _find_state_attributes(states, ATTR_SUPPORTED_FEATURES):
|
|
|
|
# Merge supported features by emulating support for every feature
|
|
|
|
# we find.
|
|
|
|
self._supported_features |= support
|
|
|
|
# Bitwise-and the supported features with the GroupedLight's features
|
|
|
|
# so that we don't break in the future when a new feature is added.
|
|
|
|
self._supported_features &= SUPPORT_GROUP_LIGHT
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def _find_state_attributes(states: List[State], key: str) -> Iterator[Any]:
|
2018-03-02 01:14:26 +00:00
|
|
|
"""Find attributes with matching key from states."""
|
|
|
|
for state in states:
|
|
|
|
value = state.attributes.get(key)
|
|
|
|
if value is not None:
|
|
|
|
yield value
|
|
|
|
|
|
|
|
|
|
|
|
def _mean_int(*args):
|
|
|
|
"""Return the mean of the supplied values."""
|
|
|
|
return int(sum(args) / len(args))
|
|
|
|
|
|
|
|
|
|
|
|
def _mean_tuple(*args):
|
|
|
|
"""Return the mean values along the columns of the supplied values."""
|
2020-04-30 19:37:58 +00:00
|
|
|
return tuple(sum(x) / len(x) for x in zip(*args))
|
2018-03-02 01:14:26 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def _reduce_attribute(
|
|
|
|
states: List[State],
|
|
|
|
key: str,
|
|
|
|
default: Optional[Any] = None,
|
|
|
|
reduce: Callable[..., Any] = _mean_int,
|
|
|
|
) -> Any:
|
2018-03-02 01:14:26 +00:00
|
|
|
"""Find the first attribute matching key from states.
|
|
|
|
|
|
|
|
If none are found, return default.
|
|
|
|
"""
|
|
|
|
attrs = list(_find_state_attributes(states, key))
|
|
|
|
|
|
|
|
if not attrs:
|
|
|
|
return default
|
|
|
|
|
|
|
|
if len(attrs) == 1:
|
|
|
|
return attrs[0]
|
|
|
|
|
|
|
|
return reduce(*attrs)
|