Merge pull request #28 from ijardillier/fix/cover-restore-state

Fix cover restore to avoid staying stuck unavailable after restart
This commit is contained in:
Daniel Moindrot
2026-06-12 08:02:54 +02:00
committed by GitHub
+14 -7
View File
@@ -122,27 +122,34 @@ class AirSendCover(RestoreEntity, CoverEntity):
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Restore last known state when added to hass.""" """Restore last known state when added to hass."""
await super().async_added_to_hass() await super().async_added_to_hass()
# Get the last known state # Get the last known state
last_state = await self.async_get_last_state() last_state = await self.async_get_last_state()
if last_state: # Only restore from a meaningful previous state. If HASS was
# restarted while the entity was "unavailable" (e.g. AirSend
# connectivity loss), don't carry that over: keep the defaults
# so the entity comes back available instead of staying stuck.
if last_state and last_state.state not in ('unavailable', 'unknown'):
# Restore position for covers with position support (type 4099) # Restore position for covers with position support (type 4099)
if self._device.is_cover_with_position: if self._device.is_cover_with_position:
# Try to restore position from attributes # Try to restore position from attributes
if last_state.attributes.get('current_position') is not None: if last_state.attributes.get('current_position') is not None:
self._attr_current_cover_position = last_state.attributes['current_position'] self._attr_current_cover_position = last_state.attributes['current_position']
# Override position based on state if fully open/closed # Override position based on state if fully open/closed
if last_state.state == 'closed': if last_state.state == 'closed':
self._attr_current_cover_position = 0 self._attr_current_cover_position = 0
elif last_state.state == 'open': elif last_state.state == 'open':
self._attr_current_cover_position = 100 self._attr_current_cover_position = 100
# Restore closed/open state for all covers # Restore closed/open state for all covers
if last_state.state == 'closed': if last_state.state == 'closed':
self._closed = True self._closed = True
elif last_state.state == 'open': elif last_state.state == 'open':
self._closed = False self._closed = False
self.async_write_ha_state() # If no usable last_state, keep the defaults (50% for position covers)
# If no last_state, keep the defaults (50% for position covers)
# Always publish the current state so the entity doesn't stay
# stuck on a stale cached state across a restart.
self.async_write_ha_state()