diff --git a/hassio/src/system/hassio-host-info.ts b/hassio/src/system/hassio-host-info.ts index 22c48f2917..dfe7224b4a 100644 --- a/hassio/src/system/hassio-host-info.ts +++ b/hassio/src/system/hassio-host-info.ts @@ -15,6 +15,7 @@ import { } from "lit-element"; import memoizeOne from "memoize-one"; import { atLeastVersion } from "../../../src/common/config/version"; +import "../../../src/components/buttons/ha-progress-button"; import "../../../src/components/ha-button-menu"; import "../../../src/components/ha-card"; import "../../../src/components/ha-settings-row"; @@ -108,12 +109,12 @@ class HassioHostInfo extends LitElement { ${this.hostInfo.version !== this.hostInfo.version_latest && this.hostInfo.features.includes("hassos") ? html` - - + Update + ` : ""} @@ -141,24 +142,24 @@ class HassioHostInfo extends LitElement {
${this.hostInfo.features.includes("reboot") ? html` - - + Reboot + ` : ""} ${this.hostInfo.features.includes("shutdown") ? html` - - + Shutdown + ` : ""} @@ -185,6 +186,177 @@ class HassioHostInfo extends LitElement { `; } + protected firstUpdated(): void { + this._loadData(); + } + + private _primaryIpAddress = memoizeOne((network_info: NetworkInfo) => { + if (!network_info) { + return ""; + } + return Object.keys(network_info?.interfaces) + .map((device) => network_info.interfaces[device]) + .find((device) => device.primary)?.ip_address; + }); + + private async _handleMenuAction(ev: CustomEvent) { + switch (ev.detail.index) { + case 0: + await this._showHardware(); + break; + case 1: + await this._importFromUSB(); + break; + } + } + + private async _showHardware(): Promise { + try { + const content = await fetchHassioHardwareInfo(this.hass); + showHassioMarkdownDialog(this, { + title: "Hardware", + content: `
${safeDump(content, { indent: 2 })}
`, + }); + } catch (err) { + showAlertDialog(this, { + title: "Failed to get Hardware list", + text: + typeof err === "object" ? err.body?.message || "Unkown error" : err, + }); + } + } + + private async _hostReboot(ev: CustomEvent): Promise { + const button = ev.target as any; + button.progress = true; + + const confirmed = await showConfirmationDialog(this, { + title: "Reboot", + text: "Are you sure you want to reboot the host?", + confirmText: "reboot host", + dismissText: "no", + }); + + if (!confirmed) { + button.progress = false; + return; + } + + try { + await rebootHost(this.hass); + } catch (err) { + showAlertDialog(this, { + title: "Failed to reboot", + text: + typeof err === "object" ? err.body?.message || "Unkown error" : err, + }); + } + button.progress = false; + } + + private async _hostShutdown(ev: CustomEvent): Promise { + const button = ev.target as any; + button.progress = true; + + const confirmed = await showConfirmationDialog(this, { + title: "Shutdown", + text: "Are you sure you want to shutdown the host?", + confirmText: "shutdown host", + dismissText: "no", + }); + + if (!confirmed) { + button.progress = false; + return; + } + + try { + await shutdownHost(this.hass); + } catch (err) { + showAlertDialog(this, { + title: "Failed to shutdown", + text: + typeof err === "object" ? err.body?.message || "Unkown error" : err, + }); + } + button.progress = false; + } + + private async _osUpdate(ev: CustomEvent): Promise { + const button = ev.target as any; + button.progress = true; + + const confirmed = await showConfirmationDialog(this, { + title: "Update", + text: "Are you sure you want to update the OS?", + confirmText: "update os", + dismissText: "no", + }); + + if (!confirmed) { + button.progress = false; + return; + } + + try { + await updateOS(this.hass); + } catch (err) { + showAlertDialog(this, { + title: "Failed to update", + text: + typeof err === "object" ? err.body?.message || "Unkown error" : err, + }); + } + button.progress = false; + } + + private async _changeNetworkClicked(): Promise { + showNetworkDialog(this, { + network: this._networkInfo!, + loadData: () => this._loadData(), + }); + } + + private async _changeHostnameClicked(): Promise { + const curHostname: string = this.hostInfo.hostname; + const hostname = await showPromptDialog(this, { + title: "Change hostname", + inputLabel: "Please enter a new hostname:", + inputType: "string", + defaultValue: curHostname, + }); + + if (hostname && hostname !== curHostname) { + try { + await changeHostOptions(this.hass, { hostname }); + this.hostInfo = await fetchHassioHostInfo(this.hass); + } catch (err) { + showAlertDialog(this, { + title: "Setting hostname failed", + text: + typeof err === "object" ? err.body?.message || "Unkown error" : err, + }); + } + } + } + + private async _importFromUSB(): Promise { + try { + await configSyncOS(this.hass); + this.hostInfo = await fetchHassioHostInfo(this.hass); + } catch (err) { + showAlertDialog(this, { + title: "Failed to import from USB", + text: + typeof err === "object" ? err.body?.message || "Unkown error" : err, + }); + } + } + + private async _loadData(): Promise { + this._networkInfo = await fetchNetworkInfo(this.hass); + } + static get styles(): CSSResult[] { return [ haStyle, @@ -240,162 +412,6 @@ class HassioHostInfo extends LitElement { `, ]; } - - protected firstUpdated(): void { - this._loadData(); - } - - private _primaryIpAddress = memoizeOne((network_info: NetworkInfo) => { - if (!network_info) { - return ""; - } - return Object.keys(network_info?.interfaces) - .map((device) => network_info.interfaces[device]) - .find((device) => device.primary)?.ip_address; - }); - - private async _handleMenuAction(ev: CustomEvent) { - switch (ev.detail.index) { - case 0: - await this._showHardware(); - break; - case 1: - await this._importFromUSB(); - break; - } - } - - private async _showHardware(): Promise { - try { - const content = await fetchHassioHardwareInfo(this.hass); - showHassioMarkdownDialog(this, { - title: "Hardware", - content: `
${safeDump(content, { indent: 2 })}
`, - }); - } catch (err) { - showAlertDialog(this, { - title: "Failed to get Hardware list", - text: - typeof err === "object" ? err.body?.message || "Unkown error" : err, - }); - } - } - - private async _hostReboot(): Promise { - const confirmed = await showConfirmationDialog(this, { - title: "Reboot", - text: "Are you sure you want to reboot the host?", - confirmText: "reboot host", - dismissText: "no", - }); - - if (!confirmed) { - return; - } - - try { - await rebootHost(this.hass); - } catch (err) { - showAlertDialog(this, { - title: "Failed to reboot", - text: - typeof err === "object" ? err.body?.message || "Unkown error" : err, - }); - } - } - - private async _hostShutdown(): Promise { - const confirmed = await showConfirmationDialog(this, { - title: "Shutdown", - text: "Are you sure you want to shutdown the host?", - confirmText: "shutdown host", - dismissText: "no", - }); - - if (!confirmed) { - return; - } - - try { - await shutdownHost(this.hass); - } catch (err) { - showAlertDialog(this, { - title: "Failed to shutdown", - text: - typeof err === "object" ? err.body?.message || "Unkown error" : err, - }); - } - } - - private async _osUpdate(): Promise { - const confirmed = await showConfirmationDialog(this, { - title: "Update", - text: "Are you sure you want to update the OS?", - confirmText: "update os", - dismissText: "no", - }); - - if (!confirmed) { - return; - } - - try { - await updateOS(this.hass); - } catch (err) { - showAlertDialog(this, { - title: "Failed to update", - text: - typeof err === "object" ? err.body?.message || "Unkown error" : err, - }); - } - } - - private async _changeNetworkClicked(): Promise { - showNetworkDialog(this, { - network: this._networkInfo!, - loadData: () => this._loadData(), - }); - } - - private async _changeHostnameClicked(): Promise { - const curHostname: string = this.hostInfo.hostname; - const hostname = await showPromptDialog(this, { - title: "Change hostname", - inputLabel: "Please enter a new hostname:", - inputType: "string", - defaultValue: curHostname, - }); - - if (hostname && hostname !== curHostname) { - try { - await changeHostOptions(this.hass, { hostname }); - this.hostInfo = await fetchHassioHostInfo(this.hass); - } catch (err) { - showAlertDialog(this, { - title: "Setting hostname failed", - text: - typeof err === "object" ? err.body?.message || "Unkown error" : err, - }); - } - } - } - - private async _importFromUSB(): Promise { - try { - await configSyncOS(this.hass); - this.hostInfo = await fetchHassioHostInfo(this.hass); - } catch (err) { - showAlertDialog(this, { - title: "Failed to import from USB", - text: - typeof err === "object" ? err.body?.message || "Unkown error" : err, - }); - } - } - - private async _loadData(): Promise { - this._networkInfo = await fetchNetworkInfo(this.hass); - } } declare global {