Switch rfxtrx to dispatcher (#37271)
* Switch to dispatcher * Adjust tests for dispatcher * Store device in sensor * Move state application into binary sensor entity class * Move more specifics into per platform classes * Should not apply event in init of sensor * Switch to call_later * Make apply_event public * No point in slugifying debug logs * Adjust error in off delay and event * Make sure we match with masked id
This commit is contained in:
1 parent
0067b6a84d
commit
2088092f7c
9 files changed
+255
-170
No files matched your search
@@ -16,7 +16,7 @@ from homeassistant.const import (
|
||||
CONF_NAME,
|
||||
)
|
||||
from homeassistant.helpers import config_validation as cv, event as evt
|
||||
from homeassistant.util import dt as dt_util, slugify
|
||||
from homeassistant.util import slugify
|
||||
|
||||
from . import (
|
||||
ATTR_NAME,
|
||||
@@ -25,15 +25,16 @@ from . import (
|
||||
CONF_DEVICES,
|
||||
CONF_FIRE_EVENT,
|
||||
CONF_OFF_DELAY,
|
||||
RECEIVED_EVT_SUBSCRIBERS,
|
||||
RFX_DEVICES,
|
||||
apply_received_command,
|
||||
SIGNAL_EVENT,
|
||||
find_possible_pt2262_device,
|
||||
fire_command_event,
|
||||
get_pt2262_cmd,
|
||||
get_pt2262_device,
|
||||
get_pt2262_deviceid,
|
||||
get_rfx_object,
|
||||
)
|
||||
from .const import COMMAND_OFF_LIST, COMMAND_ON_LIST
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -120,6 +121,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
|
||||
pkt_id = "".join(f"{x:02x}" for x in event.data)
|
||||
sensor = RfxtrxBinarySensor(event, pkt_id)
|
||||
sensor.apply_event(event)
|
||||
RFX_DEVICES[device_id] = sensor
|
||||
add_entities([sensor])
|
||||
_LOGGER.info(
|
||||
@@ -130,43 +132,8 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
event.device.subtype,
|
||||
)
|
||||
|
||||
elif not isinstance(sensor, RfxtrxBinarySensor):
|
||||
return
|
||||
else:
|
||||
_LOGGER.debug(
|
||||
"Binary sensor update (Device ID: %s Class: %s Sub: %s)",
|
||||
slugify(event.device.id_string.lower()),
|
||||
event.device.__class__.__name__,
|
||||
event.device.subtype,
|
||||
)
|
||||
|
||||
if sensor.is_lighting4:
|
||||
if sensor.data_bits is not None:
|
||||
cmd = get_pt2262_cmd(device_id, sensor.data_bits)
|
||||
sensor.apply_cmd(int(cmd, 16))
|
||||
else:
|
||||
sensor.update_state(True)
|
||||
else:
|
||||
apply_received_command(event)
|
||||
|
||||
if (
|
||||
sensor.is_on
|
||||
and sensor.off_delay is not None
|
||||
and sensor.delay_listener is None
|
||||
):
|
||||
|
||||
def off_delay_listener(now):
|
||||
"""Switch device off after a delay."""
|
||||
sensor.delay_listener = None
|
||||
sensor.update_state(False)
|
||||
|
||||
sensor.delay_listener = evt.track_point_in_time(
|
||||
hass, off_delay_listener, dt_util.utcnow() + sensor.off_delay
|
||||
)
|
||||
|
||||
# Subscribe to main RFXtrx events
|
||||
if binary_sensor_update not in RECEIVED_EVT_SUBSCRIBERS:
|
||||
RECEIVED_EVT_SUBSCRIBERS.append(binary_sensor_update)
|
||||
hass.helpers.dispatcher.dispatcher_connect(SIGNAL_EVENT, binary_sensor_update)
|
||||
|
||||
|
||||
class RfxtrxBinarySensor(BinarySensorEntity):
|
||||
@@ -204,6 +171,35 @@ class RfxtrxBinarySensor(BinarySensorEntity):
|
||||
else:
|
||||
self._masked_id = None
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Restore RFXtrx switch device state (ON/OFF)."""
|
||||
await super().async_added_to_hass()
|
||||
|
||||
def _handle_event(event):
|
||||
"""Check if event applies to me and update."""
|
||||
if self._masked_id:
|
||||
masked_id = get_pt2262_deviceid(event.device.id_string, self._data_bits)
|
||||
if masked_id != self._masked_id:
|
||||
return
|
||||
else:
|
||||
if event.device.id_string != self.event.device.id_string:
|
||||
return
|
||||
|
||||
_LOGGER.debug(
|
||||
"Binary sensor update (Device ID: %s Class: %s Sub: %s)",
|
||||
event.device.id_string,
|
||||
event.device.__class__.__name__,
|
||||
event.device.subtype,
|
||||
)
|
||||
|
||||
self.apply_event(event)
|
||||
|
||||
self.async_on_remove(
|
||||
self.hass.helpers.dispatcher.async_dispatcher_connect(
|
||||
SIGNAL_EVENT, _handle_event
|
||||
)
|
||||
)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the device name."""
|
||||
@@ -259,15 +255,45 @@ class RfxtrxBinarySensor(BinarySensorEntity):
|
||||
"""Return unique identifier of remote device."""
|
||||
return self._unique_id
|
||||
|
||||
def apply_cmd(self, cmd):
|
||||
"""Apply a command for updating the state."""
|
||||
if cmd == self.cmd_on:
|
||||
self.update_state(True)
|
||||
elif cmd == self.cmd_off:
|
||||
self.update_state(False)
|
||||
def _apply_event_lighting4(self, event):
|
||||
"""Apply event for a lighting 4 device."""
|
||||
if self.data_bits is not None:
|
||||
cmd = get_pt2262_cmd(event.device.id_string, self.data_bits)
|
||||
cmd = int(cmd, 16)
|
||||
if cmd == self.cmd_on:
|
||||
self._state = True
|
||||
elif cmd == self.cmd_off:
|
||||
self._state = False
|
||||
else:
|
||||
self._state = True
|
||||
|
||||
def _apply_event_standard(self, event):
|
||||
if event.values["Command"] in COMMAND_ON_LIST:
|
||||
self._state = True
|
||||
elif event.values["Command"] in COMMAND_OFF_LIST:
|
||||
self._state = False
|
||||
|
||||
def apply_event(self, event):
|
||||
"""Apply command from rfxtrx."""
|
||||
if self.is_lighting4:
|
||||
self._apply_event_lighting4(event)
|
||||
else:
|
||||
self._apply_event_standard(event)
|
||||
|
||||
def update_state(self, state):
|
||||
"""Update the state of the device."""
|
||||
self._state = state
|
||||
if self.hass:
|
||||
self.schedule_update_ha_state()
|
||||
if self.should_fire_event:
|
||||
fire_command_event(self.hass, self.entity_id, event.values["Command"])
|
||||
|
||||
if self.is_on and self.off_delay is not None and self.delay_listener is None:
|
||||
|
||||
def off_delay_listener(now):
|
||||
"""Switch device off after a delay."""
|
||||
self.delay_listener = None
|
||||
self._state = False
|
||||
if self.hass:
|
||||
self.schedule_update_ha_state()
|
||||
|
||||
self.delay_listener = evt.call_later(
|
||||
self.hass, self.off_delay.total_seconds(), off_delay_listener
|
||||
)
|
||||
Reference in new issue
Block a user