Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(discovery): add support for availability topics #3510

Merged
merged 4 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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._availabilityConfig(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._availabilityConfig(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 _availabilityConfig(
ccutrer marked this conversation as resolved.
Show resolved Hide resolved
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
Loading