allow mqtt autodiscover

This commit is contained in:
Alexandre
2021-12-03 21:26:47 +01:00
parent 6401009627
commit f3d08df1fe
3 changed files with 36 additions and 11 deletions

View File

@@ -0,0 +1,25 @@
#!/usr/bin/env bashio
#####################
# Autodiscover mqtt #
#####################
if bashio::config.true 'mqtt_autodiscover'; then
bashio::log.info "mqtt_autodiscover is defined in options, attemption autodiscovery..."
# Check if available
[ ! bashio::services.available "mqtt" ] && bashio::exit.nok "No internal MQTT service found. Please install Mosquitto broker"
# Get variables
bashio::log.info "... MQTT service found, fetching server detail ..."
export MQTT_HOST=$(bashio::services mqtt "host")
export MQTT_PORT=$(bashio::services mqtt "port")
export MQTT_SSL=$(bashio::services mqtt "ssl")
export MQTT_USERNAME=$(bashio::services mqtt "username")
export MQTT_PASSWORD=$(bashio::services mqtt "password")
# Export variables
for variables in "MQTT_HOST=$MQTT_HOST" "MQTT_PORT=$MQTT_PORT" "MQTT_SSL=$MQTT_SSL" "MQTT_USERNAME=$MQTT_USERNAME" "MQTT_PASSWORD=$MQTT_PASSWORD"; do
sed -i "1a export $variables" /etc/services.d/*/*run*
sed -i "1a export $variables" /scripts/*run*
done
# Log
bashio::log.info "Received user: '$MQTT_USERNAME' for MQTT at '$MQTT_HOST:$MQTT_PORT'!"
fi

View File

@@ -45,10 +45,10 @@ function parse_yaml {
local prefix=$2 || local prefix=""
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @ | tr @ '\034')
sed -ne "s|^\($s\):|\1|" \
-e "s| #.*$||g" \
-e "s|#.*$||g" \
-e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
-e "s| #.*$||g" \
-e "s|#.*$||g" \
-e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
awk -F$fs '{
indent = length($1)/2;
vname[indent] = $2;
@@ -84,11 +84,11 @@ while IFS= read -r line; do
export $line # Export the variable
logmsg="Variable set : $line"
if [ -f /etc/services.d/*/*run* ]; then
sed -i "1a export $line" /etc/services.d/*/run # Export the variable
sed -i "1a export $line" /etc/services.d/*/run # Export the variable
sed -i "1a bashio::log.blue \"$logmsg\" || echo \"$logmsg\"" /etc/services.d/*/run # Show text in colour
fi
if [ -f /scripts/*run* ]; then
sed -i "1a export $line" /scripts/*run* # Export the variable
sed -i "1a export $line" /scripts/*run* # Export the variable
sed -i "1a bashio::log.blue \"$logmsg\" || echo \"$logmsg\"" /scripts/*run* # Show text in colour
fi
bashio::log.blue "$line"
@@ -101,8 +101,8 @@ done <"/tmpfile"
# Test mode
TZ=$(bashio::config "TZ")
if [ $TZ = "test" ]; then
echo "secret mode found, launching script in /config/test.sh"
cd /config
chmod 777 test.sh
./test.sh
echo "secret mode found, launching script in /config/test.sh"
cd /config
chmod 777 test.sh
./test.sh
fi

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env bashio
#!/usr/bin/env bashio
##############
# Launch App #