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 <MindFreeze@users.noreply.github.com>
This commit is contained in:
Wendelin 2024-11-08 12:53:22 +01:00 committed by GitHub
parent cbfcad71d5
commit d8df380edc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 99 additions and 2 deletions

View File

@ -13,12 +13,13 @@ import { classMap } from "lit/directives/class-map";
import { groupBy } from "../../../../../common/util/group-by"; import { groupBy } from "../../../../../common/util/group-by";
import "../../../../../components/ha-alert"; import "../../../../../components/ha-alert";
import "../../../../../components/ha-card"; import "../../../../../components/ha-card";
import "../../../../../components/ha-icon-next";
import "../../../../../components/ha-select"; import "../../../../../components/ha-select";
import "../../../../../components/ha-settings-row"; import "../../../../../components/ha-settings-row";
import "../../../../../components/ha-svg-icon"; import "../../../../../components/ha-svg-icon";
import "../../../../../components/ha-textfield"; import "../../../../../components/ha-textfield";
import "../../../../../components/ha-selector/ha-selector-boolean"; 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 { computeDeviceName } from "../../../../../data/device_registry";
import type { import type {
ZWaveJSNodeConfigParam, ZWaveJSNodeConfigParam,
@ -29,6 +30,7 @@ import type {
import { import {
fetchZwaveNodeConfigParameters, fetchZwaveNodeConfigParameters,
fetchZwaveNodeMetadata, fetchZwaveNodeMetadata,
invokeZWaveCCApi,
setZwaveNodeConfigParameter, setZwaveNodeConfigParameter,
} from "../../../../../data/zwave_js"; } from "../../../../../data/zwave_js";
import "../../../../../layouts/hass-error-screen"; import "../../../../../layouts/hass-error-screen";
@ -38,6 +40,8 @@ import { haStyle } from "../../../../../resources/styles";
import type { HomeAssistant, Route } from "../../../../../types"; import type { HomeAssistant, Route } from "../../../../../types";
import "../../../ha-config-section"; import "../../../ha-config-section";
import { configTabs } from "./zwave_js-config-router"; import { configTabs } from "./zwave_js-config-router";
import { showConfirmationDialog } from "../../../../../dialogs/generic/show-dialog-box";
import { fireEvent } from "../../../../../common/dom/fire_event";
const icons = { const icons = {
accepted: mdiCheckCircle, accepted: mdiCheckCircle,
@ -67,6 +71,8 @@ class ZWaveJSNodeConfig extends LitElement {
@state() private _error?: string; @state() private _error?: string;
@state() private _resetDialogProgress = false;
public connectedCallback(): void { public connectedCallback(): void {
super.connectedCallback(); super.connectedCallback();
this.deviceId = this.route.path.substr(1); this.deviceId = this.route.path.substr(1);
@ -94,6 +100,8 @@ class ZWaveJSNodeConfig extends LitElement {
const device = this.hass.devices[this.deviceId]; const device = this.hass.devices[this.deviceId];
const deviceName = device ? computeDeviceName(device, this.hass) : "";
return html` return html`
<hass-tabs-subpage <hass-tabs-subpage
.hass=${this.hass} .hass=${this.hass}
@ -114,7 +122,7 @@ class ZWaveJSNodeConfig extends LitElement {
${device ${device
? html` ? html`
<div class="device-info"> <div class="device-info">
<h2>${computeDeviceName(device, this.hass)}</h2> <h2>${deviceName}</h2>
<p>${device.manufacturer} ${device.model}</p> <p>${device.manufacturer} ${device.model}</p>
</div> </div>
` `
@ -174,6 +182,17 @@ class ZWaveJSNodeConfig extends LitElement {
</ha-card> </ha-card>
</div>` </div>`
)} )}
<div class="reset">
<ha-progress-button
.disabled=${this._resetDialogProgress}
.progress=${this._resetDialogProgress}
@click=${this._openResetDialog}
>
${this.hass.localize(
"ui.panel.config.zwave_js.node_config.reset_to_default.button_label"
)}
</ha-progress-button>
</div>
</ha-config-section> </ha-config-section>
</hass-tabs-subpage> </hass-tabs-subpage>
`; `;
@ -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 { static get styles(): CSSResultGroup {
return [ return [
haStyle, haStyle,
@ -520,6 +599,12 @@ class ZWaveJSNodeConfig extends LitElement {
.switch { .switch {
text-align: right; text-align: right;
} }
.reset {
display: flex;
justify-content: flex-end;
margin-bottom: 24px;
}
`, `,
]; ];
} }

View File

@ -4965,6 +4965,18 @@
"set_param_error": "An error occurred.", "set_param_error": "An error occurred.",
"parameter": "Parameter", "parameter": "Parameter",
"bitmask": "Bitmask", "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" "default": "Default"
}, },
"network_status": { "network_status": {