From 48b167807503df5bd0d0b1b04681346e49e2f760 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Sun, 28 Apr 2024 18:50:15 +0200 Subject: [PATCH] Add test helper to remove device (#116234) * Add test helper to remove device * Rename * Fix signature --- .../components/config/test_device_registry.py | 81 +++---------------- tests/conftest.py | 11 +++ tests/typing.py | 1 + 3 files changed, 21 insertions(+), 72 deletions(-) diff --git a/tests/components/config/test_device_registry.py b/tests/components/config/test_device_registry.py index f88ae42b98a..1b7eff84472 100644 --- a/tests/components/config/test_device_registry.py +++ b/tests/components/config/test_device_registry.py @@ -278,14 +278,7 @@ async def test_remove_config_entry_from_device( # Try removing a config entry from the device, it should fail because # async_remove_config_entry_device returns False - await ws_client.send_json_auto_id( - { - "type": "config/device_registry/remove_config_entry", - "config_entry_id": entry_1.entry_id, - "device_id": device_entry.id, - } - ) - response = await ws_client.receive_json() + response = await ws_client.remove_device(device_entry.id, entry_1.entry_id) assert not response["success"] assert response["error"]["code"] == "home_assistant_error" @@ -294,14 +287,7 @@ async def test_remove_config_entry_from_device( can_remove = True # Remove the 1st config entry - await ws_client.send_json_auto_id( - { - "type": "config/device_registry/remove_config_entry", - "config_entry_id": entry_1.entry_id, - "device_id": device_entry.id, - } - ) - response = await ws_client.receive_json() + response = await ws_client.remove_device(device_entry.id, entry_1.entry_id) assert response["success"] assert response["result"]["config_entries"] == [entry_2.entry_id] @@ -312,14 +298,7 @@ async def test_remove_config_entry_from_device( } # Remove the 2nd config entry - await ws_client.send_json_auto_id( - { - "type": "config/device_registry/remove_config_entry", - "config_entry_id": entry_2.entry_id, - "device_id": device_entry.id, - } - ) - response = await ws_client.receive_json() + response = await ws_client.remove_device(device_entry.id, entry_2.entry_id) assert response["success"] assert response["result"] is None @@ -398,28 +377,14 @@ async def test_remove_config_entry_from_device_fails( assert device_entry.id != fake_device_id # Try removing a non existing config entry from the device - await ws_client.send_json_auto_id( - { - "type": "config/device_registry/remove_config_entry", - "config_entry_id": fake_entry_id, - "device_id": device_entry.id, - } - ) - response = await ws_client.receive_json() + response = await ws_client.remove_device(device_entry.id, fake_entry_id) assert not response["success"] assert response["error"]["code"] == "home_assistant_error" assert response["error"]["message"] == "Unknown config entry" # Try removing a config entry which does not support removal from the device - await ws_client.send_json_auto_id( - { - "type": "config/device_registry/remove_config_entry", - "config_entry_id": entry_1.entry_id, - "device_id": device_entry.id, - } - ) - response = await ws_client.receive_json() + response = await ws_client.remove_device(device_entry.id, entry_1.entry_id) assert not response["success"] assert response["error"]["code"] == "home_assistant_error" @@ -428,28 +393,14 @@ async def test_remove_config_entry_from_device_fails( ) # Try removing a config entry from a device which does not exist - await ws_client.send_json_auto_id( - { - "type": "config/device_registry/remove_config_entry", - "config_entry_id": entry_2.entry_id, - "device_id": fake_device_id, - } - ) - response = await ws_client.receive_json() + response = await ws_client.remove_device(fake_device_id, entry_2.entry_id) assert not response["success"] assert response["error"]["code"] == "home_assistant_error" assert response["error"]["message"] == "Unknown device" # Try removing a config entry from a device which it's not connected to - await ws_client.send_json_auto_id( - { - "type": "config/device_registry/remove_config_entry", - "config_entry_id": entry_2.entry_id, - "device_id": device_entry.id, - } - ) - response = await ws_client.receive_json() + response = await ws_client.remove_device(device_entry.id, entry_2.entry_id) assert response["success"] assert set(response["result"]["config_entries"]) == { @@ -457,28 +408,14 @@ async def test_remove_config_entry_from_device_fails( entry_3.entry_id, } - await ws_client.send_json_auto_id( - { - "type": "config/device_registry/remove_config_entry", - "config_entry_id": entry_2.entry_id, - "device_id": device_entry.id, - } - ) - response = await ws_client.receive_json() + response = await ws_client.remove_device(device_entry.id, entry_2.entry_id) assert not response["success"] assert response["error"]["code"] == "home_assistant_error" assert response["error"]["message"] == "Config entry not in device" # Try removing a config entry which can't be loaded from a device - allowed - await ws_client.send_json_auto_id( - { - "type": "config/device_registry/remove_config_entry", - "config_entry_id": entry_3.entry_id, - "device_id": device_entry.id, - } - ) - response = await ws_client.receive_json() + response = await ws_client.remove_device(device_entry.id, entry_3.entry_id) assert not response["success"] assert response["error"]["code"] == "home_assistant_error" diff --git a/tests/conftest.py b/tests/conftest.py index 4feae83798f..4852a41c061 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -856,10 +856,21 @@ def hass_ws_client( data["id"] = next(id_generator) return websocket.send_json(data) + async def _remove_device(device_id: str, config_entry_id: str) -> Any: + await _send_json_auto_id( + { + "type": "config/device_registry/remove_config_entry", + "config_entry_id": config_entry_id, + "device_id": device_id, + } + ) + return await websocket.receive_json() + # wrap in client wrapped_websocket = cast(MockHAClientWebSocket, websocket) wrapped_websocket.client = client wrapped_websocket.send_json_auto_id = _send_json_auto_id + wrapped_websocket.remove_device = _remove_device return wrapped_websocket return create_client diff --git a/tests/typing.py b/tests/typing.py index 3e6a7cd4bc3..18824163fd2 100644 --- a/tests/typing.py +++ b/tests/typing.py @@ -20,6 +20,7 @@ class MockHAClientWebSocket(ClientWebSocketResponse): client: TestClient send_json_auto_id: Callable[[dict[str, Any]], Coroutine[Any, Any, None]] + remove_device: Callable[[str, str], Coroutine[Any, Any, Any]] ClientSessionGenerator = Callable[..., Coroutine[Any, Any, TestClient]]