From 7ad0b37a9e0ec2f874636dc3ad420291aee7bfef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Thu, 20 Jan 2022 19:12:52 +0100 Subject: [PATCH] Use backend logic for partial backup while updating (#11364) --- .../update-available/update-available-card.ts | 61 +++++-------------- src/data/hassio/addon.ts | 7 ++- src/data/supervisor/core.ts | 7 ++- src/translations/en.json | 1 - 4 files changed, 26 insertions(+), 50 deletions(-) diff --git a/hassio/src/update-available/update-available-card.ts b/hassio/src/update-available/update-available-card.ts index 46e0bc64cc..df35fcd40c 100644 --- a/hassio/src/update-available/update-available-card.ts +++ b/hassio/src/update-available/update-available-card.ts @@ -29,10 +29,6 @@ import { HassioAddonDetails, updateHassioAddon, } from "../../../src/data/hassio/addon"; -import { - createHassioPartialBackup, - HassioPartialBackupCreateParams, -} from "../../../src/data/hassio/backup"; import { extractApiErrorMessage, ignoreSupervisorError, @@ -103,7 +99,7 @@ class UpdateAvailableCard extends LitElement { @state() private _addonInfo?: HassioAddonDetails; - @state() private _action: "backup" | "update" | null = null; + @state() private _updating = false; @state() private _error?: string; @@ -138,7 +134,7 @@ class UpdateAvailableCard extends LitElement { name: this._name, })}

` - : this._action === null + : !this._updating ? html` ${this._changelogContent ? html` @@ -172,18 +168,13 @@ class UpdateAvailableCard extends LitElement { : html`

- ${this._action === "update" - ? this.supervisor.localize("update_available.updating", { - name: this._name, - version: this._version_latest, - }) - : this.supervisor.localize( - "update_available.creating_backup", - { name: this._name } - )} + ${this.supervisor.localize("update_available.updating", { + name: this._name, + version: this._version_latest, + })}

`} - ${this._version !== this._version_latest && this._action === null + ${this._version !== this._version_latest && !this._updating ? html`
${changelog @@ -319,37 +310,16 @@ class UpdateAvailableCard extends LitElement { private async _update() { this._error = undefined; - if (this._shouldCreateBackup) { - let backupArgs: HassioPartialBackupCreateParams; - if (this._updateType === "addon") { - backupArgs = { - name: `addon_${this.addonSlug}_${this._version}`, - addons: [this.addonSlug!], - homeassistant: false, - }; - } else { - backupArgs = { - name: `${this._updateType}_${this._version}`, - folders: ["homeassistant"], - homeassistant: true, - }; - } - this._action = "backup"; - try { - await createHassioPartialBackup(this.hass, backupArgs); - } catch (err: any) { - this._error = extractApiErrorMessage(err); - this._action = null; - return; - } - } - - this._action = "update"; + this._updating = true; try { if (this._updateType === "addon") { - await updateHassioAddon(this.hass, this.addonSlug!); + await updateHassioAddon( + this.hass, + this.addonSlug!, + this._shouldCreateBackup + ); } else if (this._updateType === "core") { - await updateCore(this.hass); + await updateCore(this.hass, this._shouldCreateBackup); } else if (this._updateType === "os") { await updateOS(this.hass); } else if (this._updateType === "supervisor") { @@ -358,11 +328,12 @@ class UpdateAvailableCard extends LitElement { } catch (err: any) { if (this.hass.connection.connected && !ignoreSupervisorError(err)) { this._error = extractApiErrorMessage(err); - this._action = null; + this._updating = false; return; } } fireEvent(this, "update-complete"); + this._updating = false; } static get styles(): CSSResultGroup { diff --git a/src/data/hassio/addon.ts b/src/data/hassio/addon.ts index 93ac68c506..ccd099edc7 100644 --- a/src/data/hassio/addon.ts +++ b/src/data/hassio/addon.ts @@ -302,7 +302,8 @@ export const installHassioAddon = async ( export const updateHassioAddon = async ( hass: HomeAssistant, - slug: string + slug: string, + backup: boolean ): Promise => { if (atLeastVersion(hass.config.version, 2021, 2, 4)) { await hass.callWS({ @@ -310,11 +311,13 @@ export const updateHassioAddon = async ( endpoint: `/store/addons/${slug}/update`, method: "post", timeout: null, + data: { backup }, }); } else { await hass.callApi>( "POST", - `hassio/addons/${slug}/update` + `hassio/addons/${slug}/update`, + { backup } ); } }; diff --git a/src/data/supervisor/core.ts b/src/data/supervisor/core.ts index 2d0cec2df7..5e816a8822 100644 --- a/src/data/supervisor/core.ts +++ b/src/data/supervisor/core.ts @@ -6,15 +6,18 @@ export const restartCore = async (hass: HomeAssistant) => { await hass.callService("homeassistant", "restart"); }; -export const updateCore = async (hass: HomeAssistant) => { +export const updateCore = async (hass: HomeAssistant, backup: boolean) => { if (atLeastVersion(hass.config.version, 2021, 2, 4)) { await hass.callWS({ type: "supervisor/api", endpoint: "/core/update", method: "post", timeout: null, + data: { backup }, }); } else { - await hass.callApi>("POST", `hassio/core/update`); + await hass.callApi>("POST", `hassio/core/update`, { + backup, + }); } }; diff --git a/src/translations/en.json b/src/translations/en.json index 70027504fc..d7e11559b5 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -4296,7 +4296,6 @@ "create_backup": "Create backup before updating", "description": "You have {version} installed. Click update to update to version {newest_version}", "updating": "Updating {name} to version {version}", - "creating_backup": "Creating backup of {name}", "no_update": "No update available for {name}" }, "confirm": {