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
@@ -11,7 +11,7 @@ from homeassistant.components.sensor import (
|
||||
)
|
||||
from homeassistant.const import CONF_DEVICES
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
|
||||
from . import (
|
||||
CONF_AUTOMATIC_ADD,
|
||||
@@ -21,7 +21,7 @@ from . import (
|
||||
get_device_id,
|
||||
get_rfx_object,
|
||||
)
|
||||
from .const import DATA_RFXTRX_CONFIG
|
||||
from .const import ATTR_EVENT, DATA_RFXTRX_CONFIG
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -113,12 +113,12 @@ async def async_setup_entry(
|
||||
hass.helpers.dispatcher.async_dispatcher_connect(SIGNAL_EVENT, sensor_update)
|
||||
|
||||
|
||||
class RfxtrxSensor(Entity):
|
||||
class RfxtrxSensor(RestoreEntity):
|
||||
"""Representation of a RFXtrx sensor."""
|
||||
|
||||
def __init__(self, device, device_id, data_type, event=None):
|
||||
"""Initialize the sensor."""
|
||||
self.event = None
|
||||
self._event = None
|
||||
self._device = device
|
||||
self._name = f"{device.type_string} {device.id_string} {data_type}"
|
||||
self.data_type = data_type
|
||||
@@ -136,6 +136,13 @@ class RfxtrxSensor(Entity):
|
||||
"""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
|
||||
@@ -149,9 +156,9 @@ class RfxtrxSensor(Entity):
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the sensor."""
|
||||
if not self.event:
|
||||
if not self._event:
|
||||
return None
|
||||
value = self.event.values.get(self.data_type)
|
||||
value = self._event.values.get(self.data_type)
|
||||
return self._convert_fun(value)
|
||||
|
||||
@property
|
||||
@@ -162,9 +169,9 @@ class RfxtrxSensor(Entity):
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the device state attributes."""
|
||||
if not self.event:
|
||||
if not self._event:
|
||||
return None
|
||||
return self.event.values
|
||||
return {ATTR_EVENT: "".join(f"{x:02x}" for x in self._event.data)}
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
@@ -192,7 +199,7 @@ class RfxtrxSensor(Entity):
|
||||
|
||||
def _apply_event(self, event):
|
||||
"""Apply command from rfxtrx."""
|
||||
self.event = event
|
||||
self._event = event
|
||||
|
||||
@callback
|
||||
def _handle_event(self, event, device_id):
|
||||
|
||||
Reference in new issue
Block a user