Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hassio-addon-info feedback #6734

Merged
merged 9 commits into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
131 changes: 98 additions & 33 deletions hassio/src/addon-view/info/hassio-addon-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import {
CSSResult,
customElement,
html,
internalProperty,
LitElement,
property,
internalProperty,
TemplateResult,
} from "lit-element";
import { classMap } from "lit-html/directives/class-map";
Expand All @@ -34,25 +34,32 @@ import "../../../../src/components/buttons/ha-progress-button";
import "../../../../src/components/ha-card";
import "../../../../src/components/ha-label-badge";
import "../../../../src/components/ha-markdown";
import "../../../../src/components/ha-settings-row";
import "../../../../src/components/ha-svg-icon";
import "../../../../src/components/ha-switch";
import {
fetchHassioAddonChangelog,
fetchHassioAddonInfo,
HassioAddonDetails,
HassioAddonSetOptionParams,
HassioAddonSetSecurityParams,
installHassioAddon,
setHassioAddonOption,
setHassioAddonSecurity,
startHassioAddon,
uninstallHassioAddon,
validateHassioAddonOption,
} from "../../../../src/data/hassio/addon";
import { showConfirmationDialog } from "../../../../src/dialogs/generic/show-dialog-box";
import { extractApiErrorMessage } from "../../../../src/data/hassio/common";
import {
showAlertDialog,
showConfirmationDialog,
} from "../../../../src/dialogs/generic/show-dialog-box";
import { haStyle } from "../../../../src/resources/styles";
import { HomeAssistant } from "../../../../src/types";
import "../../components/hassio-card-content";
import { showHassioMarkdownDialog } from "../../dialogs/markdown/show-dialog-hassio-markdown";
import { hassioStyle } from "../../resources/hassio-style";
import "../../../../src/components/ha-settings-row";

const STAGE_ICON = {
stable: mdiCheckCircle,
Expand Down Expand Up @@ -127,8 +134,6 @@ class HassioAddonInfo extends LitElement {

@internalProperty() private _error?: string;

@property({ type: Boolean }) private _installing = false;

protected render(): TemplateResult {
return html`
${this._computeUpdateAvailable
Expand Down Expand Up @@ -401,7 +406,7 @@ class HassioAddonInfo extends LitElement {
></ha-switch>
</ha-settings-row>

${this.hass.userData?.showAdvanced
${this.addon.startup !== "once"
? html`
<ha-settings-row ?three-line=${this.narrow}>
<span slot="heading">
Expand Down Expand Up @@ -499,12 +504,9 @@ class HassioAddonInfo extends LitElement {
</ha-call-api-button>
`
: html`
<ha-call-api-button
.hass=${this.hass}
.path="hassio/addons/${this.addon.slug}/start"
>
<ha-progress-button @click=${this._startClicked}>
Start
</ha-call-api-button>
</ha-progress-button>
`}
${this._computeShowWebUI
? html`
Expand All @@ -528,12 +530,12 @@ class HassioAddonInfo extends LitElement {
</mwc-button>
`
: ""}
<mwc-button
<ha-progress-button
class=" right warning"
@click=${this._uninstallClicked}
>
Uninstall
</mwc-button>
</ha-progress-button>
${this.addon.build
? html`
<ha-call-api-button
Expand All @@ -555,8 +557,7 @@ class HassioAddonInfo extends LitElement {
`
: ""}
<ha-progress-button
.disabled=${!this.addon.available || this._installing}
.progress=${this._installing}
.disabled=${!this.addon.available}
@click=${this._installClicked}
>
Install
Expand Down Expand Up @@ -663,7 +664,9 @@ class HassioAddonInfo extends LitElement {
};
fireEvent(this, "hass-api-called", eventdata);
} catch (err) {
this._error = `Failed to set addon option, ${err.body?.message || err}`;
this._error = `Failed to set addon option, ${extractApiErrorMessage(
err
)}`;
}
}

Expand All @@ -681,7 +684,9 @@ class HassioAddonInfo extends LitElement {
};
fireEvent(this, "hass-api-called", eventdata);
} catch (err) {
this._error = `Failed to set addon option, ${err.body?.message || err}`;
this._error = `Failed to set addon option, ${extractApiErrorMessage(
err
)}`;
}
}

Expand All @@ -699,7 +704,9 @@ class HassioAddonInfo extends LitElement {
};
fireEvent(this, "hass-api-called", eventdata);
} catch (err) {
this._error = `Failed to set addon option, ${err.body?.message || err}`;
this._error = `Failed to set addon option, ${extractApiErrorMessage(
err
)}`;
}
}

Expand All @@ -717,9 +724,9 @@ class HassioAddonInfo extends LitElement {
};
fireEvent(this, "hass-api-called", eventdata);
} catch (err) {
this._error = `Failed to set addon security option, ${
err.body?.message || err
}`;
this._error = `Failed to set addon security option, ${extractApiErrorMessage(
err
)}`;
}
}

Expand All @@ -737,12 +744,13 @@ class HassioAddonInfo extends LitElement {
};
fireEvent(this, "hass-api-called", eventdata);
} catch (err) {
this._error = `Failed to set addon option, ${err.body?.message || err}`;
this._error = `Failed to set addon option, ${extractApiErrorMessage(
err
)}`;
}
}

private async _openChangelog(): Promise<void> {
this._error = undefined;
try {
const content = await fetchHassioAddonChangelog(
this.hass,
Expand All @@ -753,15 +761,17 @@ class HassioAddonInfo extends LitElement {
content,
});
} catch (err) {
this._error = `Failed to get addon changelog, ${
err.body?.message || err
}`;
showAlertDialog(this, {
title: "Failed to get addon changelog",
text: extractApiErrorMessage(err),
});
}
}

private async _installClicked(): Promise<void> {
this._error = undefined;
this._installing = true;
private async _installClicked(ev: CustomEvent): Promise<void> {
const button = ev.target as any;
button.progress = true;

try {
await installHassioAddon(this.hass, this.addon.slug);
const eventdata = {
Expand All @@ -771,12 +781,62 @@ class HassioAddonInfo extends LitElement {
};
fireEvent(this, "hass-api-called", eventdata);
} catch (err) {
this._error = `Failed to install addon, ${err.body?.message || err}`;
showAlertDialog(this, {
title: "Failed to install addon",
text: extractApiErrorMessage(err),
});
}
this._installing = false;
button.progress = false;
}

private async _uninstallClicked(): Promise<void> {
private async _startClicked(ev: CustomEvent): Promise<void> {
const button = ev.currentTarget as any;
button.progress = true;
try {
const validate = await validateHassioAddonOption(
this.hass,
this.addon.slug
);
if (!validate.data.valid) {
await showConfirmationDialog(this, {
title: "Failed to start addon - configruation validation faled!",
text: validate.data.message.split(" Got ")[0],
confirm: () => this._openConfiguration(),
confirmText: "Go to configruation",
dismissText: "Cancel",
});
button.progress = false;
return;
}
} catch (err) {
showAlertDialog(this, {
title: "Failed to validate addon configuration",
text: extractApiErrorMessage(err),
});
button.progress = false;
return;
}

try {
await startHassioAddon(this.hass, this.addon.slug);
this.addon = await fetchHassioAddonInfo(this.hass, this.addon.slug);
} catch (err) {
showAlertDialog(this, {
title: "Failed to start addon",
text: extractApiErrorMessage(err),
});
}
button.progress = false;
}

private _openConfiguration(): void {
navigate(this, `/hassio/addon/${this.addon.slug}/config`);
}

private async _uninstallClicked(ev: CustomEvent): Promise<void> {
const button = ev.target as any;
button.progress = true;

const confirmed = await showConfirmationDialog(this, {
title: this.addon.name,
text: "Are you sure you want to uninstall this add-on?",
Expand All @@ -785,6 +845,7 @@ class HassioAddonInfo extends LitElement {
});

if (!confirmed) {
button.progress = false;
return;
}

Expand All @@ -798,8 +859,12 @@ class HassioAddonInfo extends LitElement {
};
fireEvent(this, "hass-api-called", eventdata);
} catch (err) {
this._error = `Failed to uninstall addon, ${err.body?.message || err}`;
showAlertDialog(this, {
title: "Failed to uninstall addon",
text: extractApiErrorMessage(err),
});
}
button.progress = false;
}

static get styles(): CSSResult[] {
Expand Down
14 changes: 14 additions & 0 deletions src/data/hassio/addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export interface HassioAddonDetails extends HassioAddonInfo {
changelog: boolean;
hassio_api: boolean;
hassio_role: "default" | "homeassistant" | "manager" | "admin";
startup: "initialize" | "system" | "services" | "application" | "once";
homeassistant_api: boolean;
auth_api: boolean;
full_access: boolean;
Expand Down Expand Up @@ -158,6 +159,19 @@ export const setHassioAddonOption = async (
);
};

export const validateHassioAddonOption = async (
hass: HomeAssistant,
slug: string
) => {
return await hass.callApi<
HassioResponse<{ message: string; valid: boolean }>
>("POST", `hassio/addons/${slug}/options/validate`);
};

export const startHassioAddon = async (hass: HomeAssistant, slug: string) => {
return hass.callApi<string>("POST", `hassio/addons/${slug}/start`);
};

export const setHassioAddonSecurity = async (
hass: HomeAssistant,
slug: string,
Expand Down
8 changes: 8 additions & 0 deletions src/data/hassio/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ export interface HassioResponse<T> {

export const hassioApiResultExtractor = <T>(response: HassioResponse<T>) =>
response.data;

export const extractApiErrorMessage = (error: any): string => {
return typeof error === "object"
? typeof error.body === "object"
? error.body.message || "Unkown error, see logs"
: error.body || "Unkown error, see logs"
: error;
};