Skip to content

Commit

Permalink
fix(mqtt): possible write fails when publishing to /set (#2720)
Browse files Browse the repository at this point in the history
* fix(mqtt): possible write fails when publishing to `/set`

* fix: make it null safe
  • Loading branch information
robertsLando committed Oct 11, 2022
1 parent 3ff6ccd commit 03fa9ee
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions lib/ZwaveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3324,11 +3324,27 @@ class ZwaveClient extends TypedEventEmitter<ZwaveClientEventCallbacks> {
this._dumpNode(zwaveNode)

const values = zwaveNode.getDefinedValueIDs()
const delayedUpdates = []

for (const zwaveValue of values) {
this._addValue(zwaveNode, zwaveValue, existingValues)
const res = this._addValue(
zwaveNode,
zwaveValue,
existingValues,
true
)

if (res?.updated) {
delayedUpdates.push(
this.emitValueChanged.bind(this, res.valueId, node, true)
)
}
}

// emit value updated events when all values are added
// this prevents to have undefined target values when using mqtt
delayedUpdates.forEach((fn) => fn())

// add it to know devices types (if not already present)
if (!this._devices[node.deviceId]) {
this._devices[node.deviceId] = {
Expand Down Expand Up @@ -4232,7 +4248,8 @@ class ZwaveClient extends TypedEventEmitter<ZwaveClientEventCallbacks> {
zwaveValue: TranslatedValueID,
oldValues?: {
[key: string]: ZUIValueId
}
},
skipUpdate = false
) {
const node = this._nodes.get(zwaveNode.id)

Expand All @@ -4259,10 +4276,17 @@ class ZwaveClient extends TypedEventEmitter<ZwaveClientEventCallbacks> {
`Node ${zwaveNode.id}: value added ${valueId.id} => ${valueId.value}`
)

if (updated) {
if (!skipUpdate && updated) {
this.emitValueChanged(valueId, node, true)
}

return {
updated,
valueId,
}
}

return null
}

/**
Expand Down

0 comments on commit 03fa9ee

Please sign in to comment.