Files
devmel_hass_airsend/custom_components/airsend/cover.py
T
spartDev 8512073e92 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.
2025-08-31 20:00:08 +02:00

151 lines
5.2 KiB
Python

"""AirSend switches."""
from typing import Any
from .device import Device
from homeassistant.components.cover import CoverEntity
from homeassistant.core import HomeAssistant
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.const import CONF_DEVICES, CONF_INTERNAL_URL
from . import DOMAIN
async def async_setup_platform(
hass: HomeAssistant, config: ConfigType, async_add_entities, discovery_info=None
) -> None:
if discovery_info is None:
return
for name, options in discovery_info[CONF_DEVICES].items():
device = Device(name, options, discovery_info[CONF_INTERNAL_URL])
if device.is_cover:
entity = AirSendCover(
hass,
device,
)
async_add_entities([entity])
class AirSendCover(CoverEntity, RestoreEntity):
"""Representation of an AirSend Cover."""
def __init__(
self,
hass: HomeAssistant,
device: Device,
) -> None:
"""Initialize a cover device."""
self._hass = hass
self._device = device
uname = DOMAIN + device.name
self._unique_id = "_".join(x for x in uname)
self._closed = None
if device.is_cover_with_position:
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
def unique_id(self):
"""Return unique identifier of remote device."""
return self._unique_id
@property
def available(self):
return True
@property
def should_poll(self):
"""No polling needed."""
return False
@property
def name(self):
"""Return the name of the device if any."""
return self._device.name
@property
def extra_state_attributes(self):
return self._device.extra_state_attributes
@property
def assumed_state(self):
"""Return true if unable to access real state of entity."""
return True
@property
def is_closed(self):
"""Return if the cover is closed."""
if self._device.is_async and self._hass:
component = self._hass.states.get(self.entity_id)
if component is not None:
if component.state == 'open' or component.state == 'on' or component.state == 'up':
self._closed = False
else:
self._closed = True
return self._closed
def open_cover(self, **kwargs: Any) -> None:
"""Open the cover."""
note = {"method": 1, "type": 0, "value": "UP"}
if self._device.transfer(note, self.entity_id) == True:
self._closed = False
if self._device.is_cover_with_position:
self._attr_current_cover_position = 100
self.schedule_update_ha_state()
def close_cover(self, **kwargs: Any) -> None:
"""Close cover."""
note = {"method": 1, "type": 0, "value": "DOWN"}
if self._device.transfer(note, self.entity_id) == True:
self._closed = True
if self._device.is_cover_with_position:
self._attr_current_cover_position = 0
self.schedule_update_ha_state()
def stop_cover(self, **kwargs):
"""Stop the cover."""
note = {"method": 1, "type": 0, "value": "STOP"}
if self._device.transfer(note, self.entity_id) == True:
self._closed = False
if self._device.is_cover_with_position:
self._attr_current_cover_position = 50
self.schedule_update_ha_state()
def set_cover_position(self, **kwargs):
"""Move the cover to a specific position."""
position = int(kwargs["position"])
note = {"method": 1, "type": 9, "value": position}
if self._device.transfer(note, self.entity_id) == True:
self._attr_current_cover_position = position
self._closed = False
if self._attr_current_cover_position == 0:
self._closed = True
self.schedule_update_ha_state()