Rfxtrx use previous received event to do complete restore (#37819)
* Add event attribute to display last received event * Restore state using event attribute * Allow empty dict for device config * Must also validate normal case * Do early return
This commit is contained in:
1 parent
f5b628c04f
commit
7c9be024bb
6 files changed
+72
-39
No files matched your search
@@ -12,6 +12,7 @@ from homeassistant.const import (
|
||||
)
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers import event as evt
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
|
||||
from . import (
|
||||
CONF_AUTOMATIC_ADD,
|
||||
@@ -25,6 +26,7 @@ from . import (
|
||||
get_rfx_object,
|
||||
)
|
||||
from .const import (
|
||||
ATTR_EVENT,
|
||||
COMMAND_OFF_LIST,
|
||||
COMMAND_ON_LIST,
|
||||
DATA_RFXTRX_CONFIG,
|
||||
@@ -105,7 +107,7 @@ async def async_setup_entry(
|
||||
)
|
||||
|
||||
|
||||
class RfxtrxBinarySensor(BinarySensorEntity):
|
||||
class RfxtrxBinarySensor(BinarySensorEntity, RestoreEntity):
|
||||
"""A representation of a RFXtrx binary sensor."""
|
||||
|
||||
def __init__(
|
||||
@@ -120,7 +122,7 @@ class RfxtrxBinarySensor(BinarySensorEntity):
|
||||
event=None,
|
||||
):
|
||||
"""Initialize the RFXtrx sensor."""
|
||||
self.event = None
|
||||
self._event = None
|
||||
self._device = device
|
||||
self._name = f"{device.type_string} {device.id_string}"
|
||||
self._device_class = device_class
|
||||
@@ -141,6 +143,13 @@ class RfxtrxBinarySensor(BinarySensorEntity):
|
||||
"""Restore RFXtrx switch device state (ON/OFF)."""
|
||||
await super().async_added_to_hass()
|
||||
|
||||
if self._event is None:
|
||||
old_state = await self.async_get_last_state()
|
||||
if old_state is not None:
|
||||
event = old_state.attributes.get(ATTR_EVENT)
|
||||
if event:
|
||||
self._apply_event(get_rfx_object(event))
|
||||
|
||||
self.async_on_remove(
|
||||
self.hass.helpers.dispatcher.async_dispatcher_connect(
|
||||
SIGNAL_EVENT, self._handle_event
|
||||
@@ -152,6 +161,13 @@ class RfxtrxBinarySensor(BinarySensorEntity):
|
||||
"""Return the device name."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the device state attributes."""
|
||||
if not self._event:
|
||||
return None
|
||||
return {ATTR_EVENT: "".join(f"{x:02x}" for x in self._event.data)}
|
||||
|
||||
@property
|
||||
def data_bits(self):
|
||||
"""Return the number of data bits."""
|
||||
@@ -221,6 +237,7 @@ class RfxtrxBinarySensor(BinarySensorEntity):
|
||||
|
||||
def _apply_event(self, event):
|
||||
"""Apply command from rfxtrx."""
|
||||
self._event = event
|
||||
if event.device.packettype == DEVICE_PACKET_TYPE_LIGHTING4:
|
||||
self._apply_event_lighting4(event)
|
||||
else:
|
||||
|
||||
Reference in new issue
Block a user