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
|
|
|
|
2021-02-09 08:43:38 +00:00
|
|
|
from bond_api import Action, BPUPSubscriptions, DeviceType
|
2020-07-06 01:17:53 +00:00
|
|
|
|
|
|
|
from homeassistant.components.cover import DEVICE_CLASS_SHADE, CoverEntity
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity import Entity
|
2021-04-30 18:38:59 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-07-06 01:17:53 +00:00
|
|
|
|
2021-02-09 08:43:38 +00:00
|
|
|
from .const import BPUP_SUBS, DOMAIN, HUB
|
2020-07-11 01:20:50 +00:00
|
|
|
from .entity import BondEntity
|
2020-07-12 16:31:53 +00:00
|
|
|
from .utils import BondDevice, BondHub
|
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."""
|
2021-02-09 08:43:38 +00:00
|
|
|
data = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
hub: BondHub = data[HUB]
|
|
|
|
bpup_subs: BPUPSubscriptions = data[BPUP_SUBS]
|
2020-07-06 01:17:53 +00:00
|
|
|
|
2021-03-17 22:34:25 +00:00
|
|
|
covers: list[Entity] = [
|
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
|
2020-07-08 21:28:53 +00:00
|
|
|
]
|
2020-07-06 01:17:53 +00:00
|
|
|
|
2020-07-09 23:25:18 +00:00
|
|
|
async_add_entities(covers, True)
|
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-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)
|
2020-07-11 01:20:50 +00:00
|
|
|
|
2021-03-17 22:34:25 +00:00
|
|
|
self._closed: bool | None = None
|
2020-07-06 01:17:53 +00:00
|
|
|
|
2021-03-01 02:16:30 +00:00
|
|
|
def _apply_state(self, state: dict) -> None:
|
2020-07-23 02:15:27 +00:00
|
|
|
cover_open = state.get("open")
|
|
|
|
self._closed = True if cover_open == 0 else False if cover_open == 1 else None
|
|
|
|
|
2020-07-06 01:17:53 +00:00
|
|
|
@property
|
2021-03-17 22:34:25 +00:00
|
|
|
def device_class(self) -> str | None:
|
2020-07-06 01:17:53 +00:00
|
|
|
"""Get device class."""
|
|
|
|
return DEVICE_CLASS_SHADE
|
|
|
|
|
|
|
|
@property
|
2021-03-17 22:34:25 +00:00
|
|
|
def is_closed(self) -> bool | None:
|
2020-07-06 01:17:53 +00:00
|
|
|
"""Return if the cover is closed or not."""
|
2020-07-09 23:25:18 +00:00
|
|
|
return self._closed
|
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())
|