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

Add reset all config for zwave node, Show default value per parameter #21894

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/data/zwave_js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ export interface ZWaveJSNodeConfigParamMetadata {
type: string;
unit: string;
states: { [key: number]: string };
default: any;
}

export interface ZWaveJSSetConfigParamData {
Expand Down Expand Up @@ -284,6 +285,16 @@ export interface ZWaveJSRebuildRoutesStatusMessage {
rebuild_routes_status: { [key: number]: string };
}

export interface ZWaveJSResetAllConfigParamData {
type: string;
device_id: string;
}

export interface ZWaveJSResetAllConfigParamResult {
status?: string;
error?: string;
}

export interface ZWaveJSControllerStatisticsUpdatedMessage {
event: "statistics updated";
source: "controller";
Expand Down Expand Up @@ -872,3 +883,14 @@ export const setZWaveJSLogLevel = (
entry_id,
config: { level },
});

export const resetAllZwaveNodeConfigParameter = (
hass: HomeAssistant,
device_id: string
): Promise<ZWaveJSResetAllConfigParamResult> => {
const data: ZWaveJSResetAllConfigParamData = {
type: "zwave_js/reset_all_config_parameters",
device_id,
};
return hass.callWS(data);
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ 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-switch";
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 "../../../../../components/ha-icon-button";
import { computeDeviceName } from "../../../../../data/device_registry";
import {
ZWaveJSNodeConfigParam,
Expand All @@ -34,6 +36,7 @@ import {
ZwaveJSNodeMetadata,
fetchZwaveNodeConfigParameters,
fetchZwaveNodeMetadata,
resetAllZwaveNodeConfigParameter,
setZwaveNodeConfigParameter,
} from "../../../../../data/zwave_js";
import "../../../../../layouts/hass-error-screen";
Expand All @@ -43,6 +46,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 @@ -72,6 +77,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 @@ -99,6 +106,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 @@ -119,7 +128,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 @@ -179,6 +188,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 All @@ -189,6 +209,11 @@ class ZWaveJSNodeConfig extends LitElement {
item: ZWaveJSNodeConfigParam
): TemplateResult {
const result = this._results[id];

const isTypeBoolean =
item.configuration_value_type === "boolean" ||
this._isEnumeratedBool(item);

const labelAndDescription = html`
<span slot="prefix" class="prefix">
${this.hass.localize("ui.panel.config.zwave_js.node_config.parameter")}
Expand Down Expand Up @@ -240,23 +265,38 @@ class ZWaveJSNodeConfig extends LitElement {
</span>
`;

const defaultLabel =
item.metadata.writeable && item.metadata.default !== undefined
? `${this.hass.localize("ui.panel.config.zwave_js.node_config.default")}:
${
isTypeBoolean
? this.hass.localize(
item.metadata.default === 1
? "ui.common.yes"
: "ui.common.no"
)
: item.configuration_value_type === "enumerated"
? item.metadata.states[item.metadata.default] ||
item.metadata.default
: item.metadata.default
}`
: "";

// Numeric entries with a min value of 0 and max of 1 are considered boolean
if (
item.configuration_value_type === "boolean" ||
this._isEnumeratedBool(item)
) {
if (isTypeBoolean) {
return html`
${labelAndDescription}
<div class="switch">
<ha-switch
<ha-selector-boolean
.property=${item.property}
.endpoint=${item.endpoint}
.propertyKey=${item.property_key}
.checked=${item.value === 1}
.value=${item.value === 1}
.key=${id}
@change=${this._switchToggled}
.disabled=${!item.metadata.writeable}
></ha-switch>
.helper=${defaultLabel}
></ha-selector-boolean>
</div>
`;
}
Expand All @@ -275,10 +315,7 @@ class ZWaveJSNodeConfig extends LitElement {
.disabled=${!item.metadata.writeable}
@change=${this._numericInputChanged}
.suffix=${item.metadata.unit}
.helper=${this.hass.localize(
"ui.panel.config.zwave_js.node_config.between_min_max",
{ min: item.metadata.min, max: item.metadata.max }
)}
.helper=${`${this.hass.localize("ui.panel.config.zwave_js.node_config.between_min_max", { min: item.metadata.min, max: item.metadata.max })}${defaultLabel ? `, ${defaultLabel}` : ""}`}
helperPersistent
>
</ha-textfield>`;
Expand All @@ -295,6 +332,7 @@ class ZWaveJSNodeConfig extends LitElement {
.endpoint=${item.endpoint}
.propertyKey=${item.property_key}
@selected=${this._dropdownSelected}
.helper=${defaultLabel}
>
${Object.entries(item.metadata.states).map(
([key, entityState]) => html`
Expand Down Expand Up @@ -427,6 +465,58 @@ class ZWaveJSNodeConfig extends LitElement {
]);
}

private async _openResetDialog(event: Event) {
const progressButton = event.currentTarget as HaProgressButton;

await showConfirmationDialog(this, {
destructive: true,
title: this.hass.localize(
wendevlin marked this conversation as resolved.
Show resolved Hide resolved
"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 resetAllZwaveNodeConfigParameter(this.hass, device.id);

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 @@ -509,6 +599,12 @@ class ZWaveJSNodeConfig extends LitElement {
.switch {
text-align: right;
}

.reset {
display: flex;
justify-content: flex-end;
margin-bottom: 24px;
}
`,
];
}
Expand Down
15 changes: 14 additions & 1 deletion src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4902,7 +4902,20 @@
"set_param_queued": "The parameter change has been queued, and will be updated when the device wakes up.",
"set_param_error": "An error occurred.",
"parameter": "Parameter",
"bitmask": "Bitmask"
"bitmask": "Bitmask",
"default": "Default",
"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"
}
}
},
"network_status": {
"connected": "Connected",
Expand Down
Loading