Add backup.create service (#70118)

pull/70311/head
Franck Nijhof 2022-04-20 08:56:05 +02:00 committed by GitHub
parent 725339145f
commit c460100af1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 2 deletions

View File

@ -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)

View File

@ -0,0 +1,3 @@
create:
name: Create backup
description: Create a new backup.

View File

@ -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