Bluetooth TTS with HomeAssistant
I have been running HomeAssistant in our apartment for quite a while now. It is pretty well integrated and can adjust the lights, detect presence in all rooms and monitors temperature and humidity. One feature that I’ve been meaning to add for quite a while now is voice feedback using Text To Speech. HomeAssistant is running on my server in the livingroom that doesn’t have any speakers nearby. Ideally, the sound output should go through the Echo Dot but the API currently doesn’t support output besides what the Alexa Skills provide.
Thankfully, it is possible to connect to the Dot using Bluetooth for audio playback. Unfortunately, the setup on the server side wasn’t all that easy which is why I’m writing up the steps that I collected from various sources. This should work for a HomeAssistant installation on a Ubuntu 17.10 machine and assumes that HomeAssistant is already configured properly.
First, we’ll want to install the dependencies:
|
|
Pulseaudio
Since I’m running the server headless, there are no X-sessions and therefore pulseaudio will not be started for the login user. Instead, we can run pulseaudio in system mode. To do so, we need to create the necessary users and groups.
|
|
The last line allows the homeassistant user to access the audio devices.
Now we can create a systemd service file /etc/systemd/system/pulseaudio.service with the following content:
|
|
Since we want to allow pulseaudio to access bluetooth devices, we have to add some d-bus rules in /etc/dbus-1/system.d/pulse.conf:
|
|
Last but not least, we need to tell pulseaudio to always load the relevant bluetooth modules. This can be done by adding the following lines in /etc/pulse/system.pa:
|
|
Finally, we can enable and start the service:
|
|
Bluetooth
Now we need to make sure that Bluetooth is enabled and running:
|
|
Then we can pair the Echo Dot. This is done by using the command bluetoothctl.
|
|
Now, the server should be visible to the Echo Dot under its hostname. Pairing can be initiated through the Alexa app. During the pairing, there might be a request for confirmation. Afterwords, bluetoothctl can be quit.
At this point, everything should already be working. We can test this by adding the following lines in the HomeAssistant configuration:
|
|
We can the go to the web interface and in the service call section call the service tts.google_say with the following parameters:
|
|
This should cause HomeAssistant to request a TTS audio file from the Google TTS service. The file is then passed on to VLC which will play the file through the default pulseaudio device which is the bluetooth device.
Startup Configuration
This setup will continue to work as long as the server isn’t rebooted or the connection drops. In order to ensure that the bluetooth device is always connected, we can add a small daemon that repeatedly verifies that the device is connected and otherwise connects it. To do so, we create the script /usr/local/bin/echo-watchdog.sh (Fill in your Echo Dot’s MAC address):
|
|
|
|
And then we can create a systemd service file /etc/systemd/system/echo-watchdog.service for it:
|
|
By enabling and starting it, we make sure that the server always tries to connect the Echo Dot when it is available:
|
|
Now everything is up and running and TTS can be integrated into the HomeAssistant scripts.