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
+5
View File
@@ -49,6 +49,11 @@ Component for sending radio commands through the AirSend (RF433) or AirSend duo
- Restart Home Assistant. - Restart Home Assistant.
- Restart the AirSend addon. - Restart the AirSend addon.
6. **Configure the Integration**:
- Navigate to Settings -> Devices & services -> + Add Integration.
- Search for and select AirSend.
- Select the rooms associated with your devices to complete the setup.
These steps will integrate AirSend with Home Assistant, allowing you to manage and automate AirSend-related tasks through the Home Assistant interface. These steps will integrate AirSend with Home Assistant, allowing you to manage and automate AirSend-related tasks through the Home Assistant interface.
+1 -1
View File
@@ -1,4 +1,4 @@
#internal_url: http://192.168.0.21:33863/ #internal_url: http://172.30.33.0:33863/
devices: devices:
AirSend: AirSend:
type: 0 type: 0
-11
View File
@@ -1,11 +0,0 @@
# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:
group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
airsend: !include airsend.yaml
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.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.const import CONF_INTERNAL_URL from homeassistant.const import CONF_INTERNAL_URL
from . import DOMAIN from . import DOMAIN
@@ -29,7 +30,7 @@ async def async_setup_entry(
async_add_entities(entities) async_add_entities(entities)
class AirSendCover(CoverEntity): class AirSendCover(RestoreEntity, CoverEntity):
"""Representation of an AirSend Cover.""" """Representation of an AirSend Cover."""
def __init__(self, hass: HomeAssistant, device: Device) -> None: def __init__(self, hass: HomeAssistant, device: Device) -> None:
@@ -117,3 +118,31 @@ class AirSendCover(CoverEntity):
self._attr_current_cover_position = position self._attr_current_cover_position = position
self._closed = position == 0 self._closed = position == 0
self.async_write_ha_state() 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", 4096: "AirSend Button",
4097: "AirSend Switch", 4097: "AirSend Switch",
4098: "AirSend Cover", 4098: "AirSend Cover",
4099: "AirSend Cover (position)", 4099: "AirSend Slider",
4100: "AirSend Light", 4100: "AirSend Tilt",
4101: "AirSend Light",
} }
@@ -90,7 +91,7 @@ class Device:
if self._channel: if self._channel:
result = str(self._channel['id']) result = str(self._channel['id'])
if result: if result:
uniquefield = ['source', 'mac', 'seed'] uniquefield = ['source', 'mac', 'seed', 'token']
for field in uniquefield: for field in uniquefield:
if field in self._channel: if field in self._channel:
result += "_" result += "_"
@@ -145,7 +146,7 @@ class Device:
@property @property
def is_light(self) -> bool: def is_light(self) -> bool:
return self._rtype == 4100 return self._rtype == 4101
@property @property
def refresh_value(self) -> int: def refresh_value(self) -> int:
+2 -1
View File
@@ -2,10 +2,11 @@
"domain": "airsend", "domain": "airsend",
"name": "AirSend", "name": "AirSend",
"documentation": "https://github.com/devmel/hass_airsend", "documentation": "https://github.com/devmel/hass_airsend",
"issue_tracker": "https://github.com/devmel/hass_airsend/issues",
"dependencies": ["http"], "dependencies": ["http"],
"config_flow": true, "config_flow": true,
"codeowners": ["@devmel"], "codeowners": ["@devmel"],
"requirements": [], "requirements": [],
"version": "4.0", "version": "4.0.0",
"iot_class": "local_push" "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.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo, generate_entity_id from homeassistant.helpers.entity import DeviceInfo, generate_entity_id
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.const import CONF_INTERNAL_URL, UnitOfTemperature, LIGHT_LUX from homeassistant.const import CONF_INTERNAL_URL, UnitOfTemperature, LIGHT_LUX
@@ -51,7 +52,7 @@ async def async_setup_entry(
async_add_entities(entities) async_add_entities(entities)
class AirSendAnySensor(SensorEntity): class AirSendAnySensor(RestoreEntity, SensorEntity):
"""Generic AirSend sensor (type 1) — no coordinator, push only.""" """Generic AirSend sensor (type 1) — no coordinator, push only."""
def __init__(self, hass: HomeAssistant, device: Device, internal_url: str) -> None: def __init__(self, hass: HomeAssistant, device: Device, internal_url: str) -> None:
@@ -83,6 +84,18 @@ class AirSendAnySensor(SensorEntity):
def device_info(self) -> DeviceInfo: def device_info(self) -> DeviceInfo:
return self._device.device_info 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): class AirSendTempSensor(CoordinatorEntity, SensorEntity):
"""AirSend device temperature sensor — uses shared coordinator.""" """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.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.const import CONF_INTERNAL_URL from homeassistant.const import CONF_INTERNAL_URL
from . import DOMAIN from . import DOMAIN
@@ -28,7 +29,7 @@ async def async_setup_entry(
async_add_entities(entities) async_add_entities(entities)
class AirSendSwitch(SwitchEntity): class AirSendSwitch(RestoreEntity, SwitchEntity):
"""Representation of an AirSend Switch.""" """Representation of an AirSend Switch."""
def __init__(self, hass: HomeAssistant, device: Device) -> None: 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"}): if await self._send({"method": 1, "type": 0, "value": "OFF"}):
self._state = False self._state = False
self.async_write_ha_state() 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()
+1 -3
View File
@@ -2,6 +2,4 @@
# Use this file to store secrets like usernames and passwords. # Use this file to store secrets like usernames and passwords.
# Learn more at https://www.home-assistant.io/docs/configuration/secrets/ # Learn more at https://www.home-assistant.io/docs/configuration/secrets/
some_password: welcome some_password: welcome
apiKey: 18xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx26 spurl: sp://airsend_password@[fe80::xxxx:xxxx:xxxx:xxxx]?gw=0&rhost=192.168.xxx.xxx
spurl: sp://airsend_password@192.168.0.2