state persistence & device type fix

This commit is contained in:
Devmel Apps
2026-04-01 10:35:15 +02:00
parent 7009d6ee89
commit d31e77d1cf
11 changed files with 69 additions and 23 deletions
Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

+30 -1
View File
@@ -8,6 +8,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.const import CONF_INTERNAL_URL
from . import DOMAIN
@@ -29,7 +30,7 @@ async def async_setup_entry(
async_add_entities(entities)
class AirSendCover(CoverEntity):
class AirSendCover(RestoreEntity, CoverEntity):
"""Representation of an AirSend Cover."""
def __init__(self, hass: HomeAssistant, device: Device) -> None:
@@ -117,3 +118,31 @@ class AirSendCover(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()
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
self.async_write_ha_state()
# If no last_state, keep the defaults (50% for position covers)
+5 -4
View File
@@ -13,8 +13,9 @@ RTYPE_LABELS = {
4096: "AirSend Button",
4097: "AirSend Switch",
4098: "AirSend Cover",
4099: "AirSend Cover (position)",
4100: "AirSend Light",
4099: "AirSend Slider",
4100: "AirSend Tilt",
4101: "AirSend Light",
}
@@ -90,7 +91,7 @@ class Device:
if self._channel:
result = str(self._channel['id'])
if result:
uniquefield = ['source', 'mac', 'seed']
uniquefield = ['source', 'mac', 'seed', 'token']
for field in uniquefield:
if field in self._channel:
result += "_"
@@ -145,7 +146,7 @@ class Device:
@property
def is_light(self) -> bool:
return self._rtype == 4100
return self._rtype == 4101
@property
def refresh_value(self) -> int:
+2 -1
View File
@@ -2,10 +2,11 @@
"domain": "airsend",
"name": "AirSend",
"documentation": "https://github.com/devmel/hass_airsend",
"issue_tracker": "https://github.com/devmel/hass_airsend/issues",
"dependencies": ["http"],
"config_flow": true,
"codeowners": ["@devmel"],
"requirements": [],
"version": "4.0",
"version": "4.0.0",
"iot_class": "local_push"
}
+14 -1
View File
@@ -6,6 +6,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo, generate_entity_id
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.const import CONF_INTERNAL_URL, UnitOfTemperature, LIGHT_LUX
@@ -51,7 +52,7 @@ async def async_setup_entry(
async_add_entities(entities)
class AirSendAnySensor(SensorEntity):
class AirSendAnySensor(RestoreEntity, SensorEntity):
"""Generic AirSend sensor (type 1) — no coordinator, push only."""
def __init__(self, hass: HomeAssistant, device: Device, internal_url: str) -> None:
@@ -83,6 +84,18 @@ class AirSendAnySensor(SensorEntity):
def device_info(self) -> DeviceInfo:
return self._device.device_info
@property
def native_value(self):
return self._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 is not None:
self._state = last_state.state
self.async_write_ha_state()
class AirSendTempSensor(CoordinatorEntity, SensorEntity):
"""AirSend device temperature sensor — uses shared coordinator."""
+11 -1
View File
@@ -8,6 +8,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.const import CONF_INTERNAL_URL
from . import DOMAIN
@@ -28,7 +29,7 @@ async def async_setup_entry(
async_add_entities(entities)
class AirSendSwitch(SwitchEntity):
class AirSendSwitch(RestoreEntity, SwitchEntity):
"""Representation of an AirSend Switch."""
def __init__(self, hass: HomeAssistant, device: Device) -> None:
@@ -91,3 +92,12 @@ class AirSendSwitch(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()