Add RestoreEntity to Comelit cover (#101192)

* Add RestoreEntoty to Comelit cover

* remove logging
This commit is contained in:
Simone Chemelli 2023-10-01 20:56:34 +02:00 committed by GitHub
parent 67f7c703f4
commit 9261ad14e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,10 +6,11 @@ from typing import Any
from aiocomelit import ComelitSerialBridgeObject from aiocomelit import ComelitSerialBridgeObject
from aiocomelit.const import COVER, COVER_CLOSE, COVER_OPEN, COVER_STATUS from aiocomelit.const import COVER, COVER_CLOSE, COVER_OPEN, COVER_STATUS
from homeassistant.components.cover import CoverDeviceClass, CoverEntity from homeassistant.components.cover import STATE_CLOSED, CoverDeviceClass, CoverEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN from .const import DOMAIN
@ -32,7 +33,9 @@ async def async_setup_entry(
) )
class ComelitCoverEntity(CoordinatorEntity[ComelitSerialBridge], CoverEntity): class ComelitCoverEntity(
CoordinatorEntity[ComelitSerialBridge], RestoreEntity, CoverEntity
):
"""Cover device.""" """Cover device."""
_attr_device_class = CoverDeviceClass.SHUTTER _attr_device_class = CoverDeviceClass.SHUTTER
@ -51,8 +54,9 @@ class ComelitCoverEntity(CoordinatorEntity[ComelitSerialBridge], CoverEntity):
super().__init__(coordinator) super().__init__(coordinator)
self._attr_unique_id = f"{config_entry_entry_id}-{device.index}" self._attr_unique_id = f"{config_entry_entry_id}-{device.index}"
self._attr_device_info = coordinator.platform_device_info(device, COVER) self._attr_device_info = coordinator.platform_device_info(device, COVER)
# Device doesn't provide a status so we assume CLOSE at startup # Device doesn't provide a status so we assume UNKNOWN at first startup
self._last_action = COVER_STATUS.index("closing") self._last_action: int | None = None
self._last_state: str | None = None
def _current_action(self, action: str) -> bool: def _current_action(self, action: str) -> bool:
"""Return the current cover action.""" """Return the current cover action."""
@ -67,12 +71,19 @@ class ComelitCoverEntity(CoordinatorEntity[ComelitSerialBridge], CoverEntity):
return self.coordinator.data[COVER][self._device.index].status return self.coordinator.data[COVER][self._device.index].status
@property @property
def is_closed(self) -> bool: def is_closed(self) -> bool | None:
"""Return True if cover is closed.""" """Return if the cover is closed."""
if self._last_state in [None, "unknown"]:
return None
if self.device_status != COVER_STATUS.index("stopped"): if self.device_status != COVER_STATUS.index("stopped"):
return False return False
return bool(self._last_action == COVER_STATUS.index("closing")) if self._last_action:
return self._last_action == COVER_STATUS.index("closing")
return self._last_state == STATE_CLOSED
@property @property
def is_closing(self) -> bool: def is_closing(self) -> bool:
@ -99,3 +110,17 @@ class ComelitCoverEntity(CoordinatorEntity[ComelitSerialBridge], CoverEntity):
action = COVER_OPEN if self.is_closing else COVER_CLOSE action = COVER_OPEN if self.is_closing else COVER_CLOSE
await self._api.cover_move(self._device.index, action) await self._api.cover_move(self._device.index, action)
@callback
def _handle_coordinator_update(self) -> None:
"""Handle device update."""
self._last_state = self.state
self.async_write_ha_state()
async def async_added_to_hass(self) -> None:
"""Handle entity which will be added."""
await super().async_added_to_hass()
if last_state := await self.async_get_last_state():
self._last_state = last_state.state