Skip to content

Commit

Permalink
fix: better payload parsing (#861)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando authored Mar 10, 2021
1 parent e913db5 commit 199c558
Showing 1 changed file with 21 additions and 29 deletions.
50 changes: 21 additions & 29 deletions lib/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -903,23 +903,31 @@ Gateway.prototype.parsePayload = function (payload, valueId, valueConf) {
? payload.value
: payload

// try to parse string to bools
if (typeof payload === 'string' && isNaN(payload)) {
if (/\btrue\b|\bon\b|\block\b/gi.test(payload)) payload = true
else if (/\bfalse\b|\boff\b|\bunlock\b/gi.test(payload)) {
payload = false
}
}

// on/off becomes 100%/0%
if (typeof payload === 'boolean' && valueId.type === 'number') {
payload = payload ? valueId.max : valueId.min
}

if (valueId.commandClass === CommandClasses['Binary Toggle Switch']) {
payload = 1
} else if (
valueId.commandClass === CommandClasses['Multilevel Toggle Switch']
) {
payload = valueId.value > 0 ? 0 : 0xff
}

const hassDevice = this.discovered[valueId.id]

// Hass payload parsing
if (hassDevice) {
// try to parse string payloads in bools
if (typeof payload === 'string' && isNaN(payload)) {
if (/\btrue\b|\bon\b|\block\b/gi.test(payload)) payload = true
else if (/\bfalse\b|\boff\b|\bunlock\b/gi.test(payload)) {
payload = false
}
}

// on/off becomes 100%/0%
if (typeof payload === 'boolean' && valueId.type === 'number') {
payload = payload ? valueId.max : valueId.min
}

// map modes coming from hass
if (valueId.list && isNaN(payload)) {
// for thermostat_fan_mode command class use the fan_mode_map
Expand All @@ -936,22 +944,6 @@ Gateway.prototype.parsePayload = function (payload, valueId, valueConf) {
payload = hassDevice.mode_map[payload]
}
}

if (valueId.commandClass === CommandClasses['Binary Toggle Switch']) {
payload = 1
} else if (
valueId.commandClass === CommandClasses['Multilevel Toggle Switch']
) {
payload = valueId.value > 0 ? 0 : 0xff
}
}

if (valueId.type === 'any') {
if (payload.type === 'Buffer' && payload.data) {
payload = Buffer.from(payload.data)
} else {
payload = Buffer.from(payload)
}
}

if (valueConf) {
Expand Down

0 comments on commit 199c558

Please sign in to comment.