Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion custom_components/nodered/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
{
Expand Down
20 changes: 20 additions & 0 deletions tests/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading