Skip to content

Commit

Permalink
feat(discovery): add support for availability topics (#3510)
Browse files Browse the repository at this point in the history
Co-authored-by: Z-Wave JS Bot <[email protected]>
  • Loading branch information
ccutrer and zwave-js-bot authored Jan 10, 2024
1 parent 7c53a94 commit e7ce406
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
38 changes: 34 additions & 4 deletions api/lib/Gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,8 @@ export default class Gateway {
// Set device information using node info
payload.device = this._deviceInfo(node, nodeName)

this.setDiscoveryAvailability(node, payload)

hassDevice.object_id = utils
.sanitizeTopic(hassDevice.object_id, true)
.toLocaleLowerCase()
Expand Down Expand Up @@ -1671,10 +1673,8 @@ export default class Gateway {
payload.command_topic = setTopic || getTopic + '/set'
}

// Set availability topic using node status topic
// payload.availability_topic = this.mqtt.getTopic(this.nodeTopic(node)) + '/status/hass'
// payload.payload_available = true
// payload.payload_not_available = false
this.setDiscoveryAvailability(node, payload)

if (
['binary_sensor', 'sensor', 'lock', 'climate', 'fan'].includes(
cfg.type,
Expand Down Expand Up @@ -2370,6 +2370,36 @@ export default class Gateway {
}
}

private setDiscoveryAvailability(
node: ZUINode,
payload: { [key: string]: any },
) {
// Set availability config using node status topic, client status topic
// (which is the LWT), and driver status topic
payload.availability = [
{
payload_available: 'true',
payload_not_available: 'false',
topic: this.mqtt.getTopic(this.nodeTopic(node)) + '/status',
},
{
topic: this.mqtt.getStatusTopic(),
value_template:
"{{'online' if value_json.value else 'offline'}}",
},
{
payload_available: 'true',
payload_not_available: 'false',
topic: this.mqtt.getTopic('driver/status'),
},
]
if (this.config.payloadType !== PAYLOAD_TYPE.RAW) {
payload.availability[0].value_template =
"{{'true' if value_json.value else 'false'}}"
}
payload.availability_mode = 'all'
}

/**
* Get the Hass discovery topic for the specific node and hassDevice
*/
Expand Down
9 changes: 8 additions & 1 deletion api/lib/MqttClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ class MqttClient extends TypedEventEmitter<MqttClientEventCallbacks> {
return `${this.config.prefix}/${MqttClient.CLIENTS_PREFIX}/${this._clientID}/${suffix}`
}

/**
* Returns the topic used to report client status
*/
getStatusTopic() {
return this.getClientTopic(MqttClient.STATUS_TOPIC)
}

/**
* Method used to close clients connection, use this before destroy
*/
Expand Down Expand Up @@ -352,7 +359,7 @@ class MqttClient extends TypedEventEmitter<MqttClientEventCallbacks> {
clean: config.clean,
rejectUnauthorized: !config.allowSelfsigned,
will: {
topic: this.getClientTopic(MqttClient.STATUS_TOPIC),
topic: this.getStatusTopic(),
payload: JSON.stringify({ value: false }) as any,
qos: this.config.qos,
retain: this.config.retain,
Expand Down

0 comments on commit e7ce406

Please sign in to comment.