Handle missing hass with backup upload during onboarding (#10523)

This commit is contained in:
Joakim Sørensen 2021-11-04 13:47:30 +01:00 committed by GitHub
parent 2bbb4acf3d
commit 12ef191a0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 62 additions and 84 deletions

View File

@ -16,11 +16,9 @@ declare global {
} }
} }
const MAX_FILE_SIZE = 1 * 1024 * 1024 * 1024; // 1GB
@customElement("hassio-upload-backup") @customElement("hassio-upload-backup")
export class HassioUploadBackup extends LitElement { export class HassioUploadBackup extends LitElement {
public hass!: HomeAssistant; public hass?: HomeAssistant;
@state() public value: string | null = null; @state() public value: string | null = null;
@ -43,20 +41,6 @@ export class HassioUploadBackup extends LitElement {
private async _uploadFile(ev) { private async _uploadFile(ev) {
const file = ev.detail.files[0]; const file = ev.detail.files[0];
if (file.size > MAX_FILE_SIZE) {
showAlertDialog(this, {
title: "Backup file is too big",
text: html`The maximum allowed filesize is 1GB.<br />
<a
href="https://www.home-assistant.io/hassio/haos_common_tasks/#restoring-a-backup-on-a-new-install"
target="_blank"
>Have a look here on how to restore it.</a
>`,
confirmText: "ok",
});
return;
}
if (!["application/x-tar"].includes(file.type)) { if (!["application/x-tar"].includes(file.type)) {
showAlertDialog(this, { showAlertDialog(this, {
title: "Unsupported file format", title: "Unsupported file format",

View File

@ -15,7 +15,7 @@ export class DialogHassioBackupUpload
extends LitElement extends LitElement
implements HassDialog<HassioBackupUploadDialogParams> implements HassDialog<HassioBackupUploadDialogParams>
{ {
@property({ attribute: false }) public hass!: HomeAssistant; @property({ attribute: false }) public hass?: HomeAssistant;
@state() private _params?: HassioBackupUploadDialogParams; @state() private _params?: HassioBackupUploadDialogParams;
@ -54,7 +54,7 @@ export class DialogHassioBackupUpload
<ha-header-bar> <ha-header-bar>
<span slot="title"> Upload backup </span> <span slot="title"> Upload backup </span>
<ha-icon-button <ha-icon-button
.label=${this.hass.localize("common.close")} .label=${this.hass?.localize("common.close") || "close"}
.path=${mdiClose} .path=${mdiClose}
slot="actionItems" slot="actionItems"
dialogAction="cancel" dialogAction="cancel"

View File

@ -35,7 +35,7 @@ class HassioBackupDialog
extends LitElement extends LitElement
implements HassDialog<HassioBackupDialogParams> implements HassDialog<HassioBackupDialogParams>
{ {
@property({ attribute: false }) public hass!: HomeAssistant; @property({ attribute: false }) public hass?: HomeAssistant;
@state() private _error?: string; @state() private _error?: string;
@ -77,7 +77,7 @@ class HassioBackupDialog
<ha-header-bar> <ha-header-bar>
<span slot="title">${this._backup.name}</span> <span slot="title">${this._backup.name}</span>
<ha-icon-button <ha-icon-button
.label=${this.hass.localize("common.close")} .label=${this.hass?.localize("common.close") || "close"}
.path=${mdiClose} .path=${mdiClose}
slot="actionItems" slot="actionItems"
dialogAction="cancel" dialogAction="cancel"
@ -114,7 +114,7 @@ class HassioBackupDialog
@closed=${stopPropagation} @closed=${stopPropagation}
> >
<ha-icon-button <ha-icon-button
.label=${this.hass.localize("common.menu")} .label=${this.hass!.localize("common.menu")}
.path=${mdiDotsVertical} .path=${mdiDotsVertical}
slot="trigger" slot="trigger"
></ha-icon-button> ></ha-icon-button>
@ -192,25 +192,23 @@ class HassioBackupDialog
} }
if (!this._dialogParams?.onboarding) { if (!this._dialogParams?.onboarding) {
this.hass this.hass!.callApi(
.callApi( "POST",
"POST",
`hassio/${ `hassio/${
atLeastVersion(this.hass.config.version, 2021, 9) atLeastVersion(this.hass!.config.version, 2021, 9)
? "backups" ? "backups"
: "snapshots" : "snapshots"
}/${this._backup!.slug}/restore/partial`, }/${this._backup!.slug}/restore/partial`,
backupDetails backupDetails
) ).then(
.then( () => {
() => { this.closeDialog();
this.closeDialog(); },
}, (error) => {
(error) => { this._error = error.body.message;
this._error = error.body.message; }
} );
);
} else { } else {
fireEvent(this, "restoring"); fireEvent(this, "restoring");
fetch(`/api/hassio/backups/${this._backup!.slug}/restore/partial`, { fetch(`/api/hassio/backups/${this._backup!.slug}/restore/partial`, {
@ -244,24 +242,22 @@ class HassioBackupDialog
} }
if (!this._dialogParams?.onboarding) { if (!this._dialogParams?.onboarding) {
this.hass this.hass!.callApi(
.callApi( "POST",
"POST", `hassio/${
`hassio/${ atLeastVersion(this.hass!.config.version, 2021, 9)
atLeastVersion(this.hass.config.version, 2021, 9) ? "backups"
? "backups" : "snapshots"
: "snapshots" }/${this._backup!.slug}/restore/full`,
}/${this._backup!.slug}/restore/full`, backupDetails
backupDetails ).then(
) () => {
.then( this.closeDialog();
() => { },
this.closeDialog(); (error) => {
}, this._error = error.body.message;
(error) => { }
this._error = error.body.message; );
}
);
} else { } else {
fireEvent(this, "restoring"); fireEvent(this, "restoring");
fetch(`/api/hassio/backups/${this._backup!.slug}/restore/full`, { fetch(`/api/hassio/backups/${this._backup!.slug}/restore/full`, {
@ -283,36 +279,33 @@ class HassioBackupDialog
return; return;
} }
this.hass this.hass!.callApi(
atLeastVersion(this.hass!.config.version, 2021, 9) ? "DELETE" : "POST",
.callApi( `hassio/${
atLeastVersion(this.hass.config.version, 2021, 9) ? "DELETE" : "POST", atLeastVersion(this.hass!.config.version, 2021, 9)
`hassio/${ ? `backups/${this._backup!.slug}`
atLeastVersion(this.hass.config.version, 2021, 9) : `snapshots/${this._backup!.slug}/remove`
? `backups/${this._backup!.slug}` }`
: `snapshots/${this._backup!.slug}/remove` ).then(
}` () => {
) if (this._dialogParams!.onDelete) {
.then( this._dialogParams!.onDelete();
() => {
if (this._dialogParams!.onDelete) {
this._dialogParams!.onDelete();
}
this.closeDialog();
},
(error) => {
this._error = error.body.message;
} }
); this.closeDialog();
},
(error) => {
this._error = error.body.message;
}
);
} }
private async _downloadClicked() { private async _downloadClicked() {
let signedPath: { path: string }; let signedPath: { path: string };
try { try {
signedPath = await getSignedPath( signedPath = await getSignedPath(
this.hass, this.hass!,
`/api/hassio/${ `/api/hassio/${
atLeastVersion(this.hass.config.version, 2021, 9) atLeastVersion(this.hass!.config.version, 2021, 9)
? "backups" ? "backups"
: "snapshots" : "snapshots"
}/${this._backup!.slug}/download` }/${this._backup!.slug}/download`

View File

@ -17,7 +17,7 @@ declare global {
@customElement("ha-file-upload") @customElement("ha-file-upload")
export class HaFileUpload extends LitElement { export class HaFileUpload extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant; @property({ attribute: false }) public hass?: HomeAssistant;
@property() public accept!: string; @property() public accept!: string;
@ -88,7 +88,8 @@ export class HaFileUpload extends LitElement {
<ha-icon-button <ha-icon-button
slot="suffix" slot="suffix"
@click=${this._clearValue} @click=${this._clearValue}
.label=${this.hass.localize("ui.common.close")} .label=${this.hass?.localize("ui.common.close") ||
"close"}
.path=${mdiClose} .path=${mdiClose}
></ha-icon-button> ></ha-icon-button>
` `

View File

@ -79,7 +79,7 @@ export const fetchHassioBackups = async (
}; };
export const fetchHassioBackupInfo = async ( export const fetchHassioBackupInfo = async (
hass: HomeAssistant, hass: HomeAssistant | undefined,
backup: string backup: string
): Promise<HassioBackupDetail> => { ): Promise<HassioBackupDetail> => {
if (hass) { if (hass) {
@ -202,7 +202,7 @@ export const createHassioPartialBackup = async (
}; };
export const uploadBackup = async ( export const uploadBackup = async (
hass: HomeAssistant, hass: HomeAssistant | undefined,
file: File file: File
): Promise<HassioResponse<HassioBackup>> => { ): Promise<HassioResponse<HassioBackup>> => {
const fd = new FormData(); const fd = new FormData();