mirror of
https://github.com/tsightler/ring-mqtt.git
synced 2025-09-26 21:01:12 +08:00
Implement auto MQTT discovery in Node
This commit is contained in:
@@ -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 "-------------------------------------------------------"
|
||||
|
@@ -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')
|
||||
}
|
||||
|
Reference in New Issue
Block a user