diff --git a/homeassistant/components/rflink/__init__.py b/homeassistant/components/rflink/__init__.py index 68783c3426a..18f02d66a31 100644 --- a/homeassistant/components/rflink/__init__.py +++ b/homeassistant/components/rflink/__init__.py @@ -67,6 +67,7 @@ SERVICE_SEND_COMMAND = "send_command" SIGNAL_AVAILABILITY = "rflink_device_available" SIGNAL_HANDLE_EVENT = "rflink_handle_event_{}" +SIGNAL_EVENT = "rflink_event" TMP_ENTITY = "tmp.{}" @@ -140,6 +141,15 @@ async def async_setup(hass, config): ) ): _LOGGER.error("Failed Rflink command for %s", str(call.data)) + else: + async_dispatcher_send( + hass, + SIGNAL_EVENT, + { + EVENT_KEY_ID: call.data.get(CONF_DEVICE_ID), + EVENT_KEY_COMMAND: call.data.get(CONF_COMMAND), + }, + ) hass.services.async_register( DOMAIN, SERVICE_SEND_COMMAND, async_send_command, schema=SEND_COMMAND_SCHEMA @@ -293,6 +303,7 @@ async def async_setup(hass, config): _LOGGER.info("Connected to Rflink") hass.async_create_task(connect()) + async_dispatcher_connect(hass, SIGNAL_EVENT, event_callback) return True diff --git a/tests/components/rflink/test_init.py b/tests/components/rflink/test_init.py index 7ba90286e62..233170d8cd2 100644 --- a/tests/components/rflink/test_init.py +++ b/tests/components/rflink/test_init.py @@ -196,6 +196,50 @@ async def test_send_command_invalid_arguments(hass, monkeypatch): assert not success, "send command should not succeed for unknown command" +async def test_send_command_event_propagation(hass, monkeypatch): + """Test event propagation for send_command service.""" + domain = "light" + config = { + "rflink": {"port": "/dev/ttyABC0"}, + domain: { + "platform": "rflink", + "devices": { + "protocol_0_1": {"name": "test1"}, + }, + }, + } + + # setup mocking rflink module + _, _, protocol, _ = await mock_rflink(hass, config, domain, monkeypatch) + + # default value = 'off' + assert hass.states.get(f"{domain}.test1").state == "off" + + hass.async_create_task( + hass.services.async_call( + "rflink", + SERVICE_SEND_COMMAND, + {"device_id": "protocol_0_1", "command": "on"}, + ) + ) + await hass.async_block_till_done() + assert protocol.send_command_ack.call_args_list[0][0][0] == "protocol_0_1" + assert protocol.send_command_ack.call_args_list[0][0][1] == "on" + assert hass.states.get(f"{domain}.test1").state == "on" + + hass.async_create_task( + hass.services.async_call( + "rflink", + SERVICE_SEND_COMMAND, + {"device_id": "protocol_0_1", "command": "alloff"}, + ) + ) + await hass.async_block_till_done() + assert protocol.send_command_ack.call_args_list[1][0][0] == "protocol_0_1" + assert protocol.send_command_ack.call_args_list[1][0][1] == "alloff" + assert hass.states.get(f"{domain}.test1").state == "off" + + async def test_reconnecting_after_disconnect(hass, monkeypatch): """An unexpected disconnect should cause a reconnect.""" domain = "sensor"