diff --git a/src/panels/config/backup/dialogs/dialog-restore-backup.ts b/src/panels/config/backup/dialogs/dialog-restore-backup.ts index 9c55ef783e90..18a4b68a0875 100644 --- a/src/panels/config/backup/dialogs/dialog-restore-backup.ts +++ b/src/panels/config/backup/dialogs/dialog-restore-backup.ts @@ -88,11 +88,7 @@ class DialogRestoreBackup extends LitElement implements HassDialog { this._error = undefined; this._stage = undefined; this._step = undefined; - if (this._unsub) { - this._unsub.then((unsub) => unsub()); - this._unsub = undefined; - } - window.removeEventListener("connection-status", this._connectionStatus); + this._unsubscribe(); fireEvent(this, "dialog-closed", { dialog: this.localName }); } @@ -189,15 +185,16 @@ class DialogRestoreBackup extends LitElement implements HassDialog { } private async _restoreBackup() { + this._unsubscribe(); try { this._step = "progress"; window.addEventListener("connection-status", this._connectionStatus); + this._subscribeBackupEvents(); await this._doRestoreBackup( this._userPassword || this._backupEncryptionKey ); - this._subscribeBackupEvents(); } catch (e: any) { - window.removeEventListener("connection-status", this._connectionStatus); + this._unsubscribe(); if (e.code === "password_incorrect") { this._step = "encryption"; } else { @@ -229,6 +226,14 @@ class DialogRestoreBackup extends LitElement implements HassDialog { }); } + private _unsubscribe() { + window.removeEventListener("connection-status", this._connectionStatus); + if (this._unsub) { + this._unsub.then((unsub) => unsub()); + this._unsub = undefined; + } + } + private _restoreState() { switch (this._stage) { case "addon_repositories":