2019-04-03 15:40:03 +00:00
|
|
|
"""This platform allows several cover to be grouped into one cover."""
|
2021-03-18 07:02:55 +00:00
|
|
|
from __future__ import annotations
|
2018-03-15 11:31:31 +00:00
|
|
|
|
2021-06-16 12:47:29 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2018-03-15 11:31:31 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-03-24 03:22:35 +00:00
|
|
|
from homeassistant.components.cover import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_CURRENT_POSITION,
|
|
|
|
ATTR_CURRENT_TILT_POSITION,
|
|
|
|
ATTR_POSITION,
|
|
|
|
ATTR_TILT_POSITION,
|
|
|
|
DOMAIN,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
SERVICE_CLOSE_COVER,
|
|
|
|
SERVICE_CLOSE_COVER_TILT,
|
|
|
|
SERVICE_OPEN_COVER,
|
|
|
|
SERVICE_OPEN_COVER_TILT,
|
|
|
|
SERVICE_SET_COVER_POSITION,
|
|
|
|
SERVICE_SET_COVER_TILT_POSITION,
|
|
|
|
SERVICE_STOP_COVER,
|
|
|
|
SERVICE_STOP_COVER_TILT,
|
|
|
|
SUPPORT_CLOSE,
|
|
|
|
SUPPORT_CLOSE_TILT,
|
|
|
|
SUPPORT_OPEN,
|
|
|
|
SUPPORT_OPEN_TILT,
|
|
|
|
SUPPORT_SET_POSITION,
|
|
|
|
SUPPORT_SET_TILT_POSITION,
|
|
|
|
SUPPORT_STOP,
|
|
|
|
SUPPORT_STOP_TILT,
|
2020-04-25 16:07:15 +00:00
|
|
|
CoverEntity,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-12-05 12:44:11 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ASSUMED_STATE,
|
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
ATTR_SUPPORTED_FEATURES,
|
|
|
|
CONF_ENTITIES,
|
|
|
|
CONF_NAME,
|
2021-07-20 11:56:23 +00:00
|
|
|
CONF_UNIQUE_ID,
|
2020-06-03 16:44:04 +00:00
|
|
|
STATE_CLOSING,
|
|
|
|
STATE_OPEN,
|
|
|
|
STATE_OPENING,
|
2019-12-05 12:44:11 +00:00
|
|
|
)
|
2021-06-16 12:47:29 +00:00
|
|
|
from homeassistant.core import CoreState, Event, HomeAssistant, State
|
2019-12-05 12:44:11 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2021-06-16 12:47:29 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-07-15 05:30:47 +00:00
|
|
|
from homeassistant.helpers.event import async_track_state_change_event
|
2021-06-16 12:47:29 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2019-09-29 17:07:49 +00:00
|
|
|
|
2020-08-25 22:22:10 +00:00
|
|
|
from . import GroupEntity
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
KEY_OPEN_CLOSE = "open_close"
|
|
|
|
KEY_STOP = "stop"
|
|
|
|
KEY_POSITION = "position"
|
2018-03-15 11:31:31 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "Cover Group"
|
2018-03-15 11:31:31 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_ENTITIES): cv.entities_domain(DOMAIN),
|
2021-07-20 11:56:23 +00:00
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
)
|
2018-03-15 11:31:31 +00:00
|
|
|
|
|
|
|
|
2021-06-16 12:47:29 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: dict[str, Any] | None = None,
|
|
|
|
) -> None:
|
2018-03-15 11:31:31 +00:00
|
|
|
"""Set up the Group Cover platform."""
|
2021-07-20 11:56:23 +00:00
|
|
|
async_add_entities(
|
|
|
|
[
|
|
|
|
CoverGroup(
|
|
|
|
config.get(CONF_UNIQUE_ID), config[CONF_NAME], config[CONF_ENTITIES]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
2018-03-15 11:31:31 +00:00
|
|
|
|
|
|
|
|
2020-08-25 22:22:10 +00:00
|
|
|
class CoverGroup(GroupEntity, CoverEntity):
|
2018-03-15 11:31:31 +00:00
|
|
|
"""Representation of a CoverGroup."""
|
|
|
|
|
2021-06-16 12:47:29 +00:00
|
|
|
_attr_is_closed: bool | None = False
|
|
|
|
_attr_is_opening: bool | None = False
|
|
|
|
_attr_is_closing: bool | None = False
|
|
|
|
_attr_current_cover_position: int | None = 100
|
|
|
|
_attr_assumed_state: bool = True
|
2018-03-15 11:31:31 +00:00
|
|
|
|
2021-07-20 11:56:23 +00:00
|
|
|
def __init__(self, unique_id: str | None, name: str, entities: list[str]) -> None:
|
2021-06-16 12:47:29 +00:00
|
|
|
"""Initialize a CoverGroup entity."""
|
2018-03-15 11:31:31 +00:00
|
|
|
self._entities = entities
|
2021-03-18 07:02:55 +00:00
|
|
|
self._covers: dict[str, set[str]] = {
|
2019-09-29 17:07:49 +00:00
|
|
|
KEY_OPEN_CLOSE: set(),
|
|
|
|
KEY_STOP: set(),
|
|
|
|
KEY_POSITION: set(),
|
|
|
|
}
|
2021-03-18 07:02:55 +00:00
|
|
|
self._tilts: dict[str, set[str]] = {
|
2019-09-29 17:07:49 +00:00
|
|
|
KEY_OPEN_CLOSE: set(),
|
|
|
|
KEY_STOP: set(),
|
|
|
|
KEY_POSITION: set(),
|
|
|
|
}
|
2018-03-15 11:31:31 +00:00
|
|
|
|
2021-06-16 12:47:29 +00:00
|
|
|
self._attr_name = name
|
|
|
|
self._attr_extra_state_attributes = {ATTR_ENTITY_ID: entities}
|
2021-07-20 11:56:23 +00:00
|
|
|
self._attr_unique_id = unique_id
|
2021-06-16 12:47:29 +00:00
|
|
|
|
|
|
|
async def _update_supported_features_event(self, event: Event) -> None:
|
2020-08-25 22:22:10 +00:00
|
|
|
self.async_set_context(event.context)
|
2021-06-16 12:47:29 +00:00
|
|
|
entity = event.data.get("entity_id")
|
|
|
|
if entity is not None:
|
|
|
|
await self.async_update_supported_features(
|
|
|
|
entity, event.data.get("new_state")
|
|
|
|
)
|
2020-07-15 05:30:47 +00:00
|
|
|
|
2020-08-25 22:22:10 +00:00
|
|
|
async def async_update_supported_features(
|
2020-08-27 11:56:20 +00:00
|
|
|
self,
|
|
|
|
entity_id: str,
|
2021-03-18 07:02:55 +00:00
|
|
|
new_state: State | None,
|
2020-08-27 11:56:20 +00:00
|
|
|
update_state: bool = True,
|
2019-09-29 17:07:49 +00:00
|
|
|
) -> None:
|
2018-03-15 11:31:31 +00:00
|
|
|
"""Update dictionaries with supported features."""
|
|
|
|
if not new_state:
|
|
|
|
for values in self._covers.values():
|
|
|
|
values.discard(entity_id)
|
|
|
|
for values in self._tilts.values():
|
|
|
|
values.discard(entity_id)
|
|
|
|
if update_state:
|
2020-08-25 22:22:10 +00:00
|
|
|
await self.async_defer_or_update_ha_state()
|
2018-03-15 11:31:31 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
features = new_state.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
|
|
|
|
|
|
|
|
if features & (SUPPORT_OPEN | SUPPORT_CLOSE):
|
|
|
|
self._covers[KEY_OPEN_CLOSE].add(entity_id)
|
|
|
|
else:
|
|
|
|
self._covers[KEY_OPEN_CLOSE].discard(entity_id)
|
|
|
|
if features & (SUPPORT_STOP):
|
|
|
|
self._covers[KEY_STOP].add(entity_id)
|
|
|
|
else:
|
|
|
|
self._covers[KEY_STOP].discard(entity_id)
|
|
|
|
if features & (SUPPORT_SET_POSITION):
|
|
|
|
self._covers[KEY_POSITION].add(entity_id)
|
|
|
|
else:
|
|
|
|
self._covers[KEY_POSITION].discard(entity_id)
|
|
|
|
|
|
|
|
if features & (SUPPORT_OPEN_TILT | SUPPORT_CLOSE_TILT):
|
|
|
|
self._tilts[KEY_OPEN_CLOSE].add(entity_id)
|
|
|
|
else:
|
|
|
|
self._tilts[KEY_OPEN_CLOSE].discard(entity_id)
|
|
|
|
if features & (SUPPORT_STOP_TILT):
|
|
|
|
self._tilts[KEY_STOP].add(entity_id)
|
|
|
|
else:
|
|
|
|
self._tilts[KEY_STOP].discard(entity_id)
|
|
|
|
if features & (SUPPORT_SET_TILT_POSITION):
|
|
|
|
self._tilts[KEY_POSITION].add(entity_id)
|
|
|
|
else:
|
|
|
|
self._tilts[KEY_POSITION].discard(entity_id)
|
|
|
|
|
|
|
|
if update_state:
|
2020-08-25 22:22:10 +00:00
|
|
|
await self.async_defer_or_update_ha_state()
|
2018-03-15 11:31:31 +00:00
|
|
|
|
2021-06-16 12:47:29 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
2018-03-15 11:31:31 +00:00
|
|
|
"""Register listeners."""
|
|
|
|
for entity_id in self._entities:
|
|
|
|
new_state = self.hass.states.get(entity_id)
|
2020-11-14 20:46:24 +00:00
|
|
|
if new_state is None:
|
|
|
|
continue
|
2020-08-25 22:22:10 +00:00
|
|
|
await self.async_update_supported_features(
|
|
|
|
entity_id, new_state, update_state=False
|
|
|
|
)
|
|
|
|
self.async_on_remove(
|
|
|
|
async_track_state_change_event(
|
|
|
|
self.hass, self._entities, self._update_supported_features_event
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-09-23 18:55:32 +00:00
|
|
|
|
|
|
|
if self.hass.state == CoreState.running:
|
|
|
|
await self.async_update()
|
|
|
|
return
|
2020-08-25 22:22:10 +00:00
|
|
|
await super().async_added_to_hass()
|
2018-03-15 11:31:31 +00:00
|
|
|
|
2021-06-16 12:47:29 +00:00
|
|
|
async def async_open_cover(self, **kwargs: Any) -> None:
|
2018-03-15 11:31:31 +00:00
|
|
|
"""Move the covers up."""
|
|
|
|
data = {ATTR_ENTITY_ID: self._covers[KEY_OPEN_CLOSE]}
|
|
|
|
await self.hass.services.async_call(
|
2020-06-26 00:09:52 +00:00
|
|
|
DOMAIN, SERVICE_OPEN_COVER, data, blocking=True, context=self._context
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-03-15 11:31:31 +00:00
|
|
|
|
2021-06-16 12:47:29 +00:00
|
|
|
async def async_close_cover(self, **kwargs: Any) -> None:
|
2018-03-15 11:31:31 +00:00
|
|
|
"""Move the covers down."""
|
|
|
|
data = {ATTR_ENTITY_ID: self._covers[KEY_OPEN_CLOSE]}
|
|
|
|
await self.hass.services.async_call(
|
2020-06-26 00:09:52 +00:00
|
|
|
DOMAIN, SERVICE_CLOSE_COVER, data, blocking=True, context=self._context
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-03-15 11:31:31 +00:00
|
|
|
|
2021-06-16 12:47:29 +00:00
|
|
|
async def async_stop_cover(self, **kwargs: Any) -> None:
|
2018-03-15 11:31:31 +00:00
|
|
|
"""Fire the stop action."""
|
|
|
|
data = {ATTR_ENTITY_ID: self._covers[KEY_STOP]}
|
|
|
|
await self.hass.services.async_call(
|
2020-06-26 00:09:52 +00:00
|
|
|
DOMAIN, SERVICE_STOP_COVER, data, blocking=True, context=self._context
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-03-15 11:31:31 +00:00
|
|
|
|
2021-06-16 12:47:29 +00:00
|
|
|
async def async_set_cover_position(self, **kwargs: Any) -> None:
|
2018-03-15 11:31:31 +00:00
|
|
|
"""Set covers position."""
|
2019-07-31 19:25:30 +00:00
|
|
|
data = {
|
|
|
|
ATTR_ENTITY_ID: self._covers[KEY_POSITION],
|
|
|
|
ATTR_POSITION: kwargs[ATTR_POSITION],
|
|
|
|
}
|
2018-03-15 11:31:31 +00:00
|
|
|
await self.hass.services.async_call(
|
2020-06-26 00:09:52 +00:00
|
|
|
DOMAIN,
|
|
|
|
SERVICE_SET_COVER_POSITION,
|
|
|
|
data,
|
|
|
|
blocking=True,
|
|
|
|
context=self._context,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-03-15 11:31:31 +00:00
|
|
|
|
2021-06-16 12:47:29 +00:00
|
|
|
async def async_open_cover_tilt(self, **kwargs: Any) -> None:
|
2018-03-15 11:31:31 +00:00
|
|
|
"""Tilt covers open."""
|
|
|
|
data = {ATTR_ENTITY_ID: self._tilts[KEY_OPEN_CLOSE]}
|
|
|
|
await self.hass.services.async_call(
|
2020-06-26 00:09:52 +00:00
|
|
|
DOMAIN, SERVICE_OPEN_COVER_TILT, data, blocking=True, context=self._context
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-03-15 11:31:31 +00:00
|
|
|
|
2021-06-16 12:47:29 +00:00
|
|
|
async def async_close_cover_tilt(self, **kwargs: Any) -> None:
|
2018-03-15 11:31:31 +00:00
|
|
|
"""Tilt covers closed."""
|
|
|
|
data = {ATTR_ENTITY_ID: self._tilts[KEY_OPEN_CLOSE]}
|
|
|
|
await self.hass.services.async_call(
|
2020-06-26 00:09:52 +00:00
|
|
|
DOMAIN, SERVICE_CLOSE_COVER_TILT, data, blocking=True, context=self._context
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-03-15 11:31:31 +00:00
|
|
|
|
2021-06-16 12:47:29 +00:00
|
|
|
async def async_stop_cover_tilt(self, **kwargs: Any) -> None:
|
2018-03-15 11:31:31 +00:00
|
|
|
"""Stop cover tilt."""
|
|
|
|
data = {ATTR_ENTITY_ID: self._tilts[KEY_STOP]}
|
|
|
|
await self.hass.services.async_call(
|
2020-06-26 00:09:52 +00:00
|
|
|
DOMAIN, SERVICE_STOP_COVER_TILT, data, blocking=True, context=self._context
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-03-15 11:31:31 +00:00
|
|
|
|
2021-06-16 12:47:29 +00:00
|
|
|
async def async_set_cover_tilt_position(self, **kwargs: Any) -> None:
|
2018-03-15 11:31:31 +00:00
|
|
|
"""Set tilt position."""
|
2019-07-31 19:25:30 +00:00
|
|
|
data = {
|
|
|
|
ATTR_ENTITY_ID: self._tilts[KEY_POSITION],
|
|
|
|
ATTR_TILT_POSITION: kwargs[ATTR_TILT_POSITION],
|
|
|
|
}
|
2018-03-15 11:31:31 +00:00
|
|
|
await self.hass.services.async_call(
|
2020-06-26 00:09:52 +00:00
|
|
|
DOMAIN,
|
|
|
|
SERVICE_SET_COVER_TILT_POSITION,
|
|
|
|
data,
|
|
|
|
blocking=True,
|
|
|
|
context=self._context,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-03-15 11:31:31 +00:00
|
|
|
|
2021-06-16 12:47:29 +00:00
|
|
|
async def async_update(self) -> None:
|
2018-03-15 11:31:31 +00:00
|
|
|
"""Update state and attributes."""
|
2021-06-16 12:47:29 +00:00
|
|
|
self._attr_assumed_state = False
|
2018-03-15 11:31:31 +00:00
|
|
|
|
2021-06-16 12:47:29 +00:00
|
|
|
self._attr_is_closed = True
|
|
|
|
self._attr_is_closing = False
|
|
|
|
self._attr_is_opening = False
|
2018-03-15 11:31:31 +00:00
|
|
|
for entity_id in self._entities:
|
|
|
|
state = self.hass.states.get(entity_id)
|
|
|
|
if not state:
|
|
|
|
continue
|
2020-06-03 16:44:04 +00:00
|
|
|
if state.state == STATE_OPEN:
|
2021-06-16 12:47:29 +00:00
|
|
|
self._attr_is_closed = False
|
2018-03-15 11:31:31 +00:00
|
|
|
break
|
2020-06-03 16:44:04 +00:00
|
|
|
if state.state == STATE_CLOSING:
|
2021-06-16 12:47:29 +00:00
|
|
|
self._attr_is_closing = True
|
2020-06-03 16:44:04 +00:00
|
|
|
break
|
|
|
|
if state.state == STATE_OPENING:
|
2021-06-16 12:47:29 +00:00
|
|
|
self._attr_is_opening = True
|
2020-06-03 16:44:04 +00:00
|
|
|
break
|
2018-03-15 11:31:31 +00:00
|
|
|
|
2021-06-16 12:47:29 +00:00
|
|
|
self._attr_current_cover_position = None
|
2018-03-15 11:31:31 +00:00
|
|
|
if self._covers[KEY_POSITION]:
|
2021-06-16 12:47:29 +00:00
|
|
|
position: int | None = -1
|
|
|
|
self._attr_current_cover_position = 0 if self.is_closed else 100
|
2018-03-15 11:31:31 +00:00
|
|
|
for entity_id in self._covers[KEY_POSITION]:
|
|
|
|
state = self.hass.states.get(entity_id)
|
2020-11-14 20:46:24 +00:00
|
|
|
if state is None:
|
|
|
|
continue
|
2018-03-15 11:31:31 +00:00
|
|
|
pos = state.attributes.get(ATTR_CURRENT_POSITION)
|
|
|
|
if position == -1:
|
|
|
|
position = pos
|
|
|
|
elif position != pos:
|
2021-06-16 12:47:29 +00:00
|
|
|
self._attr_assumed_state = True
|
2018-03-15 11:31:31 +00:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
if position != -1:
|
2021-06-16 12:47:29 +00:00
|
|
|
self._attr_current_cover_position = position
|
2018-03-15 11:31:31 +00:00
|
|
|
|
2021-06-16 12:47:29 +00:00
|
|
|
self._attr_current_cover_tilt_position = None
|
2018-03-15 11:31:31 +00:00
|
|
|
if self._tilts[KEY_POSITION]:
|
|
|
|
position = -1
|
2021-06-16 12:47:29 +00:00
|
|
|
self._attr_current_cover_tilt_position = 100
|
2018-03-15 11:31:31 +00:00
|
|
|
for entity_id in self._tilts[KEY_POSITION]:
|
|
|
|
state = self.hass.states.get(entity_id)
|
2020-11-14 20:46:24 +00:00
|
|
|
if state is None:
|
|
|
|
continue
|
2018-03-15 11:31:31 +00:00
|
|
|
pos = state.attributes.get(ATTR_CURRENT_TILT_POSITION)
|
|
|
|
if position == -1:
|
|
|
|
position = pos
|
|
|
|
elif position != pos:
|
2021-06-16 12:47:29 +00:00
|
|
|
self._attr_assumed_state = True
|
2018-03-15 11:31:31 +00:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
if position != -1:
|
2021-06-16 12:47:29 +00:00
|
|
|
self._attr_current_cover_tilt_position = position
|
2018-03-15 11:31:31 +00:00
|
|
|
|
|
|
|
supported_features = 0
|
2019-07-31 19:25:30 +00:00
|
|
|
supported_features |= (
|
|
|
|
SUPPORT_OPEN | SUPPORT_CLOSE if self._covers[KEY_OPEN_CLOSE] else 0
|
|
|
|
)
|
|
|
|
supported_features |= SUPPORT_STOP if self._covers[KEY_STOP] else 0
|
|
|
|
supported_features |= SUPPORT_SET_POSITION if self._covers[KEY_POSITION] else 0
|
|
|
|
supported_features |= (
|
|
|
|
SUPPORT_OPEN_TILT | SUPPORT_CLOSE_TILT if self._tilts[KEY_OPEN_CLOSE] else 0
|
|
|
|
)
|
|
|
|
supported_features |= SUPPORT_STOP_TILT if self._tilts[KEY_STOP] else 0
|
|
|
|
supported_features |= (
|
|
|
|
SUPPORT_SET_TILT_POSITION if self._tilts[KEY_POSITION] else 0
|
|
|
|
)
|
2021-06-16 12:47:29 +00:00
|
|
|
self._attr_supported_features = supported_features
|
2018-03-15 11:31:31 +00:00
|
|
|
|
2021-06-16 12:47:29 +00:00
|
|
|
if not self._attr_assumed_state:
|
2018-03-15 11:31:31 +00:00
|
|
|
for entity_id in self._entities:
|
|
|
|
state = self.hass.states.get(entity_id)
|
2020-11-14 20:46:24 +00:00
|
|
|
if state is None:
|
|
|
|
continue
|
2018-03-15 11:31:31 +00:00
|
|
|
if state and state.attributes.get(ATTR_ASSUMED_STATE):
|
2021-06-16 12:47:29 +00:00
|
|
|
self._attr_assumed_state = True
|
2018-03-15 11:31:31 +00:00
|
|
|
break
|