From d8df380edc9266f5bc8679eba743925835378404 Mon Sep 17 00:00:00 2001 From: Wendelin <12148533+wendevlin@users.noreply.github.com> Date: Fri, 8 Nov 2024 12:53:22 +0100 Subject: [PATCH] Add reset to default to zwave node config (#21991) * Add reset to default to zwave node config * use invoke_cc_api instead of a new API --------- Co-authored-by: Petar Petrov --- .../zwave_js/zwave_js-node-config.ts | 89 ++++++++++++++++++- src/translations/en.json | 12 +++ 2 files changed, 99 insertions(+), 2 deletions(-) diff --git a/src/panels/config/integrations/integration-panels/zwave_js/zwave_js-node-config.ts b/src/panels/config/integrations/integration-panels/zwave_js/zwave_js-node-config.ts index 91be0fa470..7f3336854c 100644 --- a/src/panels/config/integrations/integration-panels/zwave_js/zwave_js-node-config.ts +++ b/src/panels/config/integrations/integration-panels/zwave_js/zwave_js-node-config.ts @@ -13,12 +13,13 @@ import { classMap } from "lit/directives/class-map"; import { groupBy } from "../../../../../common/util/group-by"; import "../../../../../components/ha-alert"; import "../../../../../components/ha-card"; -import "../../../../../components/ha-icon-next"; import "../../../../../components/ha-select"; import "../../../../../components/ha-settings-row"; import "../../../../../components/ha-svg-icon"; import "../../../../../components/ha-textfield"; import "../../../../../components/ha-selector/ha-selector-boolean"; +import "../../../../../components/buttons/ha-progress-button"; +import type { HaProgressButton } from "../../../../../components/buttons/ha-progress-button"; import { computeDeviceName } from "../../../../../data/device_registry"; import type { ZWaveJSNodeConfigParam, @@ -29,6 +30,7 @@ import type { import { fetchZwaveNodeConfigParameters, fetchZwaveNodeMetadata, + invokeZWaveCCApi, setZwaveNodeConfigParameter, } from "../../../../../data/zwave_js"; import "../../../../../layouts/hass-error-screen"; @@ -38,6 +40,8 @@ import { haStyle } from "../../../../../resources/styles"; import type { HomeAssistant, Route } from "../../../../../types"; import "../../../ha-config-section"; import { configTabs } from "./zwave_js-config-router"; +import { showConfirmationDialog } from "../../../../../dialogs/generic/show-dialog-box"; +import { fireEvent } from "../../../../../common/dom/fire_event"; const icons = { accepted: mdiCheckCircle, @@ -67,6 +71,8 @@ class ZWaveJSNodeConfig extends LitElement { @state() private _error?: string; + @state() private _resetDialogProgress = false; + public connectedCallback(): void { super.connectedCallback(); this.deviceId = this.route.path.substr(1); @@ -94,6 +100,8 @@ class ZWaveJSNodeConfig extends LitElement { const device = this.hass.devices[this.deviceId]; + const deviceName = device ? computeDeviceName(device, this.hass) : ""; + return html` -

${computeDeviceName(device, this.hass)}

+

${deviceName}

${device.manufacturer} ${device.model}

` @@ -174,6 +182,17 @@ class ZWaveJSNodeConfig extends LitElement { ` )} +
+ + ${this.hass.localize( + "ui.panel.config.zwave_js.node_config.reset_to_default.button_label" + )} + +
`; @@ -438,6 +457,66 @@ class ZWaveJSNodeConfig extends LitElement { ]); } + private async _openResetDialog(event: Event) { + const progressButton = event.currentTarget as HaProgressButton; + + await showConfirmationDialog(this, { + destructive: true, + title: this.hass.localize( + "ui.panel.config.zwave_js.node_config.reset_to_default.dialog.title" + ), + text: this.hass.localize( + "ui.panel.config.zwave_js.node_config.reset_to_default.dialog.text" + ), + confirmText: this.hass.localize( + "ui.panel.config.zwave_js.node_config.reset_to_default.dialog.reset" + ), + confirm: () => this._resetAllConfigParameters(progressButton), + }); + } + + private async _resetAllConfigParameters(progressButton: HaProgressButton) { + this._resetDialogProgress = true; + fireEvent(this, "hass-notification", { + message: this.hass.localize( + "ui.panel.config.zwave_js.node_config.reset_to_default.dialog.text_loading" + ), + }); + + try { + const device = this.hass.devices[this.deviceId]; + if (!device) { + throw new Error("device_not_found"); + } + await invokeZWaveCCApi( + this.hass, + device.id, + 0x70, // 0x70 is the command class for Configuration + undefined, + "resetAll", + [], + true + ); + + fireEvent(this, "hass-notification", { + message: this.hass.localize( + "ui.panel.config.zwave_js.node_config.reset_to_default.dialog.text_success" + ), + }); + + await this._fetchData(); + progressButton.actionSuccess(); + } catch (err: any) { + fireEvent(this, "hass-notification", { + message: this.hass.localize( + "ui.panel.config.zwave_js.node_config.reset_to_default.dialog.text_error" + ), + }); + progressButton.actionError(); + } + this._resetDialogProgress = false; + } + static get styles(): CSSResultGroup { return [ haStyle, @@ -520,6 +599,12 @@ class ZWaveJSNodeConfig extends LitElement { .switch { text-align: right; } + + .reset { + display: flex; + justify-content: flex-end; + margin-bottom: 24px; + } `, ]; } diff --git a/src/translations/en.json b/src/translations/en.json index 1fe5a23636..b72a7a54d1 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -4965,6 +4965,18 @@ "set_param_error": "An error occurred.", "parameter": "Parameter", "bitmask": "Bitmask", + "reset_to_default": { + "button_label": "Reset to default configuration", + "dialog": { + "title": "Reset to default", + "text": "All device parameters will be reset to their default values.", + "text_loading": "Resetting in progress…", + "text_success": "The device configuration has been reset to its default values.", + "text_error": "Something went wrong while resetting the device to its default values. Please check your logs!", + "reset": "Reset", + "cancel": "Cancel" + } + }, "default": "Default" }, "network_status": {