Implement auto MQTT discovery in Node

This commit is contained in:
tsightler
2022-04-25 16:40:33 -04:00
parent 474f70c937
commit 83372c07a9
2 changed files with 51 additions and 12 deletions

View File

@@ -48,10 +48,10 @@ if [ "${RUNMODE}" = "addon" ]; then
# Export MQTT service discovery data for use within NodeJS process
if bashio::services.available 'mqtt'; then
export DISCMQTTHOST=$(bashio::services mqtt "host")
export DISCMQTTPORT=$(bashio::services mqtt "port")
export DISCMQTTUSER=$(bashio::services mqtt "username")
export DISCMQTTPASS=$(bashio::services mqtt "password")
export HAMQTTHOST=$(bashio::services mqtt "host")
export HAMQTTPORT=$(bashio::services mqtt "port")
export HAMQTTUSER=$(bashio::services mqtt "username")
export HAMQTTPASS=$(bashio::services mqtt "password")
fi
fi
echo "-------------------------------------------------------"

View File

@@ -123,18 +123,57 @@ class Config {
}
doAutoConfig() {
// Only required for legacy configuration when used with BRANCH feature, can be removed after that point
// Only required for legacy configuration when used with BRANCH feature
if (!this.data.mqtt_url) {
this.data.mqtt_user = this.data.mqtt_user === '<auto_detect>' ? 'auto_user' : this.data.mqtt_user
this.data.mqtt_pass = this.data.mqtt_pass === '<auto_detect>' ? 'auto_password' : this.data.mqtt_password
this.data.mqtt_host = this.data.mqtt_host === '<auto_detect>' ? 'auto_hostname' : this.data.mqtt_host
this.data.mqtt_user = this.data.mqtt_user === '<auto_detect>' ? '1883' : this.data.mqtt_user
this.data.mqtt_url = `mqtt://${this.data.mqtt_user}:${this.data.mqtt_password}@${this.data.mqtt_host}:${this.data.mqtt_port}`
this.data.mqtt_user = this.data.mqtt_user === '<auto_detect>' ? 'AUTO_USER' : this.data.mqtt_user
this.data.mqtt_pass = this.data.mqtt_pass === '<auto_detect>' ? 'AUTO_PASSWORD' : this.data.mqtt_password
this.data.mqtt_host = this.data.mqtt_host === '<auto_detect>' ? 'AUTO_HOSTNAME' : this.data.mqtt_host
this.data.mqtt_port = this.data.mqtt_port === '<auto_detect>' ? '1883' : this.data.mqtt_port
this.data.mqtt_url = `${this.data.mqtt_port === 8883 ? 'mqtts' : 'mqtt'}://${this.data.mqtt_user}:${this.data.mqtt_password}@${this.data.mqtt_host}:${this.data.mqtt_port}`
}
try {
console.log(mqttURL)
const mqttURL = new URL(this.data.mqtt_url)
console.log(mqttURL)
if (mqttURL.hostname === "AUTO_HOSTNAME") {
if (mqttURL.protocol === 'mqtt:') {
if (process.env.HAMQTTHOST) {
mqttURL.hostname = process.env.HAMQTTHOST
if (mqttURL.hostname === 'localhost' || mqttURL.hostname === '127.0.0.1') {
debug(`Discovered invalid value for MQTT host: ${mqttURL.hostname}`)
debug('Overriding with default alias for Mosquitto MQTT addon')
mqttURL.hostname = 'core-mosquitto'
}
} else {
debug('No Home Assistant MQTT service found, using Home Assistant hostname as default')
mqttURL.hostname = process.env.HAHOSTNAME
}
} else if (mqttURL.protocol === 'mqtts:') {
mqttURL.hostname = process.env.HAHOSTNAME
}
debug(`Discovered MQTT Host: ${mqttURL.hostname}`)
}
if (!mqttURL.port) {
mqttURL.port = mqttURL.protocol === 'mqtts:' ? '8883' : '1883'
debug(`Discovered MQTT Port: ${mqttURL.port}`)
}
if (mqttURL.username === 'AUTO_USER') {
mqttURL.username = process.env.HAMQTTUSER ? process.env.HAMQTTUSER : ''
if (mqttURL.username) {
debug(`Discovered MQTT User: ${mqttURL.username}`)
}
}
if (mqttURL.username && mqttURL.password === "AUTO_PASSWORD") {
mqttURL.password = process.env.HAMQTTPASS ? process.env.HAMQTTPASS : ''
if (mqttURL.password) {
debug('Discovered MQTT password: <hidden>')
}
}
this.data.mqtt_url = mqttURL.href
} catch (err) {
debug('Failed to parse MQTT URL, please check that configuration is valid')
}