Skip to content

Commit

Permalink
Merge branch 'master' into zwave-js-server
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando committed Jan 18, 2021
2 parents 271d78e + 43fdce0 commit 04b3aad
Show file tree
Hide file tree
Showing 10 changed files with 38,648 additions and 91 deletions.
9 changes: 4 additions & 5 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ let restarting = false

// ### UTILS

function hasProperty (obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop)
}

function start (server) {
setupSocket(server)
setupInterceptor()
Expand Down Expand Up @@ -244,7 +240,10 @@ app.post('/api/importConfig', async function (req, res) {
else {
for (let i = 0; i < config.length; i++) {
const e = config[i]
if (e && (!hasProperty(e, 'name') || !hasProperty(e, 'loc'))) {
if (
e &&
(!utils.hasProperty(e, 'name') || !utils.hasProperty(e, 'loc'))
) {
continue
} else if (e) {
await gw.zwave.callApi('_setNodeName', i, e.name || '')
Expand Down
4 changes: 2 additions & 2 deletions hass/configurations.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ module.exports = {
rgb_command_topic: true,
rgb_state_topic: true,
rgb_value_template:
'{{ value_json.value[1:3] | int(0, 16) }},{{ value_json.value[3:5] | int(0, 16) }},{{ value_json.value[5:7] | int(0, 16) }}'
'{{ value_json.value[0:2] | int(0, 16) }},{{ value_json.value[2:4] | int(0, 16) }},{{ value_json.value[4:6] | int(0, 16) }}'
}
},
light_rgb_dimmer: {
Expand All @@ -180,7 +180,7 @@ module.exports = {
rgb_command_topic: true,
rgb_state_topic: true,
rgb_value_template:
'{{ value_json.value[1:3] | int(0, 16) }},{{ value_json.value[3:5] | int(0, 16) }},{{ value_json.value[5:7] | int(0, 16) }}'
'{{ value_json.value[0:2] | int(0, 16) }},{{ value_json.value[2:4] | int(0, 16) }},{{ value_json.value[4:6] | int(0, 16) }}'
}
},
light_dimmer: {
Expand Down
85 changes: 48 additions & 37 deletions lib/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,17 @@ function onHassStatus (online) {
async function onApiRequest (topic, apiName, payload) {
if (this.zwave) {
const args = payload.args || []
const result = await this.zwave.callApi(apiName, ...args)

let result

if (Array.isArray(args)) {
result = await this.zwave.callApi(apiName, ...args)
} else {
result = {
success: false,
message: 'Args must be an array'
}
}
this.mqtt.publish(topic, result)
} else {
logger.error(`Requested Zwave api ${apiName} doesn't exist`)
Expand Down Expand Up @@ -755,7 +765,7 @@ Gateway.prototype.parsePayload = function (payload, valueId, valueConf) {
) {
const rgb = payload.split(',')
if (rgb.length === 3) {
payload = '#' + rgbToHex(rgb[0]) + rgbToHex(rgb[1]) + rgbToHex(rgb[2])
payload = rgbToHex(rgb[0]) + rgbToHex(rgb[1]) + rgbToHex(rgb[2])
}
}
}
Expand Down Expand Up @@ -1437,17 +1447,19 @@ Gateway.prototype.discoverValue = function (node, vId) {
case CommandClasses['All Switch']:
case CommandClasses['Binary Toggle Switch']:
if (valueId.isCurrentValue) {
// TODO: Needs https://github.com/zwave-js/node-zwave-js/issues/806
// const rgb = node.values['51-1-0']
// if (rgb) {
// cfg = copy(hassCfg.light_rgb_switch)
// cfg.discovery_payload.rgb_state_topic = this.mqtt.getTopic(
// this.valueTopic(node, rgb)
// )
// cfg.discovery_payload.rgb_command_topic =
// cfg.discovery_payload.rgb_state_topic + '/set'
// } else {
cfg = copy(hassCfg.switch)
const rgbID = `51-${valueId.endpoint || 0}-hexColor`
const rgb = node.values[rgbID]
if (rgb) {
cfg = copy(hassCfg.light_rgb_switch)
cfg.discovery_payload.rgb_state_topic = this.mqtt.getTopic(
this.valueTopic(node, rgb)
)
cfg.discovery_payload.rgb_command_topic =
cfg.discovery_payload.rgb_state_topic + '/set'
cfg.values = [rgbID]
} else {
cfg = copy(hassCfg.switch)
}
} else return
break
case CommandClasses['Barrier Operator']:
Expand Down Expand Up @@ -1485,23 +1497,20 @@ Gateway.prototype.discoverValue = function (node, vId) {
cfg.discovery_payload.payload_close = 0
} else {
// ... otherwise use a light dimmer configuration
// TODO: Needs https://github.com/zwave-js/node-zwave-js/issues/806
// brightness level
// const rgb = node.values['51-1-0']
// if (rgb) {
// cfg = copy(hassCfg.light_rgb_dimmer)
// cfg.discovery_payload.rgb_state_topic = this.mqtt.getTopic(
// this.valueTopic(node, rgb)
// )
// cfg.discovery_payload.rgb_command_topic = this.mqtt.getTopic(result.targetTopic) + '/set'
// cfg.discovery_payload.brightness_state_topic = this.mqtt.getTopic(
// topic
// )
// cfg.discovery_payload.brightness_command_topic =
// cfg.discovery_payload.brightness_state_topic + '/set'
// } else {
cfg = copy(hassCfg.light_dimmer)
// }
const rgbID = `51-${valueId.endpoint || 0}-hexColor`
const rgb = node.values[rgbID]
if (rgb) {
cfg = copy(hassCfg.light_rgb_dimmer)
const rgbTopic = this.mqtt.getTopic(this.valueTopic(node, rgb))
cfg.discovery_payload.rgb_state_topic = rgbTopic
cfg.discovery_payload.rgb_command_topic = rgbTopic + '/set'
cfg.discovery_payload.brightness_state_topic = getTopic
cfg.discovery_payload.brightness_command_topic = setTopic
cfg.values = [rgbID]
} else {
cfg = copy(hassCfg.light_dimmer)
}
}
} else return
break
Expand Down Expand Up @@ -1716,13 +1725,13 @@ Gateway.prototype.discoverValue = function (node, vId) {
if (valueConf.icon) cfg.discovery_payload.icon = valueConf.icon
}
break
// case 'color':
// if (valueId.isCurrentValue) {
// cfg = copy(hassCfg.light_rgb)
// cfg.discovery_payload.rgb_state_topic = this.mqtt.getTopic(topic)
// cfg.discovery_payload.rgb_command_topic = cfg.discovery_payload.rgb_state_topic + '/set'
// } else return
// break
case 'color':
if (valueId.property === 'hexColor') {
cfg = copy(hassCfg.light_rgb_switch)
cfg.discovery_payload.rgb_state_topic = this.mqtt.getTopic(getTopic)
cfg.discovery_payload.rgb_command_topic = getTopic + '/set'
} else return
break
default:
return
}
Expand Down Expand Up @@ -1781,7 +1790,9 @@ Gateway.prototype.discoverValue = function (node, vId) {
const discoveryTopic = getDiscoveryTopic(cfg, nodeName)

cfg.discoveryTopic = discoveryTopic
cfg.values = [vId]
cfg.values = cfg.values || []

cfg.values.push(vId)

if (valueId.targetValue) {
cfg.values.push(valueId.targetValue)
Expand Down
Loading

0 comments on commit 04b3aad

Please sign in to comment.