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