Skip to content

Commit

Permalink
fix: user callbacks not working with MQTT
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando committed Nov 14, 2023
1 parent f956deb commit 870e414
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions lib/ZwaveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ import {
ZWavePlusRoleType,
FirmwareUpdateInfo,
PartialZWaveOptions,
InclusionUserCallbacks,
} from 'zwave-js'
import { getEnumMemberName, parseQRCodeString } from 'zwave-js/Utils'
import { logsDir, nvmBackupsDir, storeDir } from '../config/app'
Expand Down Expand Up @@ -698,6 +699,12 @@ class ZwaveClient extends TypedEventEmitter<ZwaveClientEventCallbacks> {
{ lastUpdate: number; fn: () => void; timeout: NodeJS.Timeout }
> = new Map()

private inclusionUserCallbacks: InclusionUserCallbacks = {
grantSecurityClasses: this._onGrantSecurityClasses.bind(this),
validateDSKAndEnterPIN: this._onValidateDSK.bind(this),
abort: this._onAbortInclusion.bind(this),
}

public get driverReady() {
return this.driver && this._driverReady && !this.closed
}
Expand Down Expand Up @@ -930,24 +937,22 @@ class ZwaveClient extends TypedEventEmitter<ZwaveClientEventCallbacks> {

setUserCallbacks() {
this.hasUserCallbacks = true
if (!this._driver) {
if (!this._driver || !this.cfg.serverEnabled) {
return
}

logger.info('Setting user callbacks')

this.driver.updateOptions({
inclusionUserCallbacks: {
grantSecurityClasses: this._onGrantSecurityClasses.bind(this),
validateDSKAndEnterPIN: this._onValidateDSK.bind(this),
abort: this._onAbortInclusion.bind(this),
...this.inclusionUserCallbacks,
},
})
}

removeUserCallbacks() {
this.hasUserCallbacks = false
if (!this._driver) {
if (!this._driver || !this.cfg.serverEnabled) {
return
}

Expand Down Expand Up @@ -2161,6 +2166,14 @@ class ZwaveClient extends TypedEventEmitter<ZwaveClientEventCallbacks> {
zwaveOptions.features.softReset = this.cfg.enableSoftReset
}

// when server is not enabled, disable the user callbacks set/remove
// so it can be used through MQTT
if (!this.cfg.serverEnabled) {
zwaveOptions.inclusionUserCallbacks = {
...this.inclusionUserCallbacks,
}
}

if (this.cfg.scales) {
const scales: Record<string | number, string | number> = {}
for (const s of this.cfg.scales) {
Expand Down Expand Up @@ -2247,10 +2260,7 @@ class ZwaveClient extends TypedEventEmitter<ZwaveClientEventCallbacks> {
logger.info(`Connecting to ${this.cfg.port}`)

// setup user callbacks only if there are connected clients
this.hasUserCallbacks =
(await this.socket.fetchSockets()).length > 0

if (this.hasUserCallbacks) {
if ((await this.socket.fetchSockets()).length > 0) {
this.setUserCallbacks()
}

Expand Down

0 comments on commit 870e414

Please sign in to comment.