Skip to content

Commit

Permalink
fix: gateway values not working (#317)
Browse files Browse the repository at this point in the history
Fixes #312
  • Loading branch information
robertsLando authored Jan 22, 2021
1 parent 6265459 commit df4a288
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
15 changes: 12 additions & 3 deletions lib/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ function onValueChanged (valueId, node, changed) {

// check if this value isn't discovered yet (values added after node is ready)
if (this.config.hassDiscovery && !isDiscovered) {
this.discoverValue(node, valueId.id.replace(valueId.nodeId + '-', ''))
this.discoverValue(node, getIdWithoutNode(valueId))
}

const result = this.valueTopic(node, valueId, true)
Expand Down Expand Up @@ -541,6 +541,16 @@ function copy (obj) {
return JSON.parse(JSON.stringify(obj))
}

/**
* Returns the value id without the node prefix
*
* @param {*} valueId
* @returns {string} The valueId string
*/
function getIdWithoutNode (valueId) {
return valueId.id.replace(valueId.nodeId + '-', '')
}

/**
* Get the device Object to send in discovery payload
*
Expand Down Expand Up @@ -878,11 +888,10 @@ Gateway.prototype.valueTopic = function (node, valueId, returnObject = false) {
const topic = []
let valueConf

const vID = valueId.id

// check if this value is in configuration values array
const values = this.config.values.filter(v => v.device === node.deviceId)
if (values && values.length > 0) {
const vID = getIdWithoutNode(valueId)
valueConf = values.find(v => v.value.id === vID)
}

Expand Down
21 changes: 11 additions & 10 deletions lib/ZwaveClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ function updateValueMetadata (zwaveNode, zwaveValue, zwaveValueMeta) {
zwaveValue.nodeId = zwaveNode.id

const valueId = {
id: getValueID(zwaveValue, true),
id: getValueID(zwaveValue, true), // the valueId unique in the entire network, it also has the nodeId
nodeId: zwaveNode.id,
commandClass: zwaveValue.commandClass,
commandClassName: zwaveValue.commandClassName,
Expand Down Expand Up @@ -931,7 +931,7 @@ function parseValue (zwaveNode, zwaveValue, zwaveValueMeta) {
valueId.isCurrentValue = true
const targetValue = findTargetValue(valueId, zwaveNode.getDefinedValueIDs())
if (targetValue) {
valueId.targetValue = getValueID(targetValue, false)
valueId.targetValue = getValueID(targetValue)
}
}

Expand Down Expand Up @@ -989,13 +989,13 @@ function updateValue (zwaveNode, args) {
*/
function removeValue (zwaveNode, args) {
const node = this.nodes[zwaveNode.id]
const idString = getValueID(args)
const toRemove = node ? node.values[idString] : null
const vID = getValueID(args)
const toRemove = node ? node.values[vID] : null

if (toRemove) {
delete node.values[idString]
delete node.values[vID]
this.sendToSocket(socketEvents.valueRemoved, toRemove)
logger.info(`ValueRemoved: ${idString} from node ${zwaveNode.id}`)
logger.info(`ValueRemoved: ${vID} from node ${zwaveNode.id}`)
} else {
logger.info(`ValueRemoved: no such node: ${zwaveNode.id} error`)
}
Expand Down Expand Up @@ -1058,7 +1058,7 @@ function findTargetValue (zwaveValue, definedValueIds) {
* Get a valueId from a valueId object
*
* @param {any} v The internal value id
* @param {boolean} nodeId Add node identifier
* @param {boolean} withNode Add node identifier
* @returns The value id unique identifier
*/
function getValueID (v, withNode) {
Expand Down Expand Up @@ -2213,7 +2213,8 @@ ZwaveClient.prototype.callApi = async function (apiName, ...args) {
*/
ZwaveClient.prototype.writeValue = async function (valueId, value) {
if (this.driverReady) {
logger.log('info', `Writing %o to ${getValueID(valueId)}`, value)
const vID = getValueID(valueId, true)
logger.log('info', `Writing %o to ${vID}`, value)

let result = false

Expand Down Expand Up @@ -2258,13 +2259,13 @@ ZwaveClient.prototype.writeValue = async function (valueId, value) {
} catch (error) {
logger.log(
'error',
`Error while writing %o on ${getValueID(valueId)}: ${error.message}`,
`Error while writing %o on ${vID}: ${error.message}`,
value
)
}
// https://zwave-js.github.io/node-zwave-js/#/api/node?id=setvalue
if (result === false) {
logger.log('error', `Unable to write %o on ${getValueID(valueId)}`, value)
logger.log('error', `Unable to write %o on ${vID}`, value)
}
}
}
Expand Down

0 comments on commit df4a288

Please sign in to comment.