Skip to content

Commit

Permalink
Add service backup.create_automatic (home-assistant#136152)
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery authored Jan 21, 2025
1 parent 33a2fa2 commit a60d2b6
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 1 deletion.
18 changes: 18 additions & 0 deletions homeassistant/components/backup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,26 @@ async def async_handle_create_service(call: ServiceCall) -> None:
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)

Expand Down
3 changes: 3 additions & 0 deletions homeassistant/components/backup/icons.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"services": {
"create": {
"service": "mdi:cloud-upload"
},
"create_automatic": {
"service": "mdi:cloud-upload"
}
}
}
1 change: 1 addition & 0 deletions homeassistant/components/backup/services.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
create:
create_automatic:
4 changes: 4 additions & 0 deletions homeassistant/components/backup/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
}
}
92 changes: 91 additions & 1 deletion tests/components/backup/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from .common import setup_backup_integration

from tests.typing import WebSocketGenerator


@pytest.mark.usefixtures("supervisor_client")
async def test_setup_with_hassio(
Expand Down Expand Up @@ -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:
Expand All @@ -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)

0 comments on commit a60d2b6

Please sign in to comment.