From fb7bed2ea0db9c879ff244801eca08b0a1a9c8e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Tue, 15 Oct 2024 13:00:34 +0200 Subject: [PATCH] Add WS endpoint to fetch the details of a backup (#128430) * Add WS endpoint to fetch the details of a backup * Shorten Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Adjust --------- Co-authored-by: Martin Hjelmare Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> --- homeassistant/components/backup/websocket.py | 24 ++++++++++ .../backup/snapshots/test_websocket.ambr | 48 +++++++++++++++++++ tests/components/backup/test_websocket.py | 36 ++++++++++++++ 3 files changed, 108 insertions(+) diff --git a/homeassistant/components/backup/websocket.py b/homeassistant/components/backup/websocket.py index be833edbce5dbe..7daaaad1ec77b7 100644 --- a/homeassistant/components/backup/websocket.py +++ b/homeassistant/components/backup/websocket.py @@ -18,6 +18,7 @@ def async_register_websocket_handlers(hass: HomeAssistant, with_hassio: bool) -> websocket_api.async_register_command(hass, handle_backup_start) return + websocket_api.async_register_command(hass, handle_details) websocket_api.async_register_command(hass, handle_info) websocket_api.async_register_command(hass, handle_create) websocket_api.async_register_command(hass, handle_remove) @@ -43,6 +44,29 @@ async def handle_info( ) +@websocket_api.require_admin +@websocket_api.websocket_command( + { + vol.Required("type"): "backup/details", + vol.Required("slug"): str, + } +) +@websocket_api.async_response +async def handle_details( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], +) -> None: + """Get backup details for a specific slug.""" + backup = await hass.data[DATA_MANAGER].async_get_backup(slug=msg["slug"]) + connection.send_result( + msg["id"], + { + "backup": backup, + }, + ) + + @websocket_api.require_admin @websocket_api.websocket_command( { diff --git a/tests/components/backup/snapshots/test_websocket.ambr b/tests/components/backup/snapshots/test_websocket.ambr index a1d83f5cd75976..07e099561b11a4 100644 --- a/tests/components/backup/snapshots/test_websocket.ambr +++ b/tests/components/backup/snapshots/test_websocket.ambr @@ -147,6 +147,54 @@ 'type': 'result', }) # --- +# name: test_details[with_hassio-with_backup_content] + dict({ + 'error': dict({ + 'code': 'unknown_command', + 'message': 'Unknown command.', + }), + 'id': 1, + 'success': False, + 'type': 'result', + }) +# --- +# name: test_details[with_hassio-without_backup_content] + dict({ + 'error': dict({ + 'code': 'unknown_command', + 'message': 'Unknown command.', + }), + 'id': 1, + 'success': False, + 'type': 'result', + }) +# --- +# name: test_details[without_hassio-with_backup_content] + dict({ + 'id': 1, + 'result': dict({ + 'backup': dict({ + 'date': '1970-01-01T00:00:00.000Z', + 'name': 'Test', + 'path': 'abc123.tar', + 'size': 0.0, + 'slug': 'abc123', + }), + }), + 'success': True, + 'type': 'result', + }) +# --- +# name: test_details[without_hassio-without_backup_content] + dict({ + 'id': 1, + 'result': dict({ + 'backup': None, + }), + 'success': True, + 'type': 'result', + }) +# --- # name: test_generate[with_hassio] dict({ 'error': dict({ diff --git a/tests/components/backup/test_websocket.py b/tests/components/backup/test_websocket.py index 33e997d15e498e..805182391da266 100644 --- a/tests/components/backup/test_websocket.py +++ b/tests/components/backup/test_websocket.py @@ -5,6 +5,7 @@ import pytest from syrupy import SnapshotAssertion +from homeassistant.components.backup.manager import Backup from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError @@ -52,6 +53,41 @@ async def test_info( assert snapshot == await client.receive_json() +@pytest.mark.parametrize( + "backup_content", + [ + pytest.param(TEST_BACKUP, id="with_backup_content"), + pytest.param(None, id="without_backup_content"), + ], +) +@pytest.mark.parametrize( + "with_hassio", + [ + pytest.param(True, id="with_hassio"), + pytest.param(False, id="without_hassio"), + ], +) +async def test_details( + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + snapshot: SnapshotAssertion, + with_hassio: bool, + backup_content: Backup | None, +) -> None: + """Test getting backup info.""" + await setup_backup_integration(hass, with_hassio=with_hassio) + + client = await hass_ws_client(hass) + await hass.async_block_till_done() + + with patch( + "homeassistant.components.backup.manager.BackupManager.async_get_backup", + return_value=backup_content, + ): + await client.send_json_auto_id({"type": "backup/details", "slug": "abc123"}) + assert await client.receive_json() == snapshot + + @pytest.mark.parametrize( "with_hassio", [