From c460100af1ddc14f6fca5a4437c6eed6c8dd317d Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Wed, 20 Apr 2022 08:56:05 +0200 Subject: [PATCH] Add backup.create service (#70118) --- homeassistant/components/backup/__init__.py | 11 ++++++++-- homeassistant/components/backup/services.yaml | 3 +++ tests/components/backup/test_init.py | 21 +++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 homeassistant/components/backup/services.yaml diff --git a/homeassistant/components/backup/__init__.py b/homeassistant/components/backup/__init__.py index 711051507d4d9b..8d5f9764959cb6 100644 --- a/homeassistant/components/backup/__init__.py +++ b/homeassistant/components/backup/__init__.py @@ -1,6 +1,6 @@ """The Backup integration.""" from homeassistant.components.hassio import is_hassio -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, ServiceCall from homeassistant.helpers.typing import ConfigType from .const import DOMAIN, LOGGER @@ -18,7 +18,14 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: ) return False - hass.data[DOMAIN] = BackupManager(hass) + backup_manager = BackupManager(hass) + hass.data[DOMAIN] = backup_manager + + async def async_handle_create_service(call: ServiceCall) -> None: + """Service handler for creating backups.""" + await backup_manager.generate_backup() + + hass.services.async_register(DOMAIN, "create", async_handle_create_service) async_register_websocket_handlers(hass) async_register_http_views(hass) diff --git a/homeassistant/components/backup/services.yaml b/homeassistant/components/backup/services.yaml new file mode 100644 index 00000000000000..d001c57ef5caf3 --- /dev/null +++ b/homeassistant/components/backup/services.yaml @@ -0,0 +1,3 @@ +create: + name: Create backup + description: Create a new backup. diff --git a/tests/components/backup/test_init.py b/tests/components/backup/test_init.py index a9e2fe20c6b5e3..0087b35e2bc612 100644 --- a/tests/components/backup/test_init.py +++ b/tests/components/backup/test_init.py @@ -1,6 +1,9 @@ """Tests for the Backup integration.""" +from unittest.mock import patch + import pytest +from homeassistant.components.backup.const import DOMAIN from homeassistant.core import HomeAssistant from .common import setup_backup_integration @@ -16,3 +19,21 @@ async def test_setup_with_hassio( "The backup integration is not supported on this installation method, please remove it from your configuration" in caplog.text ) + + +async def test_create_service( + hass: HomeAssistant, +) -> None: + """Test generate backup.""" + await setup_backup_integration(hass) + + with patch( + "homeassistant.components.backup.websocket.BackupManager.generate_backup", + ) as generate_backup: + await hass.services.async_call( + DOMAIN, + "create", + blocking=True, + ) + + assert generate_backup.called