Google TTS to a Bluetooth Speaker on Synology NAS

I currently have a Synology NAS (DS415+) and I want to be able to make some announcement (thought Google TTS) to a Bluetooth Speaker.

The best and easy way to handle this is using the vlc media_player component passing as a argument the alsa information related with the speaker, but because I didn’t find a easy way to install vlc in my NAS (from my previous experience, is a good practice try to avoid to tweak so much the system) I create a custom media_player component that can handle the tts.google_say service call.

In order to do this we need to:

  1. How to play a music file thought a Bluetooth Speaker
  • Create a media_player component to be used in HA
  • Configure HA
  • How to Test and use it

1. How to play a Music File thought a Bluetooth Speaker

The easiest way to do this and avoid to tweak the NAS is using the existing tools.

Audio Station is a package that gives you the ability to play music files. It uses mplayer and pulseaudio, so we will use that in order to see how to play a file to a Bluetooth Speaker.

I had a usb bluetooth dongle installed on my NAS (Kinivo BTD-300 Bluetooth 3.0 Low Energy USB adapter https://kinivo.com/product/btd-300-usb-bluetooth-adapter-3-0) and of course a Bluetooth Speaker (a old Jawbone Jambox).

We must:

  1. Pair the bluetooth speaker to the NAS, to do that just follow this tutorial https://www.synology.com/en-global/knowledgebase/DSM/help/DSM/AdminCenter/connection_bluetooth.

  2. Play a music file through the Bluetooth Speaker, to do that just follow this tutorial https://www.synology.com/en-global/knowledgebase/DSM/help/AudioStation/audiosource).

  3. While the music file is playing, open a ssh shell to the NAS and check the processes that belongs to the AudioStation user:

admin@DiskStation:/tmp$ ps ax -l | grep AudioStation
0 S  1024 18468 15503  0  80   0 -  5780 pipe_w pts/21     0:00 grep --color=auto AudioStation
5 S 254178 19776    1  0  75  -5 - 164993 ?     ?         46:43 /var/packages/AudioStation/target/sbin/synoaudiod
5 S     0 19920     1  0  69 -11 - 60270 SyS_pp ?          0:27 /var/packages/AudioStation/target/bin/pulseaudio --realtime=false
5 S 254178 20528    1  0  75  -5 -  9403 ?      ?         13:26 /var/packages/AudioStation/target/sbin/synorcd
5 S 254178 32348    1  0  75  -5 - 61262 ?      ?          0:03 /var/packages/AudioStation/target/bin/mplayer -gapless-audio -ao pulse::bluez_sink.00_21_3C_6D_37_01 -srate 44100 -channels 2 -format s16le -unique bluez_sink.00_21_3C_6D_37_01 -daemon /volume1/music/Text.mp3 -volume 67

So we can see the command line to do that is:

/var/packages/AudioStation/target/bin/mplayer -gapless-audio -ao pulse::bluez_sink.00_21_3C_6D_37_01 -srate 44100 -channels 2 -format s16le -unique bluez_sink.00_21_3C_6D_37_01 -daemon /volume1/music/Text.mp3 -volume 67

Where pulse::bluez_sink.00_21_3C_6D_37_01 is the ID of the bluetooth speaker (pulseaudio), this is important for the next step.

Let’s create a shellscript to handle that. Is important to consider that:

  • The tts.google_say service call will request to the media_player component to play a mp3 file (a URL)

  • The mplayer installed with Audio Station does not support TLS/SSL, the script will download first the file and then play the local mp3 file

  • The mplayer command needs to be executed with sudo in order to work (I didn’t find a way to avoid this).

I named the script play_url_bluetooth.sh and put it on the folder /config/programs:

2. Create a media_player component to be used in HA

I created a very simple media_player component that was named bluetooth_speaker and use the previous play_url_bluetooth.sh script:

I named the component bluetooth_speaker.py and put it on the folder /config/custom_components/media_player:

3. Configure HA

3.1 Enable https on HA (Optional but recommended)

To enable SSL Access to your HA, is necessary to add the following to configuration.yaml:

http:
  # Uncomment this to add a password (recommended!)
  api_password: YOUR_PASSWORD

  ssl_certificate: PATH_TO_YOUR_SSL_CERTIFICATE
  ssl_key: PATH_TO_YOUR_SSL_KEY

  # Uncomment this if you are using SSL or running in Docker etc
  base_url: WWW.YOUR_DOMAIN.COM:8123

3.2 Configure Google TTS

We must add the following to the configuration.yaml:

tts:
  platform: google
  cache: true
  time_memory: 300
  language: 'en'

Notice: Is important to have external access to HA in order that the TTS service works, so in this case is necessary to create a port-forwarding rule for 8123.

3.3 Configure the bluetooth_speaker component

Just add to configuration.yaml:

media_player:
  platform: bluetooth_speaker

3.4 Run HA

After run HA, you will see the new media_player component named Bluetooth Speaker:

4. How to Test and use it

4.1 Using HA States GUI

Click in the “…”

Write some text and test it:

4.2 Using the Development Tools on HA GUI

Click on the “Services” icon on Development Tools Section, and make a service call like this:

4.3 Make a Call from a Script

Inside a script, you can use the tts.google_say service like this:

 service: tts.google_say
    entity_id: media_player.bluetooth_speaker
    data:
      message: 'Testing Google Say TTS Service'
      language: 'en'
4 Likes

Great post. It helped me set up the same thing on a Raspberry Pi. I enhanced your script a little so it no longer contains any ‘hard coded’ Bluetooth address nor volume levels. Instead, you specify these as part of the media player config in HA, and they are passed in to the script by HA.

You can see it on this thread:

https://community.home-assistant.io/t/tts-on-bluetooth-speaker-via-raspberry-pi/