Skip to content

Commit

Permalink
Add reset to default to zwave node config (#21991)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
wendevlin and MindFreeze authored Nov 8, 2024
1 parent cbfcad7 commit d8df380
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -29,6 +30,7 @@ import type {
import {
fetchZwaveNodeConfigParameters,
fetchZwaveNodeMetadata,
invokeZWaveCCApi,
setZwaveNodeConfigParameter,
} from "../../../../../data/zwave_js";
import "../../../../../layouts/hass-error-screen";
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -94,6 +100,8 @@ class ZWaveJSNodeConfig extends LitElement {

const device = this.hass.devices[this.deviceId];

const deviceName = device ? computeDeviceName(device, this.hass) : "";

return html`
<hass-tabs-subpage
.hass=${this.hass}
Expand All @@ -114,7 +122,7 @@ class ZWaveJSNodeConfig extends LitElement {
${device
? html`
<div class="device-info">
<h2>${computeDeviceName(device, this.hass)}</h2>
<h2>${deviceName}</h2>
<p>${device.manufacturer} ${device.model}</p>
</div>
`
Expand Down Expand Up @@ -174,6 +182,17 @@ class ZWaveJSNodeConfig extends LitElement {
</ha-card>
</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>
</hass-tabs-subpage>
`;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -520,6 +599,12 @@ class ZWaveJSNodeConfig extends LitElement {
.switch {
text-align: right;
}
.reset {
display: flex;
justify-content: flex-end;
margin-bottom: 24px;
}
`,
];
}
Expand Down
12 changes: 12 additions & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit d8df380

Please sign in to comment.