2019-04-03 15:40:03 +00:00
|
|
|
"""Platform for the Aladdin Connect cover component."""
|
2021-05-15 20:53:42 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-06-28 21:22:18 +00:00
|
|
|
from datetime import timedelta
|
2023-02-27 15:20:01 +00:00
|
|
|
from typing import Any
|
2018-07-30 05:19:34 +00:00
|
|
|
|
2022-06-28 21:22:18 +00:00
|
|
|
from AIOAladdinConnect import AladdinConnectClient
|
2018-07-30 05:19:34 +00:00
|
|
|
|
2023-02-27 15:20:01 +00:00
|
|
|
from homeassistant.components.cover import CoverDeviceClass, CoverEntity
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import STATE_CLOSED, STATE_CLOSING, STATE_OPENING
|
2021-05-15 20:53:42 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-05-23 12:13:21 +00:00
|
|
|
from homeassistant.exceptions import PlatformNotReady
|
2022-07-14 09:09:27 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
2021-05-15 20:53:42 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2018-07-30 05:19:34 +00:00
|
|
|
|
2022-05-13 22:41:01 +00:00
|
|
|
from .const import DOMAIN, STATES_MAP, SUPPORTED_FEATURES
|
2021-05-15 20:53:42 +00:00
|
|
|
from .model import DoorDevice
|
2018-07-30 05:19:34 +00:00
|
|
|
|
2022-06-28 21:22:18 +00:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=300)
|
2018-07-30 05:19:34 +00:00
|
|
|
|
|
|
|
|
2022-05-13 22:41:01 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up the Aladdin Connect platform."""
|
2022-06-28 21:22:18 +00:00
|
|
|
acc: AladdinConnectClient = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
doors = await acc.get_doors()
|
2022-05-23 12:13:21 +00:00
|
|
|
if doors is None:
|
|
|
|
raise PlatformNotReady("Error from Aladdin Connect getting doors")
|
2022-05-13 22:41:01 +00:00
|
|
|
async_add_entities(
|
2022-06-28 21:22:18 +00:00
|
|
|
(AladdinDevice(acc, door, config_entry) for door in doors),
|
2022-05-13 22:41:01 +00:00
|
|
|
)
|
2018-07-30 05:19:34 +00:00
|
|
|
|
|
|
|
|
2020-04-25 16:07:15 +00:00
|
|
|
class AladdinDevice(CoverEntity):
|
2018-07-30 05:19:34 +00:00
|
|
|
"""Representation of Aladdin Connect cover."""
|
|
|
|
|
2021-12-08 20:16:43 +00:00
|
|
|
_attr_device_class = CoverDeviceClass.GARAGE
|
2021-07-06 06:47:49 +00:00
|
|
|
_attr_supported_features = SUPPORTED_FEATURES
|
|
|
|
|
2022-06-28 21:22:18 +00:00
|
|
|
def __init__(
|
|
|
|
self, acc: AladdinConnectClient, device: DoorDevice, entry: ConfigEntry
|
|
|
|
) -> None:
|
2022-05-13 22:41:01 +00:00
|
|
|
"""Initialize the Aladdin Connect cover."""
|
2018-07-30 05:19:34 +00:00
|
|
|
self._acc = acc
|
2022-06-28 21:22:18 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self._device_id = device["device_id"]
|
|
|
|
self._number = device["door_number"]
|
2022-07-14 09:09:27 +00:00
|
|
|
self._name = device["name"]
|
2022-07-18 21:41:06 +00:00
|
|
|
self._serial = device["serial"]
|
2022-08-24 07:53:17 +00:00
|
|
|
self._model = device["model"]
|
2022-07-14 09:09:27 +00:00
|
|
|
|
2023-01-02 20:47:36 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
2022-08-24 07:53:17 +00:00
|
|
|
identifiers={(DOMAIN, f"{self._device_id}-{self._number}")},
|
2022-07-14 09:09:27 +00:00
|
|
|
name=self._name,
|
|
|
|
manufacturer="Overhead Door",
|
2022-08-24 07:53:17 +00:00
|
|
|
model=self._model,
|
2022-07-14 09:09:27 +00:00
|
|
|
)
|
2023-01-02 20:47:36 +00:00
|
|
|
self._attr_has_entity_name = True
|
|
|
|
self._attr_unique_id = f"{self._device_id}-{self._number}"
|
2018-07-30 05:19:34 +00:00
|
|
|
|
2022-06-28 21:22:18 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""Connect Aladdin Connect to the cloud."""
|
|
|
|
|
2023-01-02 20:47:36 +00:00
|
|
|
self._acc.register_callback(
|
|
|
|
self.async_write_ha_state, self._serial, self._number
|
|
|
|
)
|
2022-07-18 21:41:06 +00:00
|
|
|
await self._acc.get_doors(self._serial)
|
2022-06-28 21:22:18 +00:00
|
|
|
|
|
|
|
async def async_will_remove_from_hass(self) -> None:
|
|
|
|
"""Close Aladdin Connect before removing."""
|
2023-01-02 20:47:36 +00:00
|
|
|
self._acc.unregister_callback(self._serial, self._number)
|
2022-06-28 21:22:18 +00:00
|
|
|
await self._acc.close()
|
|
|
|
|
|
|
|
async def async_close_cover(self, **kwargs: Any) -> None:
|
2018-07-30 05:19:34 +00:00
|
|
|
"""Issue close command to cover."""
|
2022-06-28 21:22:18 +00:00
|
|
|
await self._acc.close_door(self._device_id, self._number)
|
2018-07-30 05:19:34 +00:00
|
|
|
|
2022-06-28 21:22:18 +00:00
|
|
|
async def async_open_cover(self, **kwargs: Any) -> None:
|
2018-07-30 05:19:34 +00:00
|
|
|
"""Issue open command to cover."""
|
2022-06-28 21:22:18 +00:00
|
|
|
await self._acc.open_door(self._device_id, self._number)
|
2018-07-30 05:19:34 +00:00
|
|
|
|
2022-06-28 21:22:18 +00:00
|
|
|
async def async_update(self) -> None:
|
2018-07-30 05:19:34 +00:00
|
|
|
"""Update status of cover."""
|
2022-07-18 21:41:06 +00:00
|
|
|
await self._acc.get_doors(self._serial)
|
2022-06-28 21:22:18 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_closed(self) -> bool | None:
|
|
|
|
"""Update is closed attribute."""
|
|
|
|
value = STATES_MAP.get(self._acc.get_door_status(self._device_id, self._number))
|
|
|
|
if value is None:
|
|
|
|
return None
|
|
|
|
return value == STATE_CLOSED
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_closing(self) -> bool:
|
|
|
|
"""Update is closing attribute."""
|
|
|
|
return (
|
|
|
|
STATES_MAP.get(self._acc.get_door_status(self._device_id, self._number))
|
|
|
|
== STATE_CLOSING
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_opening(self) -> bool:
|
|
|
|
"""Update is opening attribute."""
|
|
|
|
return (
|
|
|
|
STATES_MAP.get(self._acc.get_door_status(self._device_id, self._number))
|
|
|
|
== STATE_OPENING
|
2021-07-06 06:47:49 +00:00
|
|
|
)
|