2019-04-03 15:40:03 +00:00
|
|
|
"""Support for MyQ-Enabled Garage Doors."""
|
2022-06-24 04:40:26 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2021-08-19 01:50:46 +00:00
|
|
|
from pymyq.const import DEVICE_TYPE_GATE as MYQ_DEVICE_TYPE_GATE
|
2021-02-10 20:30:52 +00:00
|
|
|
from pymyq.errors import MyQError
|
2017-02-13 10:20:07 +00:00
|
|
|
|
2018-08-29 12:33:09 +00:00
|
|
|
from homeassistant.components.cover import (
|
2021-12-15 19:44:27 +00:00
|
|
|
CoverDeviceClass,
|
2020-04-25 16:07:15 +00:00
|
|
|
CoverEntity,
|
2022-04-06 15:33:41 +00:00
|
|
|
CoverEntityFeature,
|
2019-04-10 21:24:12 +00:00
|
|
|
)
|
2022-01-03 14:13:18 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-02-10 20:30:52 +00:00
|
|
|
from homeassistant.const import STATE_CLOSED, STATE_CLOSING, STATE_OPEN, STATE_OPENING
|
2022-01-03 14:13:18 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-08-17 22:56:33 +00:00
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2022-01-03 14:13:18 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-03-22 23:28:55 +00:00
|
|
|
|
2021-08-19 01:50:46 +00:00
|
|
|
from . import MyQEntity
|
2021-02-10 20:30:52 +00:00
|
|
|
from .const import DOMAIN, MYQ_COORDINATOR, MYQ_GATEWAY, MYQ_TO_HASS
|
|
|
|
|
2020-03-20 20:28:14 +00:00
|
|
|
|
2022-01-03 14:13:18 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-03-20 20:28:14 +00:00
|
|
|
"""Set up mysq covers."""
|
2020-03-22 23:28:55 +00:00
|
|
|
data = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
myq = data[MYQ_GATEWAY]
|
|
|
|
coordinator = data[MYQ_COORDINATOR]
|
|
|
|
|
|
|
|
async_add_entities(
|
2021-08-19 01:50:46 +00:00
|
|
|
[MyQCover(coordinator, device) for device in myq.covers.values()]
|
2020-03-22 23:28:55 +00:00
|
|
|
)
|
2017-02-13 10:20:07 +00:00
|
|
|
|
|
|
|
|
2021-08-19 01:50:46 +00:00
|
|
|
class MyQCover(MyQEntity, CoverEntity):
|
2017-02-13 10:20:07 +00:00
|
|
|
"""Representation of a MyQ cover."""
|
|
|
|
|
2022-04-06 15:33:41 +00:00
|
|
|
_attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
|
2021-08-19 01:50:46 +00:00
|
|
|
|
2020-03-22 23:28:55 +00:00
|
|
|
def __init__(self, coordinator, device):
|
2017-02-13 10:20:07 +00:00
|
|
|
"""Initialize with API object, device id."""
|
2021-08-19 01:50:46 +00:00
|
|
|
super().__init__(coordinator, device)
|
2018-11-18 17:37:03 +00:00
|
|
|
self._device = device
|
2021-08-17 22:56:33 +00:00
|
|
|
if device.device_type == MYQ_DEVICE_TYPE_GATE:
|
2021-12-15 19:44:27 +00:00
|
|
|
self._attr_device_class = CoverDeviceClass.GATE
|
2021-08-17 22:56:33 +00:00
|
|
|
else:
|
2021-12-15 19:44:27 +00:00
|
|
|
self._attr_device_class = CoverDeviceClass.GARAGE
|
2021-08-17 22:56:33 +00:00
|
|
|
self._attr_unique_id = device.device_id
|
2018-05-24 05:58:35 +00:00
|
|
|
|
2017-02-13 10:20:07 +00:00
|
|
|
@property
|
2022-06-25 09:59:56 +00:00
|
|
|
def is_closed(self) -> bool:
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Return true if cover is closed, else False."""
|
2018-11-18 17:37:03 +00:00
|
|
|
return MYQ_TO_HASS.get(self._device.state) == STATE_CLOSED
|
2018-08-29 12:33:09 +00:00
|
|
|
|
|
|
|
@property
|
2022-06-25 09:59:56 +00:00
|
|
|
def is_closing(self) -> bool:
|
2018-08-29 12:33:09 +00:00
|
|
|
"""Return if the cover is closing or not."""
|
2018-11-18 17:37:03 +00:00
|
|
|
return MYQ_TO_HASS.get(self._device.state) == STATE_CLOSING
|
2018-08-29 12:33:09 +00:00
|
|
|
|
2021-02-10 20:30:52 +00:00
|
|
|
@property
|
2022-06-25 09:59:56 +00:00
|
|
|
def is_open(self) -> bool:
|
2021-02-10 20:30:52 +00:00
|
|
|
"""Return if the cover is opening or not."""
|
|
|
|
return MYQ_TO_HASS.get(self._device.state) == STATE_OPEN
|
|
|
|
|
2018-08-29 12:33:09 +00:00
|
|
|
@property
|
2022-06-25 09:59:56 +00:00
|
|
|
def is_opening(self) -> bool:
|
2018-08-29 12:33:09 +00:00
|
|
|
"""Return if the cover is opening or not."""
|
2018-11-18 17:37:03 +00:00
|
|
|
return MYQ_TO_HASS.get(self._device.state) == STATE_OPENING
|
2017-02-13 10:20:07 +00:00
|
|
|
|
2022-06-24 04:40:26 +00:00
|
|
|
async def async_close_cover(self, **kwargs: Any) -> None:
|
2018-11-18 17:37:03 +00:00
|
|
|
"""Issue close command to cover."""
|
2021-02-10 20:30:52 +00:00
|
|
|
if self.is_closing or self.is_closed:
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
wait_task = await self._device.close(wait_for_state=False)
|
|
|
|
except MyQError as err:
|
2021-08-17 22:56:33 +00:00
|
|
|
raise HomeAssistantError(
|
|
|
|
f"Closing of cover {self._device.name} failed with error: {err}"
|
|
|
|
) from err
|
2021-02-10 20:30:52 +00:00
|
|
|
|
|
|
|
# Write closing state to HASS
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
2021-08-12 03:17:25 +00:00
|
|
|
result = wait_task if isinstance(wait_task, bool) else await wait_task
|
|
|
|
|
2021-02-10 20:30:52 +00:00
|
|
|
# Write final state to HASS
|
|
|
|
self.async_write_ha_state()
|
2018-11-18 17:37:03 +00:00
|
|
|
|
2021-08-17 22:56:33 +00:00
|
|
|
if not result:
|
|
|
|
raise HomeAssistantError(f"Closing of cover {self._device.name} failed")
|
|
|
|
|
2022-06-24 04:40:26 +00:00
|
|
|
async def async_open_cover(self, **kwargs: Any) -> None:
|
2018-11-18 17:37:03 +00:00
|
|
|
"""Issue open command to cover."""
|
2021-02-10 20:30:52 +00:00
|
|
|
if self.is_opening or self.is_open:
|
|
|
|
return
|
2020-03-22 23:28:55 +00:00
|
|
|
|
2021-02-10 20:30:52 +00:00
|
|
|
try:
|
|
|
|
wait_task = await self._device.open(wait_for_state=False)
|
|
|
|
except MyQError as err:
|
2021-08-17 22:56:33 +00:00
|
|
|
raise HomeAssistantError(
|
|
|
|
f"Opening of cover {self._device.name} failed with error: {err}"
|
|
|
|
) from err
|
2021-02-10 20:30:52 +00:00
|
|
|
|
|
|
|
# Write opening state to HASS
|
2020-03-20 20:28:14 +00:00
|
|
|
self.async_write_ha_state()
|
2018-08-31 14:47:37 +00:00
|
|
|
|
2021-08-12 03:17:25 +00:00
|
|
|
result = wait_task if isinstance(wait_task, bool) else await wait_task
|
|
|
|
|
2021-02-10 20:30:52 +00:00
|
|
|
# Write final state to HASS
|
|
|
|
self.async_write_ha_state()
|
2020-03-20 20:28:14 +00:00
|
|
|
|
2021-08-17 22:56:33 +00:00
|
|
|
if not result:
|
|
|
|
raise HomeAssistantError(f"Opening of cover {self._device.name} failed")
|