2020-07-06 01:17:53 +00:00
|
|
|
"""Support for Bond covers."""
|
2021-03-17 22:34:25 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-04-30 18:38:59 +00:00
|
|
|
from typing import Any
|
2020-07-06 01:17:53 +00:00
|
|
|
|
2022-05-26 04:12:43 +00:00
|
|
|
from bond_async import Action, BPUPSubscriptions, DeviceType
|
2020-07-06 01:17:53 +00:00
|
|
|
|
2021-11-12 03:31:58 +00:00
|
|
|
from homeassistant.components.cover import (
|
2022-05-20 14:49:26 +00:00
|
|
|
ATTR_POSITION,
|
2021-12-09 07:26:37 +00:00
|
|
|
CoverDeviceClass,
|
2021-11-12 03:31:58 +00:00
|
|
|
CoverEntity,
|
2022-04-05 21:53:45 +00:00
|
|
|
CoverEntityFeature,
|
2021-11-12 03:31:58 +00:00
|
|
|
)
|
2020-07-06 01:17:53 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
2021-04-30 18:38:59 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-07-06 01:17:53 +00:00
|
|
|
|
2022-06-18 03:45:20 +00:00
|
|
|
from .const import DOMAIN
|
2020-07-11 01:20:50 +00:00
|
|
|
from .entity import BondEntity
|
2022-06-18 03:45:20 +00:00
|
|
|
from .models import BondData
|
2020-07-12 16:31:53 +00:00
|
|
|
from .utils import BondDevice, BondHub
|
2020-07-06 01:17:53 +00:00
|
|
|
|
|
|
|
|
2022-05-20 14:49:26 +00:00
|
|
|
def _bond_to_hass_position(bond_position: int) -> int:
|
|
|
|
"""Convert bond 0-open 100-closed to hass 0-closed 100-open."""
|
|
|
|
return abs(bond_position - 100)
|
|
|
|
|
|
|
|
|
|
|
|
def _hass_to_bond_position(hass_position: int) -> int:
|
|
|
|
"""Convert hass 0-closed 100-open to bond 0-open 100-closed."""
|
|
|
|
return 100 - hass_position
|
|
|
|
|
|
|
|
|
2020-07-06 01:17:53 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
2021-04-30 18:38:59 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2020-07-06 01:17:53 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set up Bond cover devices."""
|
2022-06-18 03:45:20 +00:00
|
|
|
data: BondData = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
hub = data.hub
|
|
|
|
bpup_subs = data.bpup_subs
|
|
|
|
|
2022-06-15 06:30:59 +00:00
|
|
|
async_add_entities(
|
2021-02-09 08:43:38 +00:00
|
|
|
BondCover(hub, device, bpup_subs)
|
2020-07-16 15:31:15 +00:00
|
|
|
for device in hub.devices
|
2020-07-23 01:22:25 +00:00
|
|
|
if device.type == DeviceType.MOTORIZED_SHADES
|
2022-06-15 06:30:59 +00:00
|
|
|
)
|
2020-07-06 01:17:53 +00:00
|
|
|
|
|
|
|
|
2020-07-11 01:20:50 +00:00
|
|
|
class BondCover(BondEntity, CoverEntity):
|
2020-07-06 01:17:53 +00:00
|
|
|
"""Representation of a Bond cover."""
|
|
|
|
|
2021-12-09 07:26:37 +00:00
|
|
|
_attr_device_class = CoverDeviceClass.SHADE
|
2021-07-16 21:06:18 +00:00
|
|
|
|
2021-03-01 02:16:30 +00:00
|
|
|
def __init__(
|
|
|
|
self, hub: BondHub, device: BondDevice, bpup_subs: BPUPSubscriptions
|
|
|
|
) -> None:
|
2020-07-06 01:17:53 +00:00
|
|
|
"""Create HA entity representing Bond cover."""
|
2021-02-09 08:43:38 +00:00
|
|
|
super().__init__(hub, device, bpup_subs)
|
2022-11-22 06:09:19 +00:00
|
|
|
supported_features = CoverEntityFeature(0)
|
2022-05-20 14:49:26 +00:00
|
|
|
if self._device.supports_set_position():
|
|
|
|
supported_features |= CoverEntityFeature.SET_POSITION
|
2021-11-12 03:31:58 +00:00
|
|
|
if self._device.supports_open():
|
2022-04-05 21:53:45 +00:00
|
|
|
supported_features |= CoverEntityFeature.OPEN
|
2021-11-12 03:31:58 +00:00
|
|
|
if self._device.supports_close():
|
2022-04-05 21:53:45 +00:00
|
|
|
supported_features |= CoverEntityFeature.CLOSE
|
2021-11-12 03:31:58 +00:00
|
|
|
if self._device.supports_tilt_open():
|
2022-04-05 21:53:45 +00:00
|
|
|
supported_features |= CoverEntityFeature.OPEN_TILT
|
2021-11-12 03:31:58 +00:00
|
|
|
if self._device.supports_tilt_close():
|
2022-04-05 21:53:45 +00:00
|
|
|
supported_features |= CoverEntityFeature.CLOSE_TILT
|
2021-11-12 03:31:58 +00:00
|
|
|
if self._device.supports_hold():
|
|
|
|
if self._device.supports_open() or self._device.supports_close():
|
2022-04-05 21:53:45 +00:00
|
|
|
supported_features |= CoverEntityFeature.STOP
|
2021-11-12 03:31:58 +00:00
|
|
|
if self._device.supports_tilt_open() or self._device.supports_tilt_close():
|
2022-04-05 21:53:45 +00:00
|
|
|
supported_features |= CoverEntityFeature.STOP_TILT
|
2021-11-12 03:31:58 +00:00
|
|
|
self._attr_supported_features = supported_features
|
2020-07-11 01:20:50 +00:00
|
|
|
|
2022-06-15 06:30:59 +00:00
|
|
|
def _apply_state(self) -> None:
|
|
|
|
state = self._device.state
|
2020-07-23 02:15:27 +00:00
|
|
|
cover_open = state.get("open")
|
2022-05-20 14:49:26 +00:00
|
|
|
self._attr_is_closed = None if cover_open is None else cover_open == 0
|
|
|
|
if (bond_position := state.get("position")) is not None:
|
|
|
|
self._attr_current_cover_position = _bond_to_hass_position(bond_position)
|
|
|
|
|
|
|
|
async def async_set_cover_position(self, **kwargs: Any) -> None:
|
|
|
|
"""Set the cover position."""
|
|
|
|
await self._hub.bond.action(
|
|
|
|
self._device.device_id,
|
|
|
|
Action.set_position(_hass_to_bond_position(kwargs[ATTR_POSITION])),
|
2021-07-16 21:06:18 +00:00
|
|
|
)
|
2020-07-06 01:17:53 +00:00
|
|
|
|
2020-07-23 01:22:25 +00:00
|
|
|
async def async_open_cover(self, **kwargs: Any) -> None:
|
2020-07-06 01:17:53 +00:00
|
|
|
"""Open the cover."""
|
2020-07-23 01:22:25 +00:00
|
|
|
await self._hub.bond.action(self._device.device_id, Action.open())
|
2020-07-06 01:17:53 +00:00
|
|
|
|
2020-07-23 01:22:25 +00:00
|
|
|
async def async_close_cover(self, **kwargs: Any) -> None:
|
2020-07-06 01:17:53 +00:00
|
|
|
"""Close cover."""
|
2020-07-23 01:22:25 +00:00
|
|
|
await self._hub.bond.action(self._device.device_id, Action.close())
|
2020-07-06 01:17:53 +00:00
|
|
|
|
2021-03-01 02:16:30 +00:00
|
|
|
async def async_stop_cover(self, **kwargs: Any) -> None:
|
2020-07-06 01:17:53 +00:00
|
|
|
"""Hold cover."""
|
2020-07-23 01:22:25 +00:00
|
|
|
await self._hub.bond.action(self._device.device_id, Action.hold())
|
2021-11-12 03:31:58 +00:00
|
|
|
|
|
|
|
async def async_open_cover_tilt(self, **kwargs: Any) -> None:
|
|
|
|
"""Open the cover tilt."""
|
|
|
|
await self._hub.bond.action(self._device.device_id, Action.tilt_open())
|
|
|
|
|
|
|
|
async def async_close_cover_tilt(self, **kwargs: Any) -> None:
|
|
|
|
"""Close the cover tilt."""
|
|
|
|
await self._hub.bond.action(self._device.device_id, Action.tilt_close())
|
|
|
|
|
|
|
|
async def async_stop_cover_tilt(self, **kwargs: Any) -> None:
|
|
|
|
"""Stop the cover."""
|
|
|
|
await self._hub.bond.action(self._device.device_id, Action.hold())
|