2019-02-13 20:21:14 +00:00
|
|
|
"""Support for deCONZ covers."""
|
2021-11-17 14:08:37 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-11-19 20:16:21 +00:00
|
|
|
from typing import Any, cast
|
2021-11-17 14:08:37 +00:00
|
|
|
|
2022-07-06 22:31:47 +00:00
|
|
|
from pydeconz.interfaces.lights import CoverAction
|
2022-05-11 11:19:28 +00:00
|
|
|
from pydeconz.models.event import EventType
|
2022-04-24 08:27:56 +00:00
|
|
|
from pydeconz.models.light.cover import Cover
|
2021-09-29 19:19:21 +00:00
|
|
|
|
2018-09-21 17:59:20 +00:00
|
|
|
from homeassistant.components.cover import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_POSITION,
|
2020-11-24 20:42:11 +00:00
|
|
|
ATTR_TILT_POSITION,
|
2020-09-25 20:49:28 +00:00
|
|
|
DOMAIN,
|
2021-12-09 12:23:01 +00:00
|
|
|
CoverDeviceClass,
|
2020-04-25 16:07:15 +00:00
|
|
|
CoverEntity,
|
2022-04-05 22:00:37 +00:00
|
|
|
CoverEntityFeature,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-11-17 14:08:37 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2018-09-21 17:59:20 +00:00
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
from .deconz_device import DeconzDevice
|
2021-11-17 14:08:37 +00:00
|
|
|
from .gateway import DeconzGateway, get_gateway_from_config_entry
|
2019-01-15 18:29:56 +00:00
|
|
|
|
2022-07-06 22:31:47 +00:00
|
|
|
DECONZ_TYPE_TO_DEVICE_CLASS = {
|
2021-12-09 12:23:01 +00:00
|
|
|
"Level controllable output": CoverDeviceClass.DAMPER,
|
|
|
|
"Window covering controller": CoverDeviceClass.SHADE,
|
|
|
|
"Window covering device": CoverDeviceClass.SHADE,
|
2021-06-23 19:40:34 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 17:59:20 +00:00
|
|
|
|
2021-11-17 14:08:37 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-11-23 10:37:11 +00:00
|
|
|
"""Set up covers for deCONZ component."""
|
2019-04-05 00:48:24 +00:00
|
|
|
gateway = get_gateway_from_config_entry(hass, config_entry)
|
2020-09-25 20:49:28 +00:00
|
|
|
gateway.entities[DOMAIN] = set()
|
2018-11-05 15:21:44 +00:00
|
|
|
|
2018-09-21 17:59:20 +00:00
|
|
|
@callback
|
2022-05-11 11:19:28 +00:00
|
|
|
def async_add_cover(_: EventType, cover_id: str) -> None:
|
2018-09-21 17:59:20 +00:00
|
|
|
"""Add cover from deCONZ."""
|
2022-07-06 22:31:47 +00:00
|
|
|
async_add_entities([DeconzCover(cover_id, gateway)])
|
2018-09-21 17:59:20 +00:00
|
|
|
|
2022-05-25 03:48:09 +00:00
|
|
|
gateway.register_platform_add_device_callback(
|
|
|
|
async_add_cover,
|
|
|
|
gateway.api.lights.covers,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-09-21 17:59:20 +00:00
|
|
|
|
|
|
|
|
2020-04-25 16:07:15 +00:00
|
|
|
class DeconzCover(DeconzDevice, CoverEntity):
|
2018-09-21 17:59:20 +00:00
|
|
|
"""Representation of a deCONZ cover."""
|
|
|
|
|
2020-09-25 20:49:28 +00:00
|
|
|
TYPE = DOMAIN
|
2021-11-17 14:08:37 +00:00
|
|
|
_device: Cover
|
2020-09-25 20:49:28 +00:00
|
|
|
|
2022-07-06 22:31:47 +00:00
|
|
|
def __init__(self, cover_id: str, gateway: DeconzGateway) -> None:
|
2019-02-18 16:43:22 +00:00
|
|
|
"""Set up cover device."""
|
2022-07-06 22:31:47 +00:00
|
|
|
super().__init__(cover := gateway.api.lights.covers[cover_id], gateway)
|
2018-11-05 15:21:44 +00:00
|
|
|
|
2022-04-05 22:00:37 +00:00
|
|
|
self._attr_supported_features = CoverEntityFeature.OPEN
|
|
|
|
self._attr_supported_features |= CoverEntityFeature.CLOSE
|
|
|
|
self._attr_supported_features |= CoverEntityFeature.STOP
|
|
|
|
self._attr_supported_features |= CoverEntityFeature.SET_POSITION
|
2018-09-21 17:59:20 +00:00
|
|
|
|
2020-11-24 20:42:11 +00:00
|
|
|
if self._device.tilt is not None:
|
2022-04-05 22:00:37 +00:00
|
|
|
self._attr_supported_features |= CoverEntityFeature.OPEN_TILT
|
|
|
|
self._attr_supported_features |= CoverEntityFeature.CLOSE_TILT
|
|
|
|
self._attr_supported_features |= CoverEntityFeature.STOP_TILT
|
|
|
|
self._attr_supported_features |= CoverEntityFeature.SET_TILT_POSITION
|
2018-09-21 17:59:20 +00:00
|
|
|
|
2022-07-06 22:31:47 +00:00
|
|
|
self._attr_device_class = DECONZ_TYPE_TO_DEVICE_CLASS.get(cover.type)
|
2018-09-21 17:59:20 +00:00
|
|
|
|
|
|
|
@property
|
2021-11-17 14:08:37 +00:00
|
|
|
def current_cover_position(self) -> int:
|
2020-11-24 20:42:11 +00:00
|
|
|
"""Return the current position of the cover."""
|
2022-04-23 05:27:47 +00:00
|
|
|
return 100 - self._device.lift
|
2020-11-24 20:42:11 +00:00
|
|
|
|
|
|
|
@property
|
2021-11-17 14:08:37 +00:00
|
|
|
def is_closed(self) -> bool:
|
2020-11-24 20:42:11 +00:00
|
|
|
"""Return if the cover is closed."""
|
|
|
|
return not self._device.is_open
|
2018-09-21 17:59:20 +00:00
|
|
|
|
2021-11-19 20:16:21 +00:00
|
|
|
async def async_set_cover_position(self, **kwargs: Any) -> None:
|
2018-09-21 17:59:20 +00:00
|
|
|
"""Move the cover to a specific position."""
|
2021-11-19 20:16:21 +00:00
|
|
|
position = 100 - cast(int, kwargs[ATTR_POSITION])
|
2022-07-06 22:31:47 +00:00
|
|
|
await self.gateway.api.lights.covers.set_state(
|
|
|
|
id=self._device.resource_id,
|
|
|
|
lift=position,
|
|
|
|
)
|
2018-09-21 17:59:20 +00:00
|
|
|
|
2021-11-17 14:08:37 +00:00
|
|
|
async def async_open_cover(self, **kwargs: Any) -> None:
|
2018-09-21 17:59:20 +00:00
|
|
|
"""Open cover."""
|
2022-07-06 22:31:47 +00:00
|
|
|
await self.gateway.api.lights.covers.set_state(
|
|
|
|
id=self._device.resource_id,
|
|
|
|
action=CoverAction.OPEN,
|
|
|
|
)
|
2018-09-21 17:59:20 +00:00
|
|
|
|
2021-11-17 14:08:37 +00:00
|
|
|
async def async_close_cover(self, **kwargs: Any) -> None:
|
2018-09-21 17:59:20 +00:00
|
|
|
"""Close cover."""
|
2022-07-06 22:31:47 +00:00
|
|
|
await self.gateway.api.lights.covers.set_state(
|
|
|
|
id=self._device.resource_id,
|
|
|
|
action=CoverAction.CLOSE,
|
|
|
|
)
|
2018-09-21 17:59:20 +00:00
|
|
|
|
2021-11-17 14:08:37 +00:00
|
|
|
async def async_stop_cover(self, **kwargs: Any) -> None:
|
2018-10-20 13:13:23 +00:00
|
|
|
"""Stop cover."""
|
2022-07-06 22:31:47 +00:00
|
|
|
await self.gateway.api.lights.covers.set_state(
|
|
|
|
id=self._device.resource_id,
|
|
|
|
action=CoverAction.STOP,
|
|
|
|
)
|
2020-11-24 20:42:11 +00:00
|
|
|
|
|
|
|
@property
|
2021-11-17 14:08:37 +00:00
|
|
|
def current_cover_tilt_position(self) -> int | None:
|
2020-11-24 20:42:11 +00:00
|
|
|
"""Return the current tilt position of the cover."""
|
|
|
|
if self._device.tilt is not None:
|
2022-04-23 05:27:47 +00:00
|
|
|
return 100 - self._device.tilt
|
2020-11-28 12:11:13 +00:00
|
|
|
return None
|
2020-11-24 20:42:11 +00:00
|
|
|
|
2021-11-19 20:16:21 +00:00
|
|
|
async def async_set_cover_tilt_position(self, **kwargs: Any) -> None:
|
2020-11-24 20:42:11 +00:00
|
|
|
"""Tilt the cover to a specific position."""
|
2021-11-19 20:16:21 +00:00
|
|
|
position = 100 - cast(int, kwargs[ATTR_TILT_POSITION])
|
2022-07-06 22:31:47 +00:00
|
|
|
await self.gateway.api.lights.covers.set_state(
|
|
|
|
id=self._device.resource_id,
|
|
|
|
tilt=position,
|
|
|
|
)
|
2020-11-24 20:42:11 +00:00
|
|
|
|
2021-11-17 14:08:37 +00:00
|
|
|
async def async_open_cover_tilt(self, **kwargs: Any) -> None:
|
2020-11-24 20:42:11 +00:00
|
|
|
"""Open cover tilt."""
|
2022-07-06 22:31:47 +00:00
|
|
|
await self.gateway.api.lights.covers.set_state(
|
|
|
|
id=self._device.resource_id,
|
|
|
|
tilt=0,
|
|
|
|
)
|
2020-11-24 20:42:11 +00:00
|
|
|
|
2021-11-17 14:08:37 +00:00
|
|
|
async def async_close_cover_tilt(self, **kwargs: Any) -> None:
|
2020-11-24 20:42:11 +00:00
|
|
|
"""Close cover tilt."""
|
2022-07-06 22:31:47 +00:00
|
|
|
await self.gateway.api.lights.covers.set_state(
|
|
|
|
id=self._device.resource_id,
|
|
|
|
tilt=100,
|
|
|
|
)
|
2020-11-24 20:42:11 +00:00
|
|
|
|
2021-11-17 14:08:37 +00:00
|
|
|
async def async_stop_cover_tilt(self, **kwargs: Any) -> None:
|
2020-11-24 20:42:11 +00:00
|
|
|
"""Stop cover tilt."""
|
2022-07-06 22:31:47 +00:00
|
|
|
await self.gateway.api.lights.covers.set_state(
|
|
|
|
id=self._device.resource_id,
|
|
|
|
action=CoverAction.STOP,
|
|
|
|
)
|