diff --git a/custom_components/airsend/cover.py b/custom_components/airsend/cover.py index d3dfd9f..d7384a8 100644 --- a/custom_components/airsend/cover.py +++ b/custom_components/airsend/cover.py @@ -122,27 +122,34 @@ class AirSendCover(RestoreEntity, CoverEntity): 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: + + # 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) 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 - self.async_write_ha_state() - # If no last_state, keep the defaults (50% for position covers) + # If no usable 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()