feat: Add state persistence for cover entities

Implements RestoreEntity to maintain cover position and state after Home Assistant restarts.

Changes:
- Add RestoreEntity mixin to AirSendCover class
- Implement async_added_to_hass() to restore previous state
- Position restoration for type 4099 covers (with position support)
- Open/closed state restoration for all cover types
- Maintains backward compatibility with 50% default for new covers

Fixes issue where covers would reset to 50% position after every restart.
This commit is contained in:
spartDev
2025-08-31 20:00:08 +02:00
parent 0c10a19810
commit 8512073e92
+29 -1
View File
@@ -6,6 +6,7 @@ from .device import Device
from homeassistant.components.cover import CoverEntity from homeassistant.components.cover import CoverEntity
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.const import CONF_DEVICES, CONF_INTERNAL_URL from homeassistant.const import CONF_DEVICES, CONF_INTERNAL_URL
from . import DOMAIN from . import DOMAIN
@@ -26,7 +27,7 @@ async def async_setup_platform(
async_add_entities([entity]) async_add_entities([entity])
class AirSendCover(CoverEntity): class AirSendCover(CoverEntity, RestoreEntity):
"""Representation of an AirSend Cover.""" """Representation of an AirSend Cover."""
def __init__( def __init__(
@@ -43,6 +44,33 @@ class AirSendCover(CoverEntity):
if device.is_cover_with_position: if device.is_cover_with_position:
self._attr_current_cover_position = 50 self._attr_current_cover_position = 50
async def async_added_to_hass(self):
"""Restore last known state when added to hass."""
await super().async_added_to_hass()
# Get the last known state
last_state = await self.async_get_last_state()
if last_state:
# Restore position for covers with position support (type 4099)
if self._device.is_cover_with_position:
# Try to restore position from attributes
if last_state.attributes.get('current_position') is not None:
self._attr_current_cover_position = last_state.attributes['current_position']
# Override position based on state if fully open/closed
if last_state.state == 'closed':
self._attr_current_cover_position = 0
elif last_state.state == 'open':
self._attr_current_cover_position = 100
# Restore closed/open state for all covers
if last_state.state == 'closed':
self._closed = True
elif last_state.state == 'open':
self._closed = False
# If no last_state, keep the defaults (50% for position covers)
@property @property
def unique_id(self): def unique_id(self):
"""Return unique identifier of remote device.""" """Return unique identifier of remote device."""