Hi
Thought I’d post this as it took a while to find the relevant files to have success and have my Bluetooth speakers staying permanently connected for tts and playing media on my Pi3 with Hass. I am now using Bluetooth and the audio jack with amplified speakers and can connect to either speakers with tts or to play media via shell_command
Lisited below is the process I followed, I ran in this order but if your getting disconnection issues and have your bluetooth connected (pulse/bluez, possibly others), you may want to jump straight to the emojis and the file changes for bluetooth for the ones you have… although some of the stuff listed below may have helped my connection…
I started with a backup of my Hassbian as an image via Win32Diskimager, I’ve been stung before with loss of the work on my config. As it turned out in the end I did restore back to my previous image and starting again to get everything working. WinSCP is invaluable for quick editing of system files. I also backed up all my .homeassistant folder and all config files, just in case.
I followed the great guide here first;
https://github.com/pkozul/ha-tts-bluetooth-speaker
After that made sure I ran sudo apt-get update
and sudo apt-get upgrade
before continuing.
I uninstalled sudo apt-get remove bluez
and reinstalled installed latest Bluez from instructions here;
https://learn.adafruit.com/install-bluez-on-the-raspberry-pi/installation
Uninstalled and reinstalled pulseaudio sudo apt-get remove pulseaudio
sudo apt-get install pulseaudio
and installed volume control for pulse audio sudo apt-get install pavucontrol
I also installed this if memory serves; sudo apt-get install alsa-utils bluez-tools pulseaudio-module-bluetooth
the last module was already installed in the instructions in the first post above
Not sure if it changed anything but I uninstalled and reinstalled vlc-nox, I also installed omxplayer.
sudo apt-get remove vlc-nox
sudo apt-get install vlc-nox
sudo apt-get install omxplayer
Ran this for vlc;
sudo usermod -a -G audio homeassistant
This was important
sudo chmod a+x /home/homeassistant/.homeassistant/scripts/pair_bluetooth.sh
(thats the directory my file is installed).
I have this in /etc/sudoers
file (for the time being but I will close this down to the files as needed for media);
#includedir /etc/sudoers.d
hass ALL=(ALL) NOPASSWD: ALL
This was useful for checking sinks were working with bluetooth and my card;
pactl list short sinks
(mine lists two sinks, one for the Alsa card and one for the Bluez bluetooth speaker followed by the Mac address of connected speaker)
These files below were all the ones I altered to ensure Bluetooth stayed connected and I haven’t seen any issues with this (make sure that the instructions are followed in the post for the bluetooth tts speaker above for the Bluetooth speaker address to automatically connect to the Pi and the relevant permissions for that script have been run, I installed the files/folders from the post in my same directory with my .yaml
's- .homeassistant
).
Listed is the file name and the section I altered, the rest of the file content was left alone, then re saved all;
.
/etc/pulse/system.pa
disable the module loading with # in front of load-module;
# Automatically suspend sinks/sources that become idle for too long
#load-module module-suspend-on-idle
.
this shoulod have already been added at the base of this file;
.
### Bluetooth Support
.ifexists module-bluetooth-discover.so
load-module module-bluetooth-discover
.endif
.
.
/etc/pulse/daemon.conf
change these;
#changed below from 20 to -1 for both
; exit-idle-time =-1
; scache-idle-time =-1
.
.
/lib/systemd/system/bluetooth.target
change from yes to no;
StopWhenUnneeded=no
.
.
/etc/bluetooth/input.conf
changed to this;
IdleTimeout=0
.
.
/etc/bluetooth/main.conf
added;
AutoConnect=true
.
.
/etc/pulse/default.pa
disable load-module with # in front;
### Automatically suspend sinks/sources that become idle for too long
#removed below#
#load-module module-suspend-on-idle
.
and add this in at the base if not there;
load-module module-switch-on-connect
# load system wide configuration
.include /etc/pulse/default.pa
.
.
and ensure this file was created as part of the tts guide/post
.
etc/systemd/system/pulseaudio.service
.
[Unit]
Description=Pulse Audio
[Service]
Type=simple
ExecStart=/usr/bin/pulseaudio --system --disallow-exit --disable-shm --exit-idle-time=-1
[Install]
WantedBy=multi-user.target
.
This was helpful to understand pulse audio;
https://wiki.archlinux.org/index.php/PulseAudio
hcidump [-i hciX]
was good for Bluetooth status (I kept encountering something like ‘disconnected by local host’ when monitoring the Bluetooth connection (before altering all the files)
Volume can be controlled from, with the up/down arrows;
alsamixer
I’m sure I probably didn’t need to alter all the files but I have a great consistent connection.
Bluetoothctl
For the Bluetooth control I made sure I trusted the device when pairing and used the following to access the bluetooth; $ bluetoothctl
Options once in;
[bluetooth]#agent on
[bluetooth]#default-agent
List devices;
[bluetooth]#power on
[bluetooth]#discoverable on
[bluetooth]#pairable on
[bluetooth]#scan on
Devices come up with mac addresses, you need to copy/paste the one you want and try to to work it out if not obvious- this is the address you need in the files in the post at the beginning and for your .yaml
Then connect to your chose output (copy and paste MAC);
[bluetooth]#trust xx:xx:xx:xx:xx:xx
[bluetooth]#pair xx:xx:xx:xx:xx:xx
[bluetooth]#connect xx:xx:xx:xx:xx:xx
[bluetooth]#info
was useful to see connection/trust/pair status.
.
exit
to exit bluetoothctl
More info on #bluetoothctl here;
https://www.npmjs.com/package/bluetoothctl
For info my configuration.yaml
;
media_player:
- platform: vlc
name: speaker_1
arguments: '--alsa-audio-device=hw:0,0'
#volume: 0.8
- platform: tts_bluetooth_speaker
address: xx:xx:xx:xx:xx:xx [Your MAC Address]
volume: 0.8
pre_silence_duration: 1
post_silence_duration: 0.5
cache_dir: /tmp/tts
tts:
platform: google
cache: true
cache_dir: /tmp/tts
time_memory: 300
Initially I had the above ‘speaker_1’ media player set as; arguments: ‘–alsa-audio-device=plughw:0,1’ as this was detailed in some other post as being potentially better via an interface but that didn’t work for the jack speakers as well, I believe my final config is direct; arguments: '--alsa-audio-device=hw:0,0'
As I was using the alsa card as well as bluetooth behind SSL I made sure this was included in my http in confiuration.yaml.
http:
base_url: https://your https address (no need for port number)
I’m running some shell scripts to call media and automatons for tts notifications (it reads the temp when the heating turns on/off and weather), its driving everyone mad in the house with everything notified! I’ll hone this down in time, although the Muppets theme tune will probably stay… it suits the kids
I hope this helps someone and saves time as it took me quite some time to get this sorted and remember it for this