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:
Daniel Moindrot
2026-06-12 08:09:24 +02:00
committed by GitHub
parent 6949219c2b
commit 9047b75ce6
4 changed files with 83 additions and 73 deletions
+11 -12
View File
@@ -1,7 +1,7 @@
"""AirSend switches."""
from typing import Any
from .device import Device
from .device import Device, TransferResult
from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry
@@ -75,13 +75,21 @@ class AirSendSwitch(RestoreEntity, SwitchEntity):
self._state = component.state == 'on'
return self._state
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'):
self._state = last_state.state == 'on'
self.async_write_ha_state()
async def _send(self, note: dict) -> bool:
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_turn_on(self, **kwargs: Any) -> None:
if await self._send({"method": 1, "type": 0, "value": "ON"}):
@@ -92,12 +100,3 @@ class AirSendSwitch(RestoreEntity, SwitchEntity):
if await self._send({"method": 1, "type": 0, "value": "OFF"}):
self._state = False
self.async_write_ha_state()
async def async_added_to_hass(self) -> None:
"""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:
self._state = last_state.state == 'on'
self.async_write_ha_state()