Improve error handling, availability logic and state restore
- Add TransferResult enum (SUCCESS, SENT, SERVER_ERROR, NETWORK_ERROR) to distinguish network errors from server errors - Only mark entities as unavailable on NETWORK_ERROR (addon unreachable) - HTTP 401, 405, 500 no longer trigger unavailable state, logged instead - Add RestoreEntity to cover, switch and light to restore last known state after HA restart
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
"""AirSend covers."""
|
||||
from typing import Any
|
||||
|
||||
from .device import Device
|
||||
from .device import Device, TransferResult
|
||||
|
||||
from homeassistant.components.cover import CoverEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
@@ -78,14 +78,32 @@ class AirSendCover(RestoreEntity, CoverEntity):
|
||||
self._closed = component.state not in ('open', 'on', 'up')
|
||||
return self._closed
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Restore last known state when added to hass."""
|
||||
await super().async_added_to_hass()
|
||||
last_state = await self.async_get_last_state()
|
||||
if last_state and last_state.state not in ('unavailable', 'unknown'):
|
||||
if self._device.is_cover_with_position:
|
||||
if last_state.attributes.get('current_position') is not None:
|
||||
self._attr_current_cover_position = last_state.attributes['current_position']
|
||||
if last_state.state == 'closed':
|
||||
self._attr_current_cover_position = 0
|
||||
elif last_state.state == 'open':
|
||||
self._attr_current_cover_position = 100
|
||||
if last_state.state == 'closed':
|
||||
self._closed = True
|
||||
elif last_state.state == 'open':
|
||||
self._closed = False
|
||||
self.async_write_ha_state()
|
||||
|
||||
async def _send(self, note: dict) -> bool:
|
||||
"""Send a command and update availability accordingly."""
|
||||
result = await self._device.async_transfer(note, self.entity_id)
|
||||
available = result is not False
|
||||
available = result != TransferResult.NETWORK_ERROR
|
||||
if self._available != available:
|
||||
self._available = available
|
||||
self.async_write_ha_state()
|
||||
return result is not False
|
||||
return result in (TransferResult.SUCCESS, TransferResult.SENT)
|
||||
|
||||
async def async_open_cover(self, **kwargs: Any) -> None:
|
||||
note = {"method": 1, "type": 0, "value": "UP"}
|
||||
@@ -118,38 +136,3 @@ class AirSendCover(RestoreEntity, CoverEntity):
|
||||
self._attr_current_cover_position = position
|
||||
self._closed = position == 0
|
||||
self.async_write_ha_state()
|
||||
|
||||
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()
|
||||
|
||||
# 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
|
||||
# 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()
|
||||
|
||||
Reference in New Issue
Block a user