To use my Bluetooth speaker with my Home Assistant controlled Multi-room audio set-up, I added a shell script that exposes Bluetooth connection status reporting and triggers over MQTT.
You can find the complete instructions on how to set up Debian with this script in my article about how I run a snapcast client with MQTT controlled Bluetooth speaker on a raspberry pi
bluetoothspeaker2mqtt will report status via MQTT TOPIC_STATE (on/off) when the device connects/disconnects. write on/off to TOPIC_SET to trigger bluetoothctl connect/disconnect
A simple shell script that will watch evtest output from the bluetooth buttons. I only tested this with a JBL Charge 4 which has one button.
Service files to automatically start bluetoothspeaker2mqtt, and to have bluetoothspeaker2mqtt start/stop a snapclient, and bluetoothspeaker-buttonwatcher
apt install expect mosquitto-clients evtest snapclient
sudo install -m 744 bluetoothspeaker2mqtt /usr/local/bin/bluetoothspeaker2mqtt
sudo install -m 744 bluetoothspeaker-buttonwatcher /usr/local/bin/bluetoothspeaker-buttonwatcher
sudo install -m 644 bluetoothspeaker-buttonwatcher.service /etc/systemd/system
sudo install -m 644 bluetoothspeaker2mqtt.service /etc/systemd/system
sudo systemctl daemon-reload
write a file /etc/bluetoothspeaker2mqtt.conf
, add settings you want to override as key/value pairs. Available are:
TOPIC_BUTTON=""
SPEAKER_NAME=""
TOPIC_STATE=""
TOPIC_SET=""
SNAPCLIENT=""
STARTSTOP_SNAPCLIENT=""
BUTTONWATCHER_SERVICE=""
STARTSTOP_BUTTONWATCHER=""
POST_CONNECT_SETTLE_TIME=""
Write a file /root/.config/mosquitto_pub
with MQTT configuration, verify you don't have trailing newlines.
-h mqtt_hostname
-u mqtt_username
-P mqtt_password
Add symlinks for both pub and sub tools:
ln -s /root/.config/mosquitto /root/.config/mosquitto_pub
ln -s /root/.config/mosquitto /root/.config/mosquitto_sub
Next, (as root) you can test the scripts manually before using them from via systemd. F first source the configuration like this:
set -o allexport
source /etc/bluetoothspeaker2mqtt.conf
set +o allexport
then run e.g.: /usr/local/bin/bluetoothspeaker2mqtt
Enable systemd services:
sudo systemctl start bluetoothspeaker2mqtt
sudo systemctl enable bluetoothspeaker2mqtt