Update 33-mqtt.sh

This commit is contained in:
Alexandre
2026-06-22 08:32:45 +02:00
committed by GitHub
parent eb336f4f8d
commit dbbbdd3581

View File

@@ -8,6 +8,11 @@ set -e
# way to auto-configure them. The behaviour is opt-in via the
# mqtt_auto_config addon option. When the option is off but Mosquitto is
# detected, we log a one-shot hint pointing users at the option.
#
# In addition to the broker credentials we enable BirdNET-Go's native Home
# Assistant MQTT auto-discovery (realtime.mqtt.homeassistant.*). This makes
# the detection sensors appear in Home Assistant automatically, so users no
# longer have to hand-write the MQTT sensor YAML from HAINTEGRATION.md.
CONFIG_LOCATION="/config/config.yaml"
@@ -23,7 +28,7 @@ MQTT_BROKER="tcp://${MQTT_HOST}:${MQTT_PORT}"
if ! bashio::config.true 'mqtt_auto_config'; then
bashio::log.green "---"
bashio::log.yellow "Home Assistant MQTT addon detected. Set 'mqtt_auto_config: true' in the addon options to wire it into BirdNET-Go automatically. Connection details:"
bashio::log.yellow "Home Assistant MQTT addon detected. Set 'mqtt_auto_config: true' in the addon options to wire it into BirdNET-Go automatically AND enable Home Assistant auto-discovery (sensors appear in HA with no manual YAML). Connection details:"
bashio::log.blue "MQTT user : ${MQTT_USER}"
bashio::log.blue "MQTT password: ${MQTT_PASS}"
bashio::log.blue "MQTT broker : ${MQTT_BROKER}"
@@ -40,10 +45,21 @@ bashio::log.green "---"
bashio::log.blue "mqtt_auto_config enabled; writing Home Assistant MQTT credentials into BirdNET-Go config"
bashio::log.blue "Broker: ${MQTT_BROKER}"
bashio::log.blue "User: ${MQTT_USER}"
bashio::log.blue "Home Assistant auto-discovery: enabled (sensors appear in HA automatically)"
bashio::log.green "---"
# $broker / $user / $pass / "birdnet" are jq/yq variables and literals,
# not shell expansions, so the single quotes are intentional.
#
# Connection fields (enabled/broker/username/password) are force-set on every
# start so they track the HA MQTT addon's rotating credentials. Topic and the
# homeassistant.* discovery knobs use "//=" so they are only seeded when
# missing. retain is seeded via an explicit has() check rather than "//=",
# because jq treats a user-set "retain: false" as falsy and "//=" would wrongly
# flip it back to true; has() seeds the default only when the key is truly
# absent. Any value the user later changes in the BirdNET-Go UI or config.yaml
# therefore survives restarts. homeassistant.enabled is force-set to true
# because turning on discovery is the whole point of the auto-config option.
# shellcheck disable=SC2016
yq -i -y \
--arg broker "$MQTT_BROKER" \
@@ -53,5 +69,9 @@ yq -i -y \
| .realtime.mqtt.broker = $broker
| .realtime.mqtt.username = $user
| .realtime.mqtt.password = $pass
| .realtime.mqtt.topic //= "birdnet"' \
| .realtime.mqtt.topic //= "birdnet"
| (if (.realtime.mqtt | has("retain")) then . else .realtime.mqtt.retain = true end)
| .realtime.mqtt.homeassistant.enabled = true
| .realtime.mqtt.homeassistant.discovery_prefix //= "homeassistant"
| .realtime.mqtt.homeassistant.device_name //= "BirdNET-Go"' \
"$CONFIG_LOCATION"