ZwaveJS: Resume adding a device if the page is refreshed (#22519)

* ZwaveJS: Resume adding a device if the page is refreshed

* tweak code style
This commit is contained in:
Petar Petrov 2024-10-30 10:59:23 +02:00 committed by GitHub
parent bc11c0b3ac
commit 00bd32acba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 57 additions and 55 deletions

View File

@ -846,7 +846,11 @@ class DialogZWaveJSAddNode extends LitElement {
undefined, undefined,
undefined, undefined,
dsk dsk
); ).catch((err) => {
this._error = err.message;
this._status = "failed";
return () => {};
});
this._addNodeTimeoutHandle = window.setTimeout(() => { this._addNodeTimeoutHandle = window.setTimeout(() => {
this._unsubscribe(); this._unsubscribe();
this._status = "timed_out"; this._status = "timed_out";

View File

@ -2,6 +2,7 @@ import "@material/mwc-button/mwc-button";
import { mdiCheckCircle, mdiCloseCircle } from "@mdi/js"; import { mdiCheckCircle, mdiCloseCircle } from "@mdi/js";
import { css, CSSResultGroup, html, LitElement, nothing } from "lit"; import { css, CSSResultGroup, html, LitElement, nothing } from "lit";
import { customElement, property, state } from "lit/decorators"; import { customElement, property, state } from "lit/decorators";
import { UnsubscribeFunc } from "home-assistant-js-websocket";
import { fireEvent } from "../../../../../common/dom/fire_event"; import { fireEvent } from "../../../../../common/dom/fire_event";
import "../../../../../components/ha-circular-progress"; import "../../../../../components/ha-circular-progress";
import { createCloseHeading } from "../../../../../components/ha-dialog"; import { createCloseHeading } from "../../../../../components/ha-dialog";
@ -25,9 +26,13 @@ class DialogZWaveJSRemoveNode extends LitElement {
@state() private _node?: ZWaveJSRemovedNode; @state() private _node?: ZWaveJSRemovedNode;
@state() private _removedCallback?: () => void;
private _removeNodeTimeoutHandle?: number; private _removeNodeTimeoutHandle?: number;
private _subscribed?: Promise<() => Promise<void>>; private _subscribed?: Promise<UnsubscribeFunc>;
@state() private _error?: string;
public disconnectedCallback(): void { public disconnectedCallback(): void {
super.disconnectedCallback(); super.disconnectedCallback();
@ -38,6 +43,10 @@ class DialogZWaveJSRemoveNode extends LitElement {
params: ZWaveJSRemoveNodeDialogParams params: ZWaveJSRemoveNodeDialogParams
): Promise<void> { ): Promise<void> {
this.entry_id = params.entry_id; this.entry_id = params.entry_id;
this._removedCallback = params.removedCallback;
if (params.skipConfirmation) {
this._startExclusion();
}
} }
protected render() { protected render() {
@ -67,7 +76,7 @@ class DialogZWaveJSRemoveNode extends LitElement {
)} )}
</mwc-button> </mwc-button>
` `
: ``} : nothing}
${this._status === "started" ${this._status === "started"
? html` ? html`
<div class="flex-container"> <div class="flex-container">
@ -93,7 +102,7 @@ class DialogZWaveJSRemoveNode extends LitElement {
)} )}
</mwc-button> </mwc-button>
` `
: ``} : nothing}
${this._status === "failed" ${this._status === "failed"
? html` ? html`
<div class="flex-container"> <div class="flex-container">
@ -107,13 +116,18 @@ class DialogZWaveJSRemoveNode extends LitElement {
"ui.panel.config.zwave_js.remove_node.exclusion_failed" "ui.panel.config.zwave_js.remove_node.exclusion_failed"
)} )}
</p> </p>
${this._error
? html`<ha-alert alert-type="error">
${this._error}
</ha-alert>`
: nothing}
</div> </div>
</div> </div>
<mwc-button slot="primaryAction" @click=${this.closeDialog}> <mwc-button slot="primaryAction" @click=${this.closeDialog}>
${this.hass.localize("ui.common.close")} ${this.hass.localize("ui.common.close")}
</mwc-button> </mwc-button>
` `
: ``} : nothing}
${this._status === "finished" ${this._status === "finished"
? html` ? html`
<div class="flex-container"> <div class="flex-container">
@ -134,7 +148,7 @@ class DialogZWaveJSRemoveNode extends LitElement {
${this.hass.localize("ui.common.close")} ${this.hass.localize("ui.common.close")}
</mwc-button> </mwc-button>
` `
: ``} : nothing}
</ha-dialog> </ha-dialog>
`; `;
} }
@ -143,13 +157,17 @@ class DialogZWaveJSRemoveNode extends LitElement {
if (!this.hass) { if (!this.hass) {
return; return;
} }
this._subscribed = this.hass.connection.subscribeMessage( this._subscribed = this.hass.connection
(message) => this._handleMessage(message), .subscribeMessage((message) => this._handleMessage(message), {
{
type: "zwave_js/remove_node", type: "zwave_js/remove_node",
entry_id: this.entry_id, entry_id: this.entry_id,
} })
); .catch((err) => {
this._status = "failed";
this._error = err.message;
return () => {};
});
this._status = "started";
this._removeNodeTimeoutHandle = window.setTimeout( this._removeNodeTimeoutHandle = window.setTimeout(
() => this._unsubscribe(), () => this._unsubscribe(),
120000 120000
@ -174,6 +192,9 @@ class DialogZWaveJSRemoveNode extends LitElement {
this._status = "finished"; this._status = "finished";
this._node = message.node; this._node = message.node;
this._unsubscribe(); this._unsubscribe();
if (this._removedCallback) {
this._removedCallback();
}
} }
} }

View File

@ -2,6 +2,8 @@ import { fireEvent } from "../../../../../common/dom/fire_event";
export interface ZWaveJSRemoveNodeDialogParams { export interface ZWaveJSRemoveNodeDialogParams {
entry_id: string; entry_id: string;
skipConfirmation?: boolean;
removedCallback?: () => void;
} }
export const loadRemoveNodeDialog = () => export const loadRemoveNodeDialog = () =>

View File

@ -36,8 +36,6 @@ import {
fetchZwaveProvisioningEntries, fetchZwaveProvisioningEntries,
InclusionState, InclusionState,
setZwaveDataCollectionPreference, setZwaveDataCollectionPreference,
stopZwaveExclusion,
stopZwaveInclusion,
subscribeZwaveControllerStatistics, subscribeZwaveControllerStatistics,
ZWaveJSClient, ZWaveJSClient,
ZWaveJSControllerStatisticsUpdatedMessage, ZWaveJSControllerStatisticsUpdatedMessage,
@ -81,9 +79,18 @@ class ZWaveJSConfigDashboard extends SubscribeMixin(LitElement) {
@state() @state()
private _statistics?: ZWaveJSControllerStatisticsUpdatedMessage; private _statistics?: ZWaveJSControllerStatisticsUpdatedMessage;
protected firstUpdated() { protected async firstUpdated() {
if (this.hass) { if (this.hass) {
this._fetchData(); await this._fetchData();
if (this._status === "connected") {
const inclusion_state = this._network?.controller.inclusion_state;
// show dialog if inclusion/exclusion is already in progress
if (inclusion_state === InclusionState.Including) {
this._addNodeClicked();
} else if (inclusion_state === InclusionState.Excluding) {
this._removeNodeClicked();
}
}
} }
} }
@ -126,31 +133,6 @@ class ZWaveJSConfigDashboard extends SubscribeMixin(LitElement) {
.path=${mdiRefresh} .path=${mdiRefresh}
.label=${this.hass!.localize("ui.common.refresh")} .label=${this.hass!.localize("ui.common.refresh")}
></ha-icon-button> ></ha-icon-button>
${this._network &&
this._status === "connected" &&
(this._network?.controller.inclusion_state ===
InclusionState.Including ||
this._network?.controller.inclusion_state ===
InclusionState.Excluding)
? html`
<ha-alert alert-type="info">
${this.hass.localize(
`ui.panel.config.zwave_js.common.in_progress_inclusion_exclusion`
)}
<mwc-button
slot="action"
.label=${this.hass.localize(
`ui.panel.config.zwave_js.common.cancel_inclusion_exclusion`
)}
@click=${this._network?.controller.inclusion_state ===
InclusionState.Including
? this._cancelInclusion
: this._cancelExclusion}
>
</mwc-button>
</ha-alert>
`
: ""}
${this._network ${this._network
? html` ? html`
<ha-card class="content network-status"> <ha-card class="content network-status">
@ -193,11 +175,11 @@ class ZWaveJSConfigDashboard extends SubscribeMixin(LitElement) {
`ui.panel.config.zwave_js.dashboard.not_ready`, `ui.panel.config.zwave_js.dashboard.not_ready`,
{ count: notReadyDevices } { count: notReadyDevices }
)})` )})`
: ""} : nothing}
</small> </small>
</div> </div>
` `
: ``} : nothing}
</div> </div>
</div> </div>
<div class="card-actions"> <div class="card-actions">
@ -224,7 +206,7 @@ class ZWaveJSConfigDashboard extends SubscribeMixin(LitElement) {
)} )}
</mwc-button></a </mwc-button></a
>` >`
: ""} : nothing}
</div> </div>
</ha-card> </ha-card>
<ha-card header="Diagnostics"> <ha-card header="Diagnostics">
@ -464,7 +446,7 @@ class ZWaveJSConfigDashboard extends SubscribeMixin(LitElement) {
</div> </div>
</ha-card> </ha-card>
` `
: ``} : nothing}
<ha-fab <ha-fab
slot="fab" slot="fab"
.label=${this.hass.localize( .label=${this.hass.localize(
@ -540,7 +522,7 @@ class ZWaveJSConfigDashboard extends SubscribeMixin(LitElement) {
</mwc-button> </mwc-button>
</div> </div>
` `
: ""}`; : nothing}`;
} }
private _handleBack(): void { private _handleBack(): void {
@ -593,6 +575,9 @@ class ZWaveJSConfigDashboard extends SubscribeMixin(LitElement) {
private async _removeNodeClicked() { private async _removeNodeClicked() {
showZWaveJSRemoveNodeDialog(this, { showZWaveJSRemoveNodeDialog(this, {
entry_id: this.configEntryId!, entry_id: this.configEntryId!,
skipConfirmation:
this._network?.controller.inclusion_state === InclusionState.Excluding,
removedCallback: () => this._fetchData(),
}); });
} }
@ -602,16 +587,6 @@ class ZWaveJSConfigDashboard extends SubscribeMixin(LitElement) {
}); });
} }
private async _cancelInclusion() {
stopZwaveInclusion(this.hass!, this.configEntryId!);
await this._fetchData();
}
private async _cancelExclusion() {
stopZwaveExclusion(this.hass!, this.configEntryId!);
await this._fetchData();
}
private _dataCollectionToggled(ev) { private _dataCollectionToggled(ev) {
setZwaveDataCollectionPreference( setZwaveDataCollectionPreference(
this.hass!, this.hass!,