From e4eab2546dea4565228e4b07419484a7031f89f8 Mon Sep 17 00:00:00 2001 From: enoch85 <4511254+enoch85@users.noreply.github.com> Date: Wed, 11 Nov 2026 18:08:24 +0100 Subject: [PATCH] fix(websocket): look up the removed device by config entry nodered/device/remove looked the device up with DeviceRegistry.async_get_device, which Home Assistant deprecated in 2026.9 and which stops working in 2027.8. Use async_get_device_by_identifier with the Node-RED config entry instead, and keep async_get_device on Home Assistant before 2026.8, which does not have it. Co-Authored-By: Claude Opus 5.5 --- custom_components/nodered/websocket.py | 20 +++++++++++++++++++- tests/test_websocket.py | 20 ++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/custom_components/nodered/websocket.py b/custom_components/nodered/websocket.py index 60a1f06..3d813c5 100644 --- a/custom_components/nodered/websocket.py +++ b/custom_components/nodered/websocket.py @@ -169,7 +169,7 @@ async def websocket_device_remove( ) -> None: """Remove a device.""" device_registry = dr.async_get(hass) - device = device_registry.async_get_device({(DOMAIN, msg[CONF_NODE_ID])}) + device = _async_get_device(hass, device_registry, msg[CONF_NODE_ID]) if device is not None: entity_registry = async_get(hass) entries = async_entries_for_device(entity_registry, device.id) @@ -184,6 +184,24 @@ async def websocket_device_remove( connection.send_message(result_message(msg[CONF_ID])) +@callback +def _async_get_device( + hass: HomeAssistant, device_registry: dr.DeviceRegistry, node_id: str +) -> dr.DeviceEntry | None: + """Return the Node-RED config entry's device for the node id.""" + identifier = (DOMAIN, node_id) + # Home Assistant before 2026.8 has no async_get_device_by_identifier + if not hasattr(device_registry, "async_get_device_by_identifier"): + return device_registry.async_get_device({identifier}) + for entry in hass.config_entries.async_entries(DOMAIN): + device = device_registry.async_get_device_by_identifier( + identifier, entry.entry_id + ) + if device is not None: + return device + return None + + @require_admin @websocket_command( { diff --git a/tests/test_websocket.py b/tests/test_websocket.py index 9e57aa7..2b11239 100644 --- a/tests/test_websocket.py +++ b/tests/test_websocket.py @@ -164,6 +164,26 @@ async def test_websocket_device_remove_handles_device_and_entities( assert updated_entry.device_id is None +@pytest.mark.asyncio +async def test_websocket_device_remove_does_not_use_deprecated_lookup( + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + caplog: pytest.LogCaptureFixture, +) -> None: + _config_entry, device, _entity = create_device_with_entity(hass, "node-1") + + websocket.register_websocket_handlers(hass) + client = await hass_ws_client(hass) + + await client.send_json( + {"id": 9, "type": "nodered/device/remove", "node_id": "node-1"} + ) + + assert await client.receive_json() == result_message(9) + assert dr.async_get(hass).async_get(device.id) is None + assert "device_registry.async_get_device`" not in caplog.text + + @pytest.mark.asyncio @patch.object(websocket, "async_get_entity") @patch.object(websocket, "async_discover")