Add service backup.create_automatic (#136152)

This commit is contained in:
Erik Montnemery 2025-01-21 13:40:54 +01:00 committed by GitHub
parent 33a2fa2c85
commit a60d2b69e3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 117 additions and 1 deletions

View File

@ -88,8 +88,26 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
password=None,
)
async def async_handle_create_automatic_service(call: ServiceCall) -> None:
"""Service handler for creating automatic backups."""
config_data = backup_manager.config.data
await backup_manager.async_create_backup(
agent_ids=config_data.create_backup.agent_ids,
include_addons=config_data.create_backup.include_addons,
include_all_addons=config_data.create_backup.include_all_addons,
include_database=config_data.create_backup.include_database,
include_folders=config_data.create_backup.include_folders,
include_homeassistant=True, # always include HA
name=config_data.create_backup.name,
password=config_data.create_backup.password,
with_automatic_settings=True,
)
if not with_hassio:
hass.services.async_register(DOMAIN, "create", async_handle_create_service)
hass.services.async_register(
DOMAIN, "create_automatic", async_handle_create_automatic_service
)
async_register_http_views(hass)

View File

@ -2,6 +2,9 @@
"services": {
"create": {
"service": "mdi:cloud-upload"
},
"create_automatic": {
"service": "mdi:cloud-upload"
}
}
}

View File

@ -1 +1,2 @@
create:
create_automatic:

View File

@ -13,6 +13,10 @@
"create": {
"name": "Create backup",
"description": "Creates a new backup."
},
"create_automatic": {
"name": "Create automatic backup",
"description": "Creates a new backup with automatic backup settings."
}
}
}

View File

@ -11,6 +11,8 @@ from homeassistant.exceptions import ServiceNotFound
from .common import setup_backup_integration
from tests.typing import WebSocketGenerator
@pytest.mark.usefixtures("supervisor_client")
async def test_setup_with_hassio(
@ -45,7 +47,16 @@ async def test_create_service(
service_data=service_data,
)
assert generate_backup.called
generate_backup.assert_called_once_with(
agent_ids=["backup.local"],
include_addons=None,
include_all_addons=False,
include_database=True,
include_folders=None,
include_homeassistant=True,
name=None,
password=None,
)
async def test_create_service_with_hassio(hass: HomeAssistant) -> None:
@ -54,3 +65,82 @@ async def test_create_service_with_hassio(hass: HomeAssistant) -> None:
with pytest.raises(ServiceNotFound):
await hass.services.async_call(DOMAIN, "create", blocking=True)
@pytest.mark.parametrize(
("commands", "expected_kwargs"),
[
(
[],
{
"agent_ids": [],
"include_addons": None,
"include_all_addons": False,
"include_database": True,
"include_folders": None,
"include_homeassistant": True,
"name": None,
"password": None,
"with_automatic_settings": True,
},
),
(
[
{
"type": "backup/config/update",
"create_backup": {
"agent_ids": ["test-agent"],
"include_addons": ["my-addon"],
"include_all_addons": True,
"include_database": False,
"include_folders": ["share"],
"name": "cool_backup",
"password": "hunter2",
},
},
],
{
"agent_ids": ["test-agent"],
"include_addons": ["my-addon"],
"include_all_addons": True,
"include_database": False,
"include_folders": ["share"],
"include_homeassistant": True,
"name": "cool_backup",
"password": "hunter2",
"with_automatic_settings": True,
},
),
],
)
@pytest.mark.parametrize("service_data", [None, {}])
@pytest.mark.parametrize("with_hassio", [True, False])
@pytest.mark.usefixtures("supervisor_client")
async def test_create_automatic_service(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
commands: list[dict[str, Any]],
expected_kwargs: dict[str, Any],
service_data: dict[str, Any] | None,
with_hassio: bool,
) -> None:
"""Test generate backup."""
await setup_backup_integration(hass, with_hassio=with_hassio)
client = await hass_ws_client(hass)
for command in commands:
await client.send_json_auto_id(command)
result = await client.receive_json()
assert result["success"]
with patch(
"homeassistant.components.backup.manager.BackupManager.async_create_backup",
) as generate_backup:
await hass.services.async_call(
DOMAIN,
"create_automatic",
blocking=True,
service_data=service_data,
)
generate_backup.assert_called_once_with(**expected_kwargs)