From e7ce406bbb2c742b2bc6af0d6c70bfc0a7b548d3 Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Wed, 10 Jan 2024 00:53:44 -0700 Subject: [PATCH] feat(discovery): add support for availability topics (#3510) Co-authored-by: Z-Wave JS Bot --- api/lib/Gateway.ts | 38 ++++++++++++++++++++++++++++++++++---- api/lib/MqttClient.ts | 9 ++++++++- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/api/lib/Gateway.ts b/api/lib/Gateway.ts index 84aa190130..637d80da74 100644 --- a/api/lib/Gateway.ts +++ b/api/lib/Gateway.ts @@ -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() @@ -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, @@ -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 */ diff --git a/api/lib/MqttClient.ts b/api/lib/MqttClient.ts index 43d7944e8d..17c02efd47 100644 --- a/api/lib/MqttClient.ts +++ b/api/lib/MqttClient.ts @@ -115,6 +115,13 @@ class MqttClient extends TypedEventEmitter { 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 */ @@ -352,7 +359,7 @@ class MqttClient extends TypedEventEmitter { 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,