Add test helper to remove device (#116234)

* Add test helper to remove device

* Rename

* Fix signature
This commit is contained in:
epenet 2024-04-28 18:50:15 +02:00 committed by GitHub
parent 3bcce2197c
commit 48b1678075
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 21 additions and 72 deletions

View file

@ -278,14 +278,7 @@ async def test_remove_config_entry_from_device(
# Try removing a config entry from the device, it should fail because # Try removing a config entry from the device, it should fail because
# async_remove_config_entry_device returns False # async_remove_config_entry_device returns False
await ws_client.send_json_auto_id( response = await ws_client.remove_device(device_entry.id, entry_1.entry_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()
assert not response["success"] assert not response["success"]
assert response["error"]["code"] == "home_assistant_error" assert response["error"]["code"] == "home_assistant_error"
@ -294,14 +287,7 @@ async def test_remove_config_entry_from_device(
can_remove = True can_remove = True
# Remove the 1st config entry # Remove the 1st config entry
await ws_client.send_json_auto_id( response = await ws_client.remove_device(device_entry.id, entry_1.entry_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()
assert response["success"] assert response["success"]
assert response["result"]["config_entries"] == [entry_2.entry_id] 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 # Remove the 2nd config entry
await ws_client.send_json_auto_id( response = await ws_client.remove_device(device_entry.id, entry_2.entry_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()
assert response["success"] assert response["success"]
assert response["result"] is None 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 assert device_entry.id != fake_device_id
# Try removing a non existing config entry from the device # Try removing a non existing config entry from the device
await ws_client.send_json_auto_id( response = await ws_client.remove_device(device_entry.id, fake_entry_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()
assert not response["success"] assert not response["success"]
assert response["error"]["code"] == "home_assistant_error" assert response["error"]["code"] == "home_assistant_error"
assert response["error"]["message"] == "Unknown config entry" assert response["error"]["message"] == "Unknown config entry"
# Try removing a config entry which does not support removal from the device # Try removing a config entry which does not support removal from the device
await ws_client.send_json_auto_id( response = await ws_client.remove_device(device_entry.id, entry_1.entry_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()
assert not response["success"] assert not response["success"]
assert response["error"]["code"] == "home_assistant_error" 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 # Try removing a config entry from a device which does not exist
await ws_client.send_json_auto_id( response = await ws_client.remove_device(fake_device_id, entry_2.entry_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()
assert not response["success"] assert not response["success"]
assert response["error"]["code"] == "home_assistant_error" assert response["error"]["code"] == "home_assistant_error"
assert response["error"]["message"] == "Unknown device" assert response["error"]["message"] == "Unknown device"
# Try removing a config entry from a device which it's not connected to # Try removing a config entry from a device which it's not connected to
await ws_client.send_json_auto_id( response = await ws_client.remove_device(device_entry.id, entry_2.entry_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()
assert response["success"] assert response["success"]
assert set(response["result"]["config_entries"]) == { assert set(response["result"]["config_entries"]) == {
@ -457,28 +408,14 @@ async def test_remove_config_entry_from_device_fails(
entry_3.entry_id, entry_3.entry_id,
} }
await ws_client.send_json_auto_id( response = await ws_client.remove_device(device_entry.id, entry_2.entry_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()
assert not response["success"] assert not response["success"]
assert response["error"]["code"] == "home_assistant_error" assert response["error"]["code"] == "home_assistant_error"
assert response["error"]["message"] == "Config entry not in device" assert response["error"]["message"] == "Config entry not in device"
# Try removing a config entry which can't be loaded from a device - allowed # Try removing a config entry which can't be loaded from a device - allowed
await ws_client.send_json_auto_id( response = await ws_client.remove_device(device_entry.id, entry_3.entry_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()
assert not response["success"] assert not response["success"]
assert response["error"]["code"] == "home_assistant_error" assert response["error"]["code"] == "home_assistant_error"

View file

@ -856,10 +856,21 @@ def hass_ws_client(
data["id"] = next(id_generator) data["id"] = next(id_generator)
return websocket.send_json(data) 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 # wrap in client
wrapped_websocket = cast(MockHAClientWebSocket, websocket) wrapped_websocket = cast(MockHAClientWebSocket, websocket)
wrapped_websocket.client = client wrapped_websocket.client = client
wrapped_websocket.send_json_auto_id = _send_json_auto_id wrapped_websocket.send_json_auto_id = _send_json_auto_id
wrapped_websocket.remove_device = _remove_device
return wrapped_websocket return wrapped_websocket
return create_client return create_client

View file

@ -20,6 +20,7 @@ class MockHAClientWebSocket(ClientWebSocketResponse):
client: TestClient client: TestClient
send_json_auto_id: Callable[[dict[str, Any]], Coroutine[Any, Any, None]] 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]] ClientSessionGenerator = Callable[..., Coroutine[Any, Any, TestClient]]