Rfxtrx fixes for beta (#37957)

This commit is contained in:
Joakim Plate authored and GitHub committed 2020-07-18 16:34:50 -07:00
1 parent d297be2698
commit 8b207df819
6 files changed
+88 -40

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,
@@ -54,7 +56,7 @@ async def async_setup_entry(
_LOGGER.error("Invalid device: %s", packet_id)
continue
if not supported(event):
return
continue
device_id = get_device_id(event.device, data_bits=entity.get(CONF_DATA_BITS))
if device_id in device_ids:
@@ -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."""
@@ -172,6 +188,11 @@ class RfxtrxBinarySensor(BinarySensorEntity):
"""No polling needed."""
return False
@property
def force_update(self) -> bool:
"""We should force updates. Repeated states have meaning."""
return True
@property
def device_class(self):
"""Return the sensor class."""
@@ -221,6 +242,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: