Skip to content

Commit

Permalink
extract availability config to separate method
Browse files Browse the repository at this point in the history
so it can also be configured on custom devices
  • Loading branch information
ccutrer committed Jan 9, 2024
1 parent 45bb903 commit 1edb6ab
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 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,30 +1673,7 @@ export default class Gateway {
payload.command_topic = setTopic || getTopic + '/set'
}

// 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'
this._availabilityConfig(node, payload)

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

private _availabilityConfig(
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

0 comments on commit 1edb6ab

Please sign in to comment.