mirror of
https://github.com/alexbelgium/hassio-addons.git
synced 2026-01-11 18:31:02 +01:00
Update 33-mqtt.sh
This commit is contained in:
@@ -2,9 +2,35 @@
|
||||
# shellcheck shell=bash
|
||||
set -e
|
||||
|
||||
if bashio::services.available 'mqtt' && ! bashio::config.true 'MQTT_DISABLED' ; then
|
||||
# Function to perform common setup steps
|
||||
common_steps () {
|
||||
# Attempt to connect to the MQTT broker
|
||||
TOPIC="birdnet"
|
||||
if mosquitto_pub -h "$MQTT_HOST" -p "$MQTT_PORT" -t "$TOPIC" -m "test" -u "$MQTT_USER" -P "$MQTT_PASS" -q 1 -d --will-topic "$TOPIC" --will-payload "Disconnected" --will-qos 1 --will-retain > /dev/null 2>&1; then
|
||||
# Adapt script with MQTT settings
|
||||
sed -i "s|%%mqtt_server%%|$MQTT_HOST|g" /helpers/birdnet_to_mqtt.py
|
||||
sed -i "s|%%mqtt_port%%|$MQTT_PORT|g" /helpers/birdnet_to_mqtt.py
|
||||
sed -i "s|%%mqtt_user%%|$MQTT_USER|g" /helpers/birdnet_to_mqtt.py
|
||||
sed -i "s|%%mqtt_pass%%|$MQTT_PASS|g" /helpers/birdnet_to_mqtt.py
|
||||
|
||||
# Copy script to the appropriate directory
|
||||
cp /helpers/birdnet_to_mqtt.py "$HOME"/BirdNET-Pi/scripts/utils/birdnet_to_mqtt.py
|
||||
chown pi:pi "$HOME"/BirdNET-Pi/scripts/utils/birdnet_to_mqtt.py
|
||||
chmod +x "$HOME"/BirdNET-Pi/scripts/utils/birdnet_to_mqtt.py
|
||||
|
||||
# Add hooks to the main analysis script
|
||||
sed -i "/load_global_model, run_analysis/a from utils.birdnet_to_mqtt import automatic_mqtt_publish" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
|
||||
sed -i '/write_to_db(/a\ automatic_mqtt_publish(file, detection, os.path.basename(detection.file_name_extr))' "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
|
||||
else
|
||||
bashio::log.fatal "MQTT connection failed, it will not be configured. Error message :"
|
||||
mosquitto_pub -h "$MQTT_HOST" -p "$MQTT_PORT" -t "$TOPIC" -m "test" -u "$MQTT_USER" -P "$MQTT_PASS" -q 1 -d --will-topic "$TOPIC" --will-payload "Disconnected" --will-qos 1 --will-retain
|
||||
fi
|
||||
}
|
||||
|
||||
# Check if MQTT service is available and not disabled
|
||||
if bashio::services.available 'mqtt' && ! bashio::config.true 'MQTT_DISABLED'; then
|
||||
bashio::log.green "---"
|
||||
bashio::log.blue "MQTT addon is active on your system! battybirdnet-pi is now automatically configured to send its ouptut to MQTT"
|
||||
bashio::log.blue "MQTT addon is active on your system! Birdnet-pi is now automatically configured to send its output to MQTT"
|
||||
bashio::log.blue "MQTT user : $(bashio::services "mqtt" "username")"
|
||||
bashio::log.blue "MQTT password : $(bashio::services "mqtt" "password")"
|
||||
bashio::log.blue "MQTT broker : tcp://$(bashio::services "mqtt" "host"):$(bashio::services "mqtt" "port")"
|
||||
@@ -14,34 +40,31 @@ if bashio::services.available 'mqtt' && ! bashio::config.true 'MQTT_DISABLED' ;
|
||||
bashio::log.blue "---"
|
||||
|
||||
# Apply MQTT settings
|
||||
sed -i "s|%%mqtt_server%%|$(bashio::services "mqtt" "host")|g" /helpers/birdnet_to_mqtt.py
|
||||
sed -i "s|%%mqtt_port%%|$(bashio::services "mqtt" "port")|g" /helpers/birdnet_to_mqtt.py
|
||||
sed -i "s|%%mqtt_user%%|$(bashio::services "mqtt" "username")|g" /helpers/birdnet_to_mqtt.py
|
||||
sed -i "s|%%mqtt_pass%%|$(bashio::services "mqtt" "password")|g" /helpers/birdnet_to_mqtt.py
|
||||
MQTT_HOST="$(bashio::services "mqtt" "host")"
|
||||
MQTT_PORT="$(bashio::services "mqtt" "port")"
|
||||
MQTT_USER="$(bashio::services "mqtt" "username")"
|
||||
MQTT_PASS="$(bashio::services "mqtt" "password")"
|
||||
|
||||
# Copy script
|
||||
cp /helpers/birdnet_to_mqtt.py /usr/bin/birdnet_to_mqtt.py
|
||||
cp /helpers/birdnet_to_mqtt.sh /custom-services.d
|
||||
chmod 777 /usr/bin/birdnet_to_mqtt.py
|
||||
chmod 777 /custom-services.d/birdnet_to_mqtt.sh
|
||||
# Perform common setup steps
|
||||
common_steps
|
||||
|
||||
# Check if manual MQTT configuration is provided
|
||||
elif bashio::config.has_value "MQTT_HOST_manual" && bashio::config.has_value "MQTT_PORT_manual"; then
|
||||
bashio::log.green "---"
|
||||
bashio::log.blue "MQTT is manually configured in the addon options"
|
||||
bashio::log.blue "battybirdnet-pi is now automatically configured to send its ouptut to MQTT"
|
||||
bashio::log.blue "Birdnet-pi is now automatically configured to send its output to MQTT"
|
||||
bashio::log.green "---"
|
||||
bashio::log.blue "Data will be posted to the topic : 'birdnet'"
|
||||
bashio::log.blue "Json data : {'Date', 'Time', 'ScientificName', 'CommonName', 'Confidence', 'SpeciesCode', 'ClipName', 'url'}"
|
||||
bashio::log.blue "---"
|
||||
|
||||
# Apply MQTT settings
|
||||
sed -i "s|%%mqtt_server%%|$(bashio::config "MQTT_HOST_manual")|g" /helpers/birdnet_to_mqtt.py
|
||||
sed -i "s|%%mqtt_port%%|$(bashio::config "MQTT_PORT_manual")|g" /helpers/birdnet_to_mqtt.py
|
||||
sed -i "s|%%mqtt_user%%|$(bashio::config "MQTT_USER_manual")|g" /helpers/birdnet_to_mqtt.py
|
||||
sed -i "s|%%mqtt_pass%%|$(bashio::config "MQTT_PASSWORD_manual")|g" /helpers/birdnet_to_mqtt.py
|
||||
# Apply manual MQTT settings
|
||||
MQTT_HOST="$(bashio::config "MQTT_HOST_manual")"
|
||||
MQTT_PORT="$(bashio::config "MQTT_PORT_manual")"
|
||||
MQTT_USER="$(bashio::config "MQTT_USER_manual")"
|
||||
MQTT_PASS="$(bashio::config "MQTT_PASSWORD_manual")"
|
||||
|
||||
# Perform common setup steps
|
||||
common_steps
|
||||
|
||||
# Copy script
|
||||
cp /helpers/birdnet_to_mqtt.py /usr/bin/birdnet_to_mqtt.py
|
||||
cp /helpers/birdnet_to_mqtt.sh /custom-services.d
|
||||
chmod +x /usr/bin/birdnet_to_mqtt.py
|
||||
chmod +x /custom-services.d/birdnet_to_mqtt.sh
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user