Migrate auto configuration to Node

This commit is contained in:
tsightler
2022-04-25 13:51:25 -04:00
parent 0d170d93ff
commit e61fedaac5
2 changed files with 22 additions and 92 deletions

View File

@@ -46,95 +46,14 @@ if [ "${RUNMODE}" = "addon" ]; then
export HAHOSTNAME=$(bashio::info.hostname)
export ADDONHOSTNAME=$HOSTNAME
# Setup the MQTT environment options based on addon configuration settings
MQTTURL=$(bashio::config "mqtt_url")
MQTTHOST=$(bashio::config "mqtt_host")
MQTTPORT=$(bashio::config "mqtt_port")
MQTTUSER=$(bashio::config "mqtt_user")
MQTTPASSWORD=$(bashio::config "mqtt_password")
if [ $MQTTURL = 'null' ] || [ $MQTTURL = '<auto_detect>' ]; then
if [ $MQTTHOST = '<auto_detect>' ]; then
if bashio::services.available 'mqtt'; then
case "$MQTTPORT" in
8883|8884)
# If using SSL/TLS then assume the hostname should be same as HA hostname
# This is the default configuration of Mosquitto addon, but it's of course
# possible that user generates customer certifiacate for MQTT addon with
# it's own hostname. I can't figure out any way to detect this so far.
MQTTHOST="${HAHOSTNAME}"
;;
*)
MQTTHOST=$(bashio::services mqtt "host")
if [ $MQTTHOST = 'localhost' ] || [ $MQTTHOST = '127.0.0.1' ]; then
echo "Discovered invalid value for MQTT host: ${MQTTHOST}"
echo "Overriding with default alias for Mosquitto MQTT addon"
MQTTHOST="core-mosquitto"
fi
;;
esac
echo "Discovered MQTT Host: ${MQTTHOST}"
else
echo "No Home Assistant MQTT service found, using Home Assistant hostname as default"
MQTTHOST="${HAHOSTNAME}"
echo "Discovered MQTT Host: ${MQTTHOST}"
fi
else
echo "Configured MQTT Hostname: ${MQTTHOST}"
fi
if [ $MQTTPORT = '<auto_detect>' ]; then
if bashio::services.available 'mqtt'; then
MQTTPORT=$(bashio::services mqtt "port")
echo "Discovered MQTT Port: ${MQTTPORT}"
else
MQTTPORT="1883"
echo "Default MQTT Port: ${MQTTPORT}"
fi
else
echo "Configured MQTT Port: ${MQTTPORT}"
fi
if [ $MQTTUSER = '<auto_detect>' ]; then
if bashio::services.available 'mqtt'; then
MQTTUSER=$(bashio::services mqtt "username")
echo "Discovered MQTT User: ${MQTTUSER}"
else
MQTTUSER=""
echo "Using anonymous MQTT connection"
fi
else
echo "Configured MQTT User: ${MQTTUSER}"
fi
if [ $MQTTPASSWORD = '<auto_detect>' ]; then
if bashio::services.available 'mqtt'; then
MQTTPASSWORD=$(bashio::services mqtt "password")
echo "Discovered MQTT password: <hidden>"
else
MQTTPASSWORD=""
fi
else
echo "Configured MQTT password: <hidden>"
fi
case "$MQTTPORT" in
8883|8884) MQTTURLPREFIX='mqtts://' ;;
*) MQTTURLPREFIX='mqtt://' ;;
esac
if [ ! -z $MQTTUSER ] && [ ! -z $MQTTPASSWORD ]; then
MQTTURL="${MQTTURLPREFIX}${MQTTUSER}:${MQTTPASSWORD}@${MQTTHOST}:${MQTTPORT}"
echo "Generated MQTT URL: ${MQTTURLPREFIX}${MQTTUSER}:<hidden>@${MQTTHOST}:${MQTTPORT}"
else
MQTTURL="${MQTTURLPREFIX}@${MQTTHOST}:${MQTTPORT}"
echo "Generated MQTT URL: ${MQTTURLPREFIX}${MQTTHOST}:${MQTTPORT}"
fi
else
echo "Configured MQTT URL: ${MQTTURL}"
# 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")
fi
fi
export MQTTURL
echo "-------------------------------------------------------"
echo "Running ring-mqtt..."
exec ./ring-mqtt.js

View File

@@ -1,6 +1,7 @@
const debug = require('debug')('ring-mqtt')
const colors = require('colors/safe')
const writeFileAtomic = require('write-file-atomic')
const url = require('url')
class Config {
constructor() {
@@ -26,9 +27,7 @@ class Config {
case 'addon':
this.file = '/data/options.json'
this.loadConfigFile()
if (!this.data.mqtt_url || this.data.mqtt_url === '<auto_detect>') {
this.data.mqtt_url = process.env.MQTTURL
}
this.doAutoConfig()
break;
default:
if (process.env.CONFIG) {
@@ -106,12 +105,12 @@ class Config {
delete this.data.mqtt_pass
this.valid = true
}
}
migrateToMqttUrl() {
debug ('Migrating legacy MQTT config options to mqtt_url...')
const mqttUrlPrefix = (this.data.port == 8883 || this.data.port == 8884) ? 'mqtts://' : 'mqtt://'
let mqttUrl = `${mqttUrlPrefix}${this.data.host}:${this.data.port}`
let mqttUrl = `${mqttUrlPrefix}${this.data.host}:${this.data.port}`
if (this.data.mqtt_user && this.data.mqtt_pass) {
mqttUrl = `${mqttUrlPrefix}${this.data.mqtt_user}:${this.data.mqtt_pass}@${this.data.host}:${this.data.port}`
}
@@ -123,6 +122,18 @@ class Config {
this.updateConfig()
}
doAutoConfig() {
let mqttURL = this.data.mqtt_url
? (this.data.mqtt_url)
: (`mqtt://${this.data.mqtt_user}:${this.data.mqtt_pass}@${this.data.mqtt_host}:${this.data.mqtt_port}`)
try {
mqttURL = new URL(mqttURL)
console.log(mqttURL)
} catch (err) {
debug('Failed to parse MQTT URL, please check that configuration is valid')
}
}
async updateConfig() {
try {
if (this.data.hasOwnProperty('ring_token')) {