Skip to content

Commit

Permalink
backups/manager: Emit supervisor update events on backup start/end
Browse files Browse the repository at this point in the history
Send supervisor events to core when backups have been started and
finished so that it's possible to create automations based on such
events.

Closes #2193
  • Loading branch information
3v1n0 committed Apr 27, 2022
1 parent 3299772 commit d385b6b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
65 changes: 64 additions & 1 deletion supervisor/backups/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,22 @@
import asyncio
import logging
from pathlib import Path
from typing import Any

from ..addons.addon import Addon
from ..const import FOLDER_HOMEASSISTANT, CoreState
from ..const import (
ATTR_BACKUP_FULL,
ATTR_BACKUP_PARTIAL,
ATTR_BACKUP_POST,
ATTR_BACKUP_PRE,
ATTR_BACKUPS,
ATTR_DATE,
ATTR_EVENT,
ATTR_NAME,
ATTR_SLUG,
FOLDER_HOMEASSISTANT,
CoreState,
)
from ..coresys import CoreSysAttributes
from ..exceptions import AddonsError
from ..jobs.decorator import Job, JobCondition
Expand Down Expand Up @@ -163,6 +176,14 @@ async def _do_backup(
finally:
self.sys_core.state = CoreState.RUNNING

def _get_backup_event_data(self, backup: Backup) -> dict[str, Any]:
"""Get backup data to be sent as supervisor update event."""
return {
ATTR_SLUG: backup.slug,
ATTR_NAME: backup.name,
ATTR_DATE: backup.date,
}

@Job(conditions=[JobCondition.FREE_SPACE, JobCondition.RUNNING])
async def do_backup_full(self, name="", password=None, compressed=True):
"""Create a full backup."""
Expand All @@ -171,12 +192,33 @@ async def do_backup_full(self, name="", password=None, compressed=True):
return None

backup = self._create_backup(name, BackupType.FULL, password, compressed)
event_data = self._get_backup_event_data(backup)
self.sys_homeassistant.websocket.supervisor_update_event(
"backup",
dict(
{
ATTR_EVENT: ATTR_BACKUP_PRE,
ATTR_BACKUPS: ATTR_BACKUP_FULL,
},
**event_data,
),
)

_LOGGER.info("Creating new full backup with slug %s", backup.slug)
async with self.lock:
backup = await self._do_backup(
backup, self.sys_addons.installed, ALL_FOLDERS, True
)
self.sys_homeassistant.websocket.supervisor_update_event(
"backup",
dict(
{
ATTR_EVENT: ATTR_BACKUP_POST,
ATTR_BACKUPS: ATTR_BACKUP_FULL,
},
**event_data,
),
)
if backup:
_LOGGER.info("Creating full backup with slug %s completed", backup.slug)
return backup
Expand Down Expand Up @@ -208,6 +250,17 @@ async def do_backup_partial(
_LOGGER.error("Nothing to create backup for")

backup = self._create_backup(name, BackupType.PARTIAL, password, compressed)
event_data = self._get_backup_event_data(backup)
self.sys_homeassistant.websocket.supervisor_update_event(
"backup",
dict(
{
ATTR_EVENT: ATTR_BACKUP_PRE,
ATTR_BACKUPS: ATTR_BACKUP_PARTIAL,
},
**event_data,
),
)

_LOGGER.info("Creating new partial backup with slug %s", backup.slug)
async with self.lock:
Expand All @@ -220,6 +273,16 @@ async def do_backup_partial(
_LOGGER.warning("Add-on %s not found/installed", addon_slug)

backup = await self._do_backup(backup, addon_list, folders, homeassistant)
self.sys_homeassistant.websocket.supervisor_update_event(
"backup",
dict(
{
ATTR_EVENT: ATTR_BACKUP_POST,
ATTR_BACKUPS: ATTR_BACKUP_PARTIAL,
},
**event_data,
),
)
if backup:
_LOGGER.info(
"Creating partial backup with slug %s completed", backup.slug
Expand Down
2 changes: 2 additions & 0 deletions supervisor/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@
ATTR_AVAILABLE = "available"
ATTR_BACKUP = "backup"
ATTR_BACKUP_EXCLUDE = "backup_exclude"
ATTR_BACKUP_FULL = "backup_full"
ATTR_BACKUP_PARTIAL = "backup_partial"
ATTR_BACKUP_POST = "backup_post"
ATTR_BACKUP_PRE = "backup_pre"
ATTR_BACKUPS = "backups"
Expand Down

0 comments on commit d385b6b

Please sign in to comment.