Skip to content

Commit

Permalink
fix: payload parse of rgb dimmers #488 (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando authored May 26, 2020
1 parent 769eac2 commit d8cca1a
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,16 @@ function copy (obj) {
return JSON.parse(JSON.stringify(obj))
}

/**
* Checks if an object_id is a rgb_dimmer
*
* @param {String} id object id of the hass discovery payload
* @returns true if the discovery payload object id is a rgb_dimmer
*/
function isRgbDimmer (id) {
return id.startsWith('rgb_dimmer')
}

/**
* Get the device Object to send in discovery payload
*
Expand Down Expand Up @@ -477,12 +487,14 @@ Gateway.prototype.parsePayload = function (payload, valueId, valueConf) {
// Hass payload parsing
if (this.discovered[valueId.value_id]) {
// parse payload for switches
if ((valueId.type === 'bool' || this.discovered[valueId.value_id].object_id === 'rgb_dimmer') && typeof payload === 'string') {
const isDimmer = isRgbDimmer(this.discovered[valueId.value_id].object_id)

if ((valueId.type === 'bool' || isDimmer) && typeof payload === 'string') {
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
}

if (this.discovered[valueId.value_id].object_id === 'rgb_dimmer') {
if (isDimmer) {
if (typeof payload === 'boolean') payload = payload ? 99 : 0
else payload = Math.round(payload / 255 * 99)
}
Expand Down

0 comments on commit d8cca1a

Please sign in to comment.