From 07c2aaa808e12774b70f677b0d323aebe934cca1 Mon Sep 17 00:00:00 2001 From: Petar Petrov Date: Mon, 17 Feb 2025 16:14:43 +0200 Subject: [PATCH] Z-WaveJS: stop inclusion subscription while dialog is open and handling it --- .../zwave_js/zwave_js-config-dashboard.ts | 63 ++++++++++++------- 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/src/panels/config/integrations/integration-panels/zwave_js/zwave_js-config-dashboard.ts b/src/panels/config/integrations/integration-panels/zwave_js/zwave_js-config-dashboard.ts index 2ddef810e7..59d3c84892 100644 --- a/src/panels/config/integrations/integration-panels/zwave_js/zwave_js-config-dashboard.ts +++ b/src/panels/config/integrations/integration-panels/zwave_js/zwave_js-config-dashboard.ts @@ -78,6 +78,8 @@ class ZWaveJSConfigDashboard extends SubscribeMixin(LitElement) { private _dialogOpen = false; + private _s2InclusionUnsubscribe?: UnsubscribeFunc; + protected async firstUpdated() { if (this.hass) { await this._fetchData(); @@ -105,19 +107,7 @@ class ZWaveJSConfigDashboard extends SubscribeMixin(LitElement) { this._statistics = message; } ), - subscribeS2Inclusion(this.hass, this.configEntryId, (message) => { - if (!this._dialogOpen) { - showZWaveJSAddNodeDialog(this, { - entry_id: this.configEntryId, - dsk: message.dsk, - onStop: () => { - setTimeout(() => this._fetchData(), 100); - this._dialogOpen = false; - }, - }); - this._dialogOpen = true; - } - }), + this._subscribeS2Inclusion(), ]; } @@ -578,17 +568,7 @@ class ZWaveJSConfigDashboard extends SubscribeMixin(LitElement) { } private async _addNodeClicked() { - if (!this._dialogOpen) { - showZWaveJSAddNodeDialog(this, { - entry_id: this.configEntryId!, - // refresh the data after the dialog is closed. add a small delay for the inclusion state to update - onStop: () => { - setTimeout(() => this._fetchData(), 100); - this._dialogOpen = false; - }, - }); - this._dialogOpen = true; - } + this._openInclusionDialog(); } private async _removeNodeClicked() { @@ -627,6 +607,41 @@ class ZWaveJSConfigDashboard extends SubscribeMixin(LitElement) { showOptionsFlowDialog(this, configEntry!); } + private _openInclusionDialog(dsk?: string) { + if (!this._dialogOpen) { + // Unsubscribe from S2 inclusion before opening dialog + if (this._s2InclusionUnsubscribe) { + this._s2InclusionUnsubscribe(); + this._s2InclusionUnsubscribe = undefined; + } + + showZWaveJSAddNodeDialog(this, { + entry_id: this.configEntryId!, + dsk, + onStop: this._handleInclusionDialogClosed, + }); + this._dialogOpen = true; + } + } + + private _handleInclusionDialogClosed = () => { + // refresh the data after the dialog is closed. add a small delay for the inclusion state to update + setTimeout(() => this._fetchData(), 100); + this._dialogOpen = false; + this._subscribeS2Inclusion(); + }; + + private async _subscribeS2Inclusion() { + this._s2InclusionUnsubscribe = await subscribeS2Inclusion( + this.hass, + this.configEntryId, + (message) => { + this._openInclusionDialog(message.dsk); + } + ); + return this._s2InclusionUnsubscribe; + } + static get styles(): CSSResultGroup { return [ haStyle,