Skip to content

Commit

Permalink
Add backup.create service (home-assistant#70118)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck authored Apr 20, 2022
1 parent 7253391 commit c460100
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
11 changes: 9 additions & 2 deletions homeassistant/components/backup/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions homeassistant/components/backup/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
create:
name: Create backup
description: Create a new backup.
21 changes: 21 additions & 0 deletions tests/components/backup/test_init.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

0 comments on commit c460100

Please sign in to comment.