diff --git a/api/lib/ZwaveClient.ts b/api/lib/ZwaveClient.ts index 5a56952c74d..65b42fd0186 100644 --- a/api/lib/ZwaveClient.ts +++ b/api/lib/ZwaveClient.ts @@ -729,8 +729,6 @@ class ZwaveClient extends TypedEventEmitter { validateDSKAndEnterPIN: this._onValidateDSK.bind(this), abort: this._onAbortInclusion.bind(this), } - private _inclusionStateInterval: NodeJS.Timeout - private _inclusionState: InclusionState = undefined private _controllerListenersAdded: boolean = false @@ -1135,11 +1133,6 @@ class ZwaveClient extends TypedEventEmitter { this.closed = true this.driverReady = false - if (this._inclusionStateInterval) { - clearInterval(this._inclusionStateInterval) - this._inclusionStateInterval = null - } - if (this.commandsTimeout) { clearTimeout(this.commandsTimeout) this.commandsTimeout = null @@ -2387,6 +2380,15 @@ class ZwaveClient extends TypedEventEmitter { } } + private async sendInitToSockets() { + const sockets = await this.socket.fetchSockets() + + for (const socket of sockets) { + // force send init to all connected sockets + socket.emit(socketEvents.init, this.getState()) + } + } + public emitValueChanged( valueId: ZUIValueId, node: ZUINode, @@ -4293,26 +4295,6 @@ class ZwaveClient extends TypedEventEmitter { this._updateControllerStatus('Driver ready') - if (!this._inclusionStateInterval) { - this._inclusionStateInterval = setInterval(() => { - if (!this.driverReady) return - - if ( - this._driver.controller.inclusionState !== - this._inclusionState - ) { - this._inclusionState = - this._driver.controller.inclusionState - - this.sendToSocket(socketEvents.controller, { - status: this._cntStatus, - error: this._error, - inclusionState: this._inclusionState, - }) - } - }, 2000) - } - try { // this must be done only after driver is ready this._scheduledConfigCheck().catch(() => { @@ -4338,6 +4320,10 @@ class ZwaveClient extends TypedEventEmitter { 'exclusion stopped', this._onExclusionStopped.bind(this), ) + .on( + 'inclusion state changed', + this._onInclusionStateChanged.bind(this), + ) .on('inclusion failed', this._onInclusionFailed.bind(this)) .on('exclusion failed', this._onExclusionFailed.bind(this)) .on('node found', this._onNodeFound.bind(this)) @@ -4417,12 +4403,7 @@ class ZwaveClient extends TypedEventEmitter { logger.info(`Scanning network with homeid: ${homeHex}`) - const sockets = await this.socket.fetchSockets() - - for (const socket of sockets) { - // force send init to all connected sockets - socket.emit(socketEvents.init, this.getState()) - } + await this.sendInitToSockets() this.loadFakeNodes().catch((e) => { logger.error(`Error while loading fake nodes: ${e.message}`) @@ -4657,6 +4638,18 @@ class ZwaveClient extends TypedEventEmitter { this.emit('event', EventSource.CONTROLLER, 'exclusion stopped') } + private _onInclusionStateChanged(state: InclusionState) { + if (state !== this._inclusionState) { + this._inclusionState = state + + this.sendToSocket(socketEvents.controller, { + status: this._cntStatus, + error: this._error, + inclusionState: this._inclusionState, + }) + } + } + private _onInclusionFailed() { const message = 'Inclusion failed' this.isReplacing = false