diff --git a/.coveragerc b/.coveragerc index e24a974ef85..ed621cbff10 100644 --- a/.coveragerc +++ b/.coveragerc @@ -175,6 +175,7 @@ omit = homeassistant/components/comed_hourly_pricing/sensor.py homeassistant/components/comelit/__init__.py homeassistant/components/comelit/const.py + homeassistant/components/comelit/cover.py homeassistant/components/comelit/coordinator.py homeassistant/components/comelit/light.py homeassistant/components/comfoconnect/fan.py diff --git a/homeassistant/components/comelit/__init__.py b/homeassistant/components/comelit/__init__.py index 2c73922582c..1a0d49f0666 100644 --- a/homeassistant/components/comelit/__init__.py +++ b/homeassistant/components/comelit/__init__.py @@ -7,7 +7,7 @@ from homeassistant.core import HomeAssistant from .const import DOMAIN from .coordinator import ComelitSerialBridge -PLATFORMS = [Platform.LIGHT] +PLATFORMS = [Platform.LIGHT, Platform.COVER] async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: diff --git a/homeassistant/components/comelit/cover.py b/homeassistant/components/comelit/cover.py new file mode 100644 index 00000000000..022ce05ff6d --- /dev/null +++ b/homeassistant/components/comelit/cover.py @@ -0,0 +1,101 @@ +"""Support for covers.""" +from __future__ import annotations + +from typing import Any + +from aiocomelit import ComelitSerialBridgeObject +from aiocomelit.const import COVER, COVER_CLOSE, COVER_OPEN, COVER_STATUS + +from homeassistant.components.cover import CoverDeviceClass, CoverEntity +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.update_coordinator import CoordinatorEntity + +from .const import DOMAIN +from .coordinator import ComelitSerialBridge + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up Comelit covers.""" + + coordinator: ComelitSerialBridge = hass.data[DOMAIN][config_entry.entry_id] + + # Use config_entry.entry_id as base for unique_id because no serial number or mac is available + async_add_entities( + ComelitCoverEntity(coordinator, device, config_entry.entry_id) + for device in coordinator.data[COVER].values() + ) + + +class ComelitCoverEntity(CoordinatorEntity[ComelitSerialBridge], CoverEntity): + """Cover device.""" + + _attr_device_class = CoverDeviceClass.SHUTTER + _attr_has_entity_name = True + _attr_name = None + + def __init__( + self, + coordinator: ComelitSerialBridge, + device: ComelitSerialBridgeObject, + config_entry_unique_id: str, + ) -> None: + """Init cover entity.""" + self._api = coordinator.api + self._device = device + super().__init__(coordinator) + self._attr_unique_id = f"{config_entry_unique_id}-{device.index}" + self._attr_device_info = coordinator.platform_device_info(device, COVER) + # Device doesn't provide a status so we assume CLOSE at startup + self._last_action = COVER_STATUS.index("closing") + + def _current_action(self, action: str) -> bool: + """Return the current cover action.""" + is_moving = self.device_status == COVER_STATUS.index(action) + if is_moving: + self._last_action = COVER_STATUS.index(action) + return is_moving + + @property + def device_status(self) -> int: + """Return current device status.""" + return self.coordinator.data[COVER][self._device.index].status + + @property + def is_closed(self) -> bool: + """Return True if cover is closed.""" + if self.device_status != COVER_STATUS.index("stopped"): + return False + + return bool(self._last_action == COVER_STATUS.index("closing")) + + @property + def is_closing(self) -> bool: + """Return if the cover is closing.""" + return self._current_action("closing") + + @property + def is_opening(self) -> bool: + """Return if the cover is opening.""" + return self._current_action("opening") + + async def async_close_cover(self, **kwargs: Any) -> None: + """Close cover.""" + await self._api.cover_move(self._device.index, COVER_CLOSE) + + async def async_open_cover(self, **kwargs: Any) -> None: + """Open cover.""" + await self._api.cover_move(self._device.index, COVER_OPEN) + + async def async_stop_cover(self, **_kwargs: Any) -> None: + """Stop the cover.""" + if not self.is_closing and not self.is_opening: + return + + action = COVER_OPEN if self.is_closing else COVER_CLOSE + await self._api.cover_move(self._device.index, action)