From e883abeb4da9fbf5e6599ca7029a11175e2a8407 Mon Sep 17 00:00:00 2001 From: Swamp-Ig Date: Tue, 10 Nov 2026 11:54:13 +0800 Subject: [PATCH 1/3] fix: report device triggers that fail to attach --- custom_components/nodered/websocket.py | 22 +++++++++++++------ tests/test_websocket.py | 29 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/custom_components/nodered/websocket.py b/custom_components/nodered/websocket.py index f7dae76..89df79a 100644 --- a/custom_components/nodered/websocket.py +++ b/custom_components/nodered/websocket.py @@ -405,12 +405,6 @@ def forward_trigger(event: dict[str, Any], _context: Context | None = None) -> N json.dumps(message, cls=NodeRedJSONEncoder, allow_nan=False), ) - def unsubscribe() -> None: - """Remove device trigger.""" - if remove_trigger is not None: - remove_trigger() - _LOGGER.info("Device trigger removed: %s", node_id) - try: trigger_config = await trigger.async_validate_trigger_config( hass, [trigger_data] @@ -454,6 +448,22 @@ def unsubscribe() -> None: ) return + # async_initialize_triggers logs the reason and returns None if attaching failed + if remove_trigger is None: + connection.send_message( + error_message( + msg[CONF_ID], + "trigger_not_attached", + "Device trigger could not be attached, check the Home Assistant log", + ) + ) + return + + def unsubscribe() -> None: + """Remove device trigger.""" + remove_trigger() + _LOGGER.info("Device trigger removed: %s", node_id) + _LOGGER.info("Device trigger created: %s", node_id) _LOGGER.debug("Device trigger config for %s: %s", node_id, trigger_data) connection.subscriptions[msg[CONF_ID]] = unsubscribe diff --git a/tests/test_websocket.py b/tests/test_websocket.py index 9e57aa7..c02def0 100644 --- a/tests/test_websocket.py +++ b/tests/test_websocket.py @@ -751,3 +751,32 @@ def _listener(event: Event) -> None: assert not contrib_announced_version(hass) assert events == [] + + +@pytest.mark.asyncio +@patch.object( + websocket.trigger, "async_initialize_triggers", new=AsyncMock(return_value=None) +) +@patch.object(websocket.trigger, "async_validate_trigger_config") +async def test_websocket_device_trigger_attach_failure( + mock_validate: AsyncMock, + hass: HomeAssistant, +) -> None: + """A trigger that fails to attach replies an error and stores no subscription.""" + mock_validate.side_effect = lambda _hass, config: config + + func: Any = websocket_device_trigger + while hasattr(func, "__wrapped__"): + func = func.__wrapped__ + + fake_conn = FakeConnection() + msg = {"id": 98, "node_id": "node-fail", "device_trigger": {}} + + await func(hass, fake_conn, msg) + + assert fake_conn.sent == error_message( + 98, + "trigger_not_attached", + "Device trigger could not be attached, check the Home Assistant log", + ) + assert fake_conn.subscriptions == {} From 42bdfe2b6b10bc2b6a447d08dd6d6d6cf8d7be1a Mon Sep 17 00:00:00 2001 From: Swamp-Ig Date: Tue, 10 Nov 2026 11:57:44 +0800 Subject: [PATCH 2/3] refactor: tidy device trigger error handling --- custom_components/nodered/websocket.py | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/custom_components/nodered/websocket.py b/custom_components/nodered/websocket.py index 89df79a..7775382 100644 --- a/custom_components/nodered/websocket.py +++ b/custom_components/nodered/websocket.py @@ -417,18 +417,8 @@ def forward_trigger(event: dict[str, Any], _context: Context | None = None) -> N DOMAIN, _LOGGER.log, ) - except vol.MultipleInvalid as err: - _LOGGER.exception("Error initializing device trigger for node_id: %s", node_id) - connection.send_message( - error_message( - msg[CONF_ID], - "invalid_trigger", - str(err), - ) - ) - return except vol.Invalid as err: - _LOGGER.exception("Error initializing device trigger for node_id: %s", node_id) + _LOGGER.error("Invalid device trigger config for node_id %s: %s", node_id, err) connection.send_message( error_message( msg[CONF_ID], From 533ff9a07c9eae0f5b5df917d0fd1328650a336a Mon Sep 17 00:00:00 2001 From: Swamp-Ig Date: Tue, 10 Nov 2026 12:01:41 +0800 Subject: [PATCH 3/3] fix: reply invalid_config for unknown device action domains --- custom_components/nodered/websocket.py | 6 +++--- tests/test_websocket.py | 28 +++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/custom_components/nodered/websocket.py b/custom_components/nodered/websocket.py index 7775382..50a86d6 100644 --- a/custom_components/nodered/websocket.py +++ b/custom_components/nodered/websocket.py @@ -122,11 +122,11 @@ async def websocket_device_action( ) -> None: """Execute a device action.""" context = connection.context(msg) - platform = await device_automation.async_get_device_automation_platform( - hass, msg["action"][CONF_DOMAIN], DeviceAutomationType.ACTION - ) try: + platform = await device_automation.async_get_device_automation_platform( + hass, msg["action"][CONF_DOMAIN], DeviceAutomationType.ACTION + ) if "entity_id" in msg["action"]: entity_registry = async_get(hass) entity_entry = entity_registry.async_get(msg["action"]["entity_id"]) diff --git a/tests/test_websocket.py b/tests/test_websocket.py index c02def0..506c1c7 100644 --- a/tests/test_websocket.py +++ b/tests/test_websocket.py @@ -22,7 +22,10 @@ websocket_device_trigger, websocket_version, ) -from homeassistant.components.device_automation.exceptions import DeviceNotFound +from homeassistant.components.device_automation.exceptions import ( + DeviceNotFound, + InvalidDeviceAutomationConfig, +) from homeassistant.components.webhook import async_register as webhook_real_register from homeassistant.components.websocket_api.messages import ( error_message, @@ -130,6 +133,29 @@ async def fake_get2(_hass: HomeAssistant, _domain: Any, _t: Any) -> Platform: assert sent == error_message(msg["id"], "device_not_found", "no device") +@pytest.mark.asyncio +@patch.object(websocket.device_automation, "async_get_device_automation_platform") +async def test_websocket_device_action_unknown_domain( + mock_get_platform: AsyncMock, + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, +) -> None: + mock_get_platform.side_effect = InvalidDeviceAutomationConfig("no platform") + websocket.register_websocket_handlers(hass) + client = await hass_ws_client(hass) + + await client.send_json( + { + "id": 8, + "type": "nodered/device/action", + "action": {"domain": "nope", "device_id": "dev-3"}, + } + ) + sent = await client.receive_json() + + assert sent == error_message(8, "invalid_config", "no platform") + + @pytest.mark.asyncio async def test_websocket_device_remove_handles_device_and_entities( hass: HomeAssistant,