From 87f6b7cdd4c9cfc856b86dfecc96659dae92607d Mon Sep 17 00:00:00 2001 From: Joakim Plate Date: Mon, 13 Jul 2020 02:57:19 +0200 Subject: [PATCH] Replace rfxtrx entity events with integration events (#37565) * Drop per entity events * Correct linting error * Drop keys() --- homeassistant/components/rfxtrx/__init__.py | 88 ++++++++++--------- .../components/rfxtrx/binary_sensor.py | 42 ++------- homeassistant/components/rfxtrx/const.py | 3 + homeassistant/components/rfxtrx/cover.py | 22 ++--- homeassistant/components/rfxtrx/light.py | 22 ++--- homeassistant/components/rfxtrx/sensor.py | 25 ++---- homeassistant/components/rfxtrx/switch.py | 22 ++--- tests/components/rfxtrx/__init__.py | 7 +- tests/components/rfxtrx/test_binary_sensor.py | 3 + tests/components/rfxtrx/test_cover.py | 1 + tests/components/rfxtrx/test_init.py | 74 ++++++---------- tests/components/rfxtrx/test_light.py | 1 + tests/components/rfxtrx/test_sensor.py | 4 +- tests/components/rfxtrx/test_switch.py | 1 + 14 files changed, 132 insertions(+), 183 deletions(-) diff --git a/homeassistant/components/rfxtrx/__init__.py b/homeassistant/components/rfxtrx/__init__.py index 7b3dc6ef276..4f757e3068d 100644 --- a/homeassistant/components/rfxtrx/__init__.py +++ b/homeassistant/components/rfxtrx/__init__.py @@ -8,8 +8,6 @@ import voluptuous as vol from homeassistant.components.binary_sensor import DEVICE_CLASSES_SCHEMA from homeassistant.const import ( - ATTR_ENTITY_ID, - ATTR_STATE, CONF_COMMAND_OFF, CONF_COMMAND_ON, CONF_DEVICE, @@ -27,23 +25,20 @@ from homeassistant.const import ( import homeassistant.helpers.config_validation as cv from homeassistant.helpers.discovery import load_platform from homeassistant.helpers.entity import Entity -from homeassistant.util import slugify -from .const import DEVICE_PACKET_TYPE_LIGHTING4 +from .const import DEVICE_PACKET_TYPE_LIGHTING4, EVENT_RFXTRX_EVENT DOMAIN = "rfxtrx" DEFAULT_SIGNAL_REPETITIONS = 1 -ATTR_FIRE_EVENT = "fire_event" +CONF_FIRE_EVENT = "fire_event" CONF_DATA_BITS = "data_bits" CONF_AUTOMATIC_ADD = "automatic_add" CONF_SIGNAL_REPETITIONS = "signal_repetitions" -CONF_FIRE_EVENT = "fire_event" CONF_DUMMY = "dummy" CONF_DEBUG = "debug" CONF_OFF_DELAY = "off_delay" -EVENT_BUTTON_PRESSED = "button_pressed" SIGNAL_EVENT = f"{DOMAIN}_event" DATA_TYPES = OrderedDict( @@ -118,23 +113,46 @@ CONFIG_SCHEMA = vol.Schema( def setup(hass, config): """Set up the RFXtrx component.""" + + # Setup some per device config + device_events = set() + device_bits = {} + for event_code, event_config in config[DOMAIN][CONF_DEVICES].items(): + event = get_rfx_object(event_code) + device_id = get_device_id( + event.device, data_bits=event_config.get(CONF_DATA_BITS) + ) + device_bits[device_id] = event_config.get(CONF_DATA_BITS) + if event_config[CONF_FIRE_EVENT]: + device_events.add(device_id) + # Declare the Handle event def handle_receive(event): """Handle received messages from RFXtrx gateway.""" # Log RFXCOM event if not event.device.id_string: return - _LOGGER.debug( - "Receive RFXCOM event from " - "(Device_id: %s Class: %s Sub: %s, Pkt_id: %s)", - slugify(event.device.id_string.lower()), - event.device.__class__.__name__, - event.device.subtype, - "".join(f"{x:02x}" for x in event.data), - ) + + event_data = { + "packet_type": event.device.packettype, + "sub_type": event.device.subtype, + "type_string": event.device.type_string, + "id_string": event.device.id_string, + "data": "".join(f"{x:02x}" for x in event.data), + "values": getattr(event, "values", None), + } + + _LOGGER.debug("Receive RFXCOM event: %s", event_data) + + data_bits = get_device_data_bits(event.device, device_bits) + device_id = get_device_id(event.device, data_bits=data_bits) # Callback to HA registered components. - hass.helpers.dispatcher.dispatcher_send(SIGNAL_EVENT, event) + hass.helpers.dispatcher.dispatcher_send(SIGNAL_EVENT, event, device_id) + + # Signal event to any other listeners + if device_id in device_events: + hass.bus.fire(EVENT_RFXTRX_EVENT, event_data) device = config[DOMAIN].get(CONF_DEVICE) host = config[DOMAIN].get(CONF_HOST) @@ -228,6 +246,17 @@ def get_pt2262_cmd(device_id, data_bits): return hex(data[-1] & mask) +def get_device_data_bits(device, device_bits): + """Deduce data bits for device based on a cache of device bits.""" + data_bits = None + if device.packettype == DEVICE_PACKET_TYPE_LIGHTING4: + for device_id, bits in device_bits.items(): + if get_device_id(device, bits) == device_id: + data_bits = bits + break + return data_bits + + def find_possible_pt2262_device(device_ids, device_id): """Look for the device which id matches the given device_id parameter.""" for dev_id in device_ids: @@ -266,35 +295,19 @@ def get_device_id(device, data_bits=None): return (f"{device.packettype:x}", f"{device.subtype:x}", id_string) -def fire_command_event(hass, entity_id, command): - """Fire a command event.""" - hass.bus.fire( - EVENT_BUTTON_PRESSED, {ATTR_ENTITY_ID: entity_id, ATTR_STATE: command.lower()} - ) - _LOGGER.debug( - "Rfxtrx fired event: (event_type: %s, %s: %s, %s: %s)", - EVENT_BUTTON_PRESSED, - ATTR_ENTITY_ID, - entity_id, - ATTR_STATE, - command.lower(), - ) - - class RfxtrxDevice(Entity): """Represents a Rfxtrx device. Contains the common logic for Rfxtrx lights and switches. """ - def __init__(self, device, datas, signal_repetitions, event=None): + def __init__(self, device, device_id, signal_repetitions, event=None): """Initialize the device.""" self.signal_repetitions = signal_repetitions self._name = f"{device.type_string} {device.id_string}" self._device = device - self._state = datas[ATTR_STATE] - self._should_fire_event = datas[ATTR_FIRE_EVENT] - self._device_id = get_device_id(device) + self._state = None + self._device_id = device_id self._unique_id = "_".join(x for x in self._device_id) if event: @@ -310,11 +323,6 @@ class RfxtrxDevice(Entity): """Return the name of the device if any.""" return self._name - @property - def should_fire_event(self): - """Return is the device must fire event.""" - return self._should_fire_event - @property def is_on(self): """Return true if device is on.""" diff --git a/homeassistant/components/rfxtrx/binary_sensor.py b/homeassistant/components/rfxtrx/binary_sensor.py index 4972a8e5622..2b02a4b748d 100644 --- a/homeassistant/components/rfxtrx/binary_sensor.py +++ b/homeassistant/components/rfxtrx/binary_sensor.py @@ -15,14 +15,11 @@ from homeassistant.helpers import event as evt from . import ( CONF_AUTOMATIC_ADD, CONF_DATA_BITS, - CONF_FIRE_EVENT, CONF_OFF_DELAY, SIGNAL_EVENT, find_possible_pt2262_device, - fire_command_event, get_device_id, get_pt2262_cmd, - get_pt2262_deviceid, get_rfx_object, ) from .const import COMMAND_OFF_LIST, COMMAND_ON_LIST, DEVICE_PACKET_TYPE_LIGHTING4 @@ -30,17 +27,6 @@ from .const import COMMAND_OFF_LIST, COMMAND_ON_LIST, DEVICE_PACKET_TYPE_LIGHTIN _LOGGER = logging.getLogger(__name__) -def _get_device_data_bits(device, device_bits): - """Deduce data bits for device based on a cache of device bits.""" - data_bits = None - if device.packettype == DEVICE_PACKET_TYPE_LIGHTING4: - for id_masked, bits in device_bits.items(): - if get_pt2262_deviceid(device.id_string, bits) == id_masked: - data_bits = bits - break - return data_bits - - def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the Binary Sensor platform to RFXtrx.""" if discovery_info is None: @@ -49,7 +35,6 @@ def setup_platform(hass, config, add_entities, discovery_info=None): sensors = [] device_ids = set() - device_bits = {} pt2262_devices = [] @@ -75,8 +60,8 @@ def setup_platform(hass, config, add_entities, discovery_info=None): device = RfxtrxBinarySensor( event.device, + device_id, entity.get(CONF_DEVICE_CLASS), - entity[CONF_FIRE_EVENT], entity.get(CONF_OFF_DELAY), entity.get(CONF_DATA_BITS), entity.get(CONF_COMMAND_ON), @@ -86,14 +71,11 @@ def setup_platform(hass, config, add_entities, discovery_info=None): add_entities(sensors) - def binary_sensor_update(event): + def binary_sensor_update(event, device_id): """Call for control updates from the RFXtrx gateway.""" if not supported(event): return - data_bits = _get_device_data_bits(event.device, device_bits) - - device_id = get_device_id(event.device, data_bits=data_bits) if device_id in device_ids: return device_ids.add(device_id) @@ -105,7 +87,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): event.device.subtype, "".join(f"{x:02x}" for x in event.data), ) - sensor = RfxtrxBinarySensor(event.device, data_bits=data_bits, event=event) + sensor = RfxtrxBinarySensor(event.device, device_id, event=event) add_entities([sensor]) # Subscribe to main RFXtrx events @@ -119,8 +101,8 @@ class RfxtrxBinarySensor(BinarySensorEntity): def __init__( self, device, + device_id, device_class=None, - should_fire=False, off_delay=None, data_bits=None, cmd_on=None, @@ -131,16 +113,15 @@ class RfxtrxBinarySensor(BinarySensorEntity): self.event = None self._device = device self._name = f"{device.type_string} {device.id_string}" - self._should_fire_event = should_fire self._device_class = device_class + self._data_bits = data_bits self._off_delay = off_delay self._state = False self.delay_listener = None - self._data_bits = data_bits self._cmd_on = cmd_on self._cmd_off = cmd_off - self._device_id = get_device_id(device, data_bits=data_bits) + self._device_id = device_id self._unique_id = "_".join(x for x in self._device_id) if event: @@ -181,11 +162,6 @@ class RfxtrxBinarySensor(BinarySensorEntity): """No polling needed.""" return False - @property - def should_fire_event(self): - """Return is the device must fire event.""" - return self._should_fire_event - @property def device_class(self): """Return the sensor class.""" @@ -231,9 +207,9 @@ class RfxtrxBinarySensor(BinarySensorEntity): else: self._apply_event_standard(event) - def _handle_event(self, event): + def _handle_event(self, event, device_id): """Check if event applies to me and update.""" - if get_device_id(event.device, data_bits=self._data_bits) != self._device_id: + if device_id != self._device_id: return _LOGGER.debug( @@ -246,8 +222,6 @@ class RfxtrxBinarySensor(BinarySensorEntity): self._apply_event(event) 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: diff --git a/homeassistant/components/rfxtrx/const.py b/homeassistant/components/rfxtrx/const.py index 40863a69eac..5bd91fd8d47 100644 --- a/homeassistant/components/rfxtrx/const.py +++ b/homeassistant/components/rfxtrx/const.py @@ -14,4 +14,7 @@ COMMAND_OFF_LIST = [ "Down", "Close (inline relay)", ] + DEVICE_PACKET_TYPE_LIGHTING4 = 0x13 + +EVENT_RFXTRX_EVENT = "rfxtrx_event" diff --git a/homeassistant/components/rfxtrx/cover.py b/homeassistant/components/rfxtrx/cover.py index afc37186a47..a9bbe224b26 100644 --- a/homeassistant/components/rfxtrx/cover.py +++ b/homeassistant/components/rfxtrx/cover.py @@ -2,18 +2,15 @@ import logging from homeassistant.components.cover import CoverEntity -from homeassistant.const import ATTR_STATE, CONF_DEVICES, STATE_OPEN +from homeassistant.const import CONF_DEVICES, STATE_OPEN from homeassistant.helpers.restore_state import RestoreEntity from . import ( - ATTR_FIRE_EVENT, CONF_AUTOMATIC_ADD, - CONF_FIRE_EVENT, CONF_SIGNAL_REPETITIONS, DEFAULT_SIGNAL_REPETITIONS, SIGNAL_EVENT, RfxtrxDevice, - fire_command_event, get_device_id, get_rfx_object, ) @@ -46,18 +43,18 @@ def setup_platform(hass, config, add_entities, discovery_info=None): continue device_ids.add(device_id) - datas = {ATTR_STATE: None, ATTR_FIRE_EVENT: entity_info[CONF_FIRE_EVENT]} - entity = RfxtrxCover(event.device, datas, entity_info[CONF_SIGNAL_REPETITIONS]) + entity = RfxtrxCover( + event.device, device_id, entity_info[CONF_SIGNAL_REPETITIONS] + ) entities.append(entity) add_entities(entities) - def cover_update(event): + def cover_update(event, device_id): """Handle cover updates from the RFXtrx gateway.""" if not supported(event): return - device_id = get_device_id(event.device) if device_id in device_ids: return device_ids.add(device_id) @@ -70,9 +67,8 @@ def setup_platform(hass, config, add_entities, discovery_info=None): "".join(f"{x:02x}" for x in event.data), ) - datas = {ATTR_STATE: False, ATTR_FIRE_EVENT: False} entity = RfxtrxCover( - event.device, datas, DEFAULT_SIGNAL_REPETITIONS, event=event + event.device, device_id, DEFAULT_SIGNAL_REPETITIONS, event=event ) add_entities([entity]) @@ -127,13 +123,11 @@ class RfxtrxCover(RfxtrxDevice, CoverEntity, RestoreEntity): elif event.values["Command"] in COMMAND_OFF_LIST: self._state = False - def _handle_event(self, event): + def _handle_event(self, event, device_id): """Check if event applies to me and update.""" - if event.device.id_string != self._device.id_string: + if device_id != self._device_id: return self._apply_event(event) self.schedule_update_ha_state() - if self.should_fire_event: - fire_command_event(self.hass, self.entity_id, event.values["Command"]) diff --git a/homeassistant/components/rfxtrx/light.py b/homeassistant/components/rfxtrx/light.py index c92d9c2ffce..e54cc82a86e 100644 --- a/homeassistant/components/rfxtrx/light.py +++ b/homeassistant/components/rfxtrx/light.py @@ -8,18 +8,15 @@ from homeassistant.components.light import ( SUPPORT_BRIGHTNESS, LightEntity, ) -from homeassistant.const import ATTR_STATE, CONF_DEVICES, STATE_ON +from homeassistant.const import CONF_DEVICES, STATE_ON from homeassistant.helpers.restore_state import RestoreEntity from . import ( - ATTR_FIRE_EVENT, CONF_AUTOMATIC_ADD, - CONF_FIRE_EVENT, CONF_SIGNAL_REPETITIONS, DEFAULT_SIGNAL_REPETITIONS, SIGNAL_EVENT, RfxtrxDevice, - fire_command_event, get_device_id, get_rfx_object, ) @@ -58,19 +55,19 @@ def setup_platform(hass, config, add_entities, discovery_info=None): continue device_ids.add(device_id) - datas = {ATTR_STATE: None, ATTR_FIRE_EVENT: entity_info[CONF_FIRE_EVENT]} - entity = RfxtrxLight(event.device, datas, entity_info[CONF_SIGNAL_REPETITIONS]) + entity = RfxtrxLight( + event.device, device_id, entity_info[CONF_SIGNAL_REPETITIONS] + ) entities.append(entity) add_entities(entities) - def light_update(event): + def light_update(event, device_id): """Handle light updates from the RFXtrx gateway.""" if not supported(event): return - device_id = get_device_id(event.device) if device_id in device_ids: return device_ids.add(device_id) @@ -83,9 +80,8 @@ def setup_platform(hass, config, add_entities, discovery_info=None): "".join(f"{x:02x}" for x in event.data), ) - datas = {ATTR_STATE: None, ATTR_FIRE_EVENT: False} entity = RfxtrxLight( - event.device, datas, DEFAULT_SIGNAL_REPETITIONS, event=event + event.device, device_id, DEFAULT_SIGNAL_REPETITIONS, event=event ) add_entities([entity]) @@ -157,13 +153,11 @@ class RfxtrxLight(RfxtrxDevice, LightEntity, RestoreEntity): self._brightness = event.values["Dim level"] * 255 // 100 self._state = self._brightness > 0 - def _handle_event(self, event): + def _handle_event(self, event, device_id): """Check if event applies to me and update.""" - if event.device.id_string != self._device.id_string: + if device_id != self._device_id: return self._apply_event(event) self.schedule_update_ha_state() - if self.should_fire_event: - fire_command_event(self.hass, self.entity_id, event.values["Command"]) diff --git a/homeassistant/components/rfxtrx/sensor.py b/homeassistant/components/rfxtrx/sensor.py index 52fca503aae..97004231994 100644 --- a/homeassistant/components/rfxtrx/sensor.py +++ b/homeassistant/components/rfxtrx/sensor.py @@ -9,12 +9,11 @@ from homeassistant.components.sensor import ( DEVICE_CLASS_SIGNAL_STRENGTH, DEVICE_CLASS_TEMPERATURE, ) -from homeassistant.const import ATTR_ENTITY_ID, CONF_DEVICES +from homeassistant.const import CONF_DEVICES from homeassistant.helpers.entity import Entity from . import ( CONF_AUTOMATIC_ADD, - CONF_FIRE_EVENT, DATA_TYPES, SIGNAL_EVENT, get_device_id, @@ -63,7 +62,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): return isinstance(event, (ControlEvent, SensorEvent)) entities = [] - for packet_id, entity_info in discovery_info[CONF_DEVICES].items(): + for packet_id in discovery_info[CONF_DEVICES]: event = get_rfx_object(packet_id) if event is None: _LOGGER.error("Invalid device: %s", packet_id) @@ -78,19 +77,16 @@ def setup_platform(hass, config, add_entities, discovery_info=None): continue data_ids.add(data_id) - entity = RfxtrxSensor( - event.device, data_type, entity_info[CONF_FIRE_EVENT], - ) + entity = RfxtrxSensor(event.device, device_id, data_type) entities.append(entity) add_entities(entities) - def sensor_update(event): + def sensor_update(event, device_id): """Handle sensor updates from the RFXtrx gateway.""" if not supported(event): return - device_id = get_device_id(event.device) for data_type in set(event.values) & set(DATA_TYPES): data_id = (*device_id, data_type) if data_id in data_ids: @@ -105,7 +101,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): "".join(f"{x:02x}" for x in event.data), ) - entity = RfxtrxSensor(event.device, data_type, event=event) + entity = RfxtrxSensor(event.device, device_id, data_type, event=event) add_entities([entity]) # Subscribe to main RFXtrx events @@ -116,15 +112,14 @@ def setup_platform(hass, config, add_entities, discovery_info=None): class RfxtrxSensor(Entity): """Representation of a RFXtrx sensor.""" - def __init__(self, device, data_type, should_fire_event=False, event=None): + def __init__(self, device, device_id, data_type, event=None): """Initialize the sensor.""" self.event = None self._device = device self._name = f"{device.type_string} {device.id_string} {data_type}" - self.should_fire_event = should_fire_event self.data_type = data_type self._unit_of_measurement = DATA_TYPES.get(data_type, "") - self._device_id = get_device_id(device) + self._device_id = device_id self._unique_id = "_".join(x for x in (*self._device_id, data_type)) self._device_class = DEVICE_CLASSES.get(data_type) @@ -186,12 +181,12 @@ class RfxtrxSensor(Entity): """Apply command from rfxtrx.""" self.event = event - def _handle_event(self, event): + def _handle_event(self, event, device_id): """Check if event applies to me and update.""" if not isinstance(event, SensorEvent): return - if event.device.id_string != self._device.id_string: + if device_id != self._device_id: return if self.data_type not in event.values: @@ -207,5 +202,3 @@ class RfxtrxSensor(Entity): self._apply_event(event) self.schedule_update_ha_state() - if self.should_fire_event: - self.hass.bus.fire("signal_received", {ATTR_ENTITY_ID: self.entity_id}) diff --git a/homeassistant/components/rfxtrx/switch.py b/homeassistant/components/rfxtrx/switch.py index 3f08deb64a2..9a822034ab8 100644 --- a/homeassistant/components/rfxtrx/switch.py +++ b/homeassistant/components/rfxtrx/switch.py @@ -4,19 +4,16 @@ import logging import RFXtrx as rfxtrxmod from homeassistant.components.switch import SwitchEntity -from homeassistant.const import ATTR_STATE, CONF_DEVICES, STATE_ON +from homeassistant.const import CONF_DEVICES, STATE_ON from homeassistant.helpers.restore_state import RestoreEntity from . import ( - ATTR_FIRE_EVENT, CONF_AUTOMATIC_ADD, - CONF_FIRE_EVENT, CONF_SIGNAL_REPETITIONS, DEFAULT_SIGNAL_REPETITIONS, DOMAIN, SIGNAL_EVENT, RfxtrxDevice, - fire_command_event, get_device_id, get_rfx_object, ) @@ -56,18 +53,18 @@ def setup_platform(hass, config, add_entities_callback, discovery_info=None): continue device_ids.add(device_id) - datas = {ATTR_STATE: None, ATTR_FIRE_EVENT: entity_info[CONF_FIRE_EVENT]} - entity = RfxtrxSwitch(event.device, datas, entity_info[CONF_SIGNAL_REPETITIONS]) + entity = RfxtrxSwitch( + event.device, device_id, entity_info[CONF_SIGNAL_REPETITIONS] + ) entities.append(entity) add_entities_callback(entities) - def switch_update(event): + def switch_update(event, device_id): """Handle sensor updates from the RFXtrx gateway.""" if not supported(event): return - device_id = get_device_id(event.device) if device_id in device_ids: return device_ids.add(device_id) @@ -80,9 +77,8 @@ def setup_platform(hass, config, add_entities_callback, discovery_info=None): "".join(f"{x:02x}" for x in event.data), ) - datas = {ATTR_STATE: None, ATTR_FIRE_EVENT: False} entity = RfxtrxSwitch( - event.device, datas, DEFAULT_SIGNAL_REPETITIONS, event=event + event.device, device_id, DEFAULT_SIGNAL_REPETITIONS, event=event ) add_entities_callback([entity]) @@ -115,16 +111,14 @@ class RfxtrxSwitch(RfxtrxDevice, SwitchEntity, RestoreEntity): elif event.values["Command"] in COMMAND_OFF_LIST: self._state = False - def _handle_event(self, event): + def _handle_event(self, event, device_id): """Check if event applies to me and update.""" - if event.device.id_string != self._device.id_string: + if device_id != self._device_id: return self._apply_event(event) self.schedule_update_ha_state() - if self.should_fire_event: - fire_command_event(self.hass, self.entity_id, event.values["Command"]) def turn_on(self, **kwargs): """Turn the device on.""" diff --git a/tests/components/rfxtrx/__init__.py b/tests/components/rfxtrx/__init__.py index 94f69c8bd90..53d53664591 100644 --- a/tests/components/rfxtrx/__init__.py +++ b/tests/components/rfxtrx/__init__.py @@ -4,6 +4,11 @@ from homeassistant.components import rfxtrx async def _signal_event(hass, packet_id): event = rfxtrx.get_rfx_object(packet_id) - hass.helpers.dispatcher.async_dispatcher_send(rfxtrx.SIGNAL_EVENT, event) + + await hass.async_add_executor_job( + hass.data[rfxtrx.DATA_RFXOBJECT].event_callback, event, + ) + + await hass.async_block_till_done() await hass.async_block_till_done() return event diff --git a/tests/components/rfxtrx/test_binary_sensor.py b/tests/components/rfxtrx/test_binary_sensor.py index 74f20a8c062..7893099c1ff 100644 --- a/tests/components/rfxtrx/test_binary_sensor.py +++ b/tests/components/rfxtrx/test_binary_sensor.py @@ -50,6 +50,7 @@ async def test_one_pt2262(hass, rfxtrx): }, ) await hass.async_block_till_done() + await hass.async_start() state = hass.states.get("binary_sensor.pt2262_22670e") assert state @@ -119,6 +120,7 @@ async def test_discover(hass, rfxtrx): }, ) await hass.async_block_till_done() + await hass.async_start() await _signal_event(hass, "0b1100100118cdea02010f70") state = hass.states.get("binary_sensor.ac_118cdea_2") @@ -145,6 +147,7 @@ async def test_off_delay(hass, rfxtrx): }, ) await hass.async_block_till_done() + await hass.async_start() state = hass.states.get("binary_sensor.ac_118cdea_2") assert state diff --git a/tests/components/rfxtrx/test_cover.py b/tests/components/rfxtrx/test_cover.py index b32fb8aecb3..84c1e18ce5d 100644 --- a/tests/components/rfxtrx/test_cover.py +++ b/tests/components/rfxtrx/test_cover.py @@ -95,6 +95,7 @@ async def test_discover_covers(hass, rfxtrx): {"rfxtrx": {"device": "abcd", "dummy": True, "automatic_add": True}}, ) await hass.async_block_till_done() + await hass.async_start() await _signal_event(hass, "0a140002f38cae010f0070") state = hass.states.get("cover.lightwaverf_siemens_f38cae_1") diff --git a/tests/components/rfxtrx/test_init.py b/tests/components/rfxtrx/test_init.py index fb07cb584b7..c9da7fb23e6 100644 --- a/tests/components/rfxtrx/test_init.py +++ b/tests/components/rfxtrx/test_init.py @@ -66,68 +66,44 @@ async def test_fire_event(hass): + "-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0", "dummy": True, "automatic_add": True, - "devices": {"0b1100cd0213c7f210010f51": {rfxtrx.ATTR_FIRE_EVENT: True}}, + "devices": { + "0b1100cd0213c7f210010f51": {rfxtrx.CONF_FIRE_EVENT: True}, + "0716000100900970": {rfxtrx.CONF_FIRE_EVENT: True}, + }, } }, ) await hass.async_block_till_done() + await hass.async_start() calls = [] @callback def record_event(event): """Add recorded event to set.""" - calls.append(event) + assert event.event_type == "rfxtrx_event" + calls.append(event.data) - hass.bus.async_listen(rfxtrx.EVENT_BUTTON_PRESSED, record_event) - - state = hass.states.get("switch.ac_213c7f2_16") - assert state - assert state.state == "off" + hass.bus.async_listen(rfxtrx.const.EVENT_RFXTRX_EVENT, record_event) await _signal_event(hass, "0b1100cd0213c7f210010f51") + await _signal_event(hass, "0716000100900970") - state = hass.states.get("switch.ac_213c7f2_16") - assert state - assert state.state == "on" - - assert any( - call.data == {"entity_id": "switch.ac_213c7f2_16", "state": "on"} - for call in calls - ) - - -async def test_fire_event_sensor(hass): - """Test fire event.""" - await async_setup_component( - hass, - "rfxtrx", + assert calls == [ { - "rfxtrx": { - "device": "/dev/serial/by-id/usb" - + "-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0", - "dummy": True, - "automatic_add": True, - "devices": {"0a520802060100ff0e0269": {rfxtrx.ATTR_FIRE_EVENT: True}}, - } + "packet_type": 17, + "sub_type": 0, + "type_string": "AC", + "id_string": "213c7f2:16", + "data": "0b1100cd0213c7f210010f51", + "values": {"Command": "On", "Rssi numeric": 5}, }, - ) - - await hass.async_block_till_done() - - calls = [] - - @callback - def record_event(event): - """Add recorded event to set.""" - calls.append(event) - - hass.bus.async_listen("signal_received", record_event) - - await _signal_event(hass, "0a520802060101ff0f0269") - assert len(calls) == 5 - assert any( - call.data - == {"entity_id": "sensor.wt260_wt260h_wt440h_wt450_wt450h_06_01_temperature"} - for call in calls - ) + { + "packet_type": 22, + "sub_type": 0, + "type_string": "Byron SX", + "id_string": "00:90", + "data": "0716000100900970", + "values": {"Sound": 9, "Battery numeric": 0, "Rssi numeric": 7}, + }, + ] diff --git a/tests/components/rfxtrx/test_light.py b/tests/components/rfxtrx/test_light.py index 46b6c10e934..de8fbcbb1e2 100644 --- a/tests/components/rfxtrx/test_light.py +++ b/tests/components/rfxtrx/test_light.py @@ -158,6 +158,7 @@ async def test_discover_light(hass, rfxtrx): {"rfxtrx": {"device": "abcd", "dummy": True, "automatic_add": True}}, ) await hass.async_block_till_done() + await hass.async_start() await _signal_event(hass, "0b11009e00e6116202020070") state = hass.states.get("light.ac_0e61162_2") diff --git a/tests/components/rfxtrx/test_sensor.py b/tests/components/rfxtrx/test_sensor.py index dd1a689acc4..d758fe8ff7e 100644 --- a/tests/components/rfxtrx/test_sensor.py +++ b/tests/components/rfxtrx/test_sensor.py @@ -105,8 +105,8 @@ async def test_several_sensors(hass, rfxtrx): } }, ) - await hass.async_block_till_done() + await hass.async_start() state = hass.states.get("sensor.wt260_wt260h_wt440h_wt450_wt450h_05_02_temperature") assert state @@ -144,6 +144,7 @@ async def test_discover_sensor(hass, rfxtrx): {"rfxtrx": {"device": "abcd", "dummy": True, "automatic_add": True}}, ) await hass.async_block_till_done() + await hass.async_start() # 1 await _signal_event(hass, "0a520801070100b81b0279") @@ -252,6 +253,7 @@ async def test_update_of_sensors(hass, rfxtrx): }, ) await hass.async_block_till_done() + await hass.async_start() state = hass.states.get("sensor.wt260_wt260h_wt440h_wt450_wt450h_05_02_temperature") assert state diff --git a/tests/components/rfxtrx/test_switch.py b/tests/components/rfxtrx/test_switch.py index 5b80c06d7b7..6b01a0ec5e3 100644 --- a/tests/components/rfxtrx/test_switch.py +++ b/tests/components/rfxtrx/test_switch.py @@ -117,6 +117,7 @@ async def test_discover_switch(hass, rfxtrx): {"rfxtrx": {"device": "abcd", "dummy": True, "automatic_add": True}}, ) await hass.async_block_till_done() + await hass.async_start() await _signal_event(hass, "0b1100100118cdea02010f70") state = hass.states.get("switch.ac_118cdea_2")