All Projects → pkozul → Ha Tts Bluetooth Speaker

pkozul / Ha Tts Bluetooth Speaker

TTS Bluetooth Speaker for Home Assistant

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Ha Tts Bluetooth Speaker

Home Assistant Config
Home Assistant config files, rewritten to use the latest features, 100+ documented automations, automatically generated ToC 🏠 🤖
Stars: ✭ 926 (+561.43%)
Mutual labels:  home-assistant, home-automation, home-assistant-config, internet-of-things
Home Assistant Config
🏠 My Home Assistant configuration, a bit different that others :) Be sure to 🌟 this repository for updates!
Stars: ✭ 1,050 (+650%)
Mutual labels:  home-assistant, home-automation, home-assistant-config, internet-of-things
Homeassistant
Example Home Assistant Configs
Stars: ✭ 846 (+504.29%)
Mutual labels:  home-assistant, home-automation, home-assistant-config, internet-of-things
Homeassistant
Example Home Assistant Configs
Stars: ✭ 168 (+20%)
Mutual labels:  home-assistant, home-automation, home-assistant-config, internet-of-things
Home Assistant
Home-Assistant-Config
Stars: ✭ 182 (+30%)
Mutual labels:  home-assistant, home-automation, home-assistant-config, internet-of-things
Ha Floorplan
Floorplan for Home Assistant
Stars: ✭ 1,626 (+1061.43%)
Mutual labels:  home-assistant, home-automation, home-assistant-config, internet-of-things
Homeassistant Config
Stars: ✭ 211 (+50.71%)
Mutual labels:  home-assistant, home-automation, home-assistant-config, internet-of-things
jarvis
Jarvis Home Automation
Stars: ✭ 81 (-42.14%)
Mutual labels:  home-automation, internet-of-things, home-assistant, home-assistant-config
Home Assistant Configuration
My Home Assistant Config. For more Information visit ->
Stars: ✭ 102 (-27.14%)
Mutual labels:  home-assistant, home-automation, home-assistant-config
Hass Config
Configuration files for Home Assistant
Stars: ✭ 32 (-77.14%)
Mutual labels:  home-assistant, home-automation, home-assistant-config
Home Assistantconfig
🏡 My Home Assistant Configs. Be sure to 🌟 my repo, if it has been of any help
Stars: ✭ 43 (-69.29%)
Mutual labels:  home-assistant, home-automation, home-assistant-config
Homeassistant Config
My personal Home Assistant config
Stars: ✭ 66 (-52.86%)
Mutual labels:  home-assistant, home-automation, home-assistant-config
Home Assistant Js Websocket
🚡 JavaScript websocket client for Home Assistant
Stars: ✭ 100 (-28.57%)
Mutual labels:  home-assistant, home-automation, internet-of-things
Home Assistant Config
🏠 My Home Assistant configuration and documentation. More info below.
Stars: ✭ 107 (-23.57%)
Mutual labels:  home-assistant, home-automation, home-assistant-config
Home Assistant Config
My Home Assistant configuration
Stars: ✭ 41 (-70.71%)
Mutual labels:  home-assistant, home-automation, home-assistant-config
Open Home Automation
Open Home Automation with Home Assistant, ESP8266/ESP32 and MQTT
Stars: ✭ 820 (+485.71%)
Mutual labels:  home-assistant, home-automation, home-assistant-config
Room Assistant
Presence tracking and more for automation on the room-level
Stars: ✭ 764 (+445.71%)
Mutual labels:  home-assistant, home-automation, bluetooth
Ha Bt Proximity
Distributed Bluetooth Room Presence Sensor for Home Assistant
Stars: ✭ 77 (-45%)
Mutual labels:  home-assistant, home-automation, bluetooth
Home Assistant Configs
Config files used for my install of Home Assistant. This is no longer maintained as I started my install over, and have been using the UI for configuration.
Stars: ✭ 73 (-47.86%)
Mutual labels:  home-assistant, home-automation, home-assistant-config
Homeassistant Config
Configuration for @brianjking & @KinnaT's Home Assistant Installation
Stars: ✭ 80 (-42.86%)
Mutual labels:  home-assistant, home-automation, home-assistant-config

TTS Bluetooth Speaker for Home Assistant

This project provides a media player (custom component) for Home Assistant that plays TTS (text-to-speech) via a Bluetooth speaker.

If you're using HA's Bluetooth device tracker (for presence detection), this project also provides a replacement Bluetooth tracker that allows both components to play nicely together.

Since the Bluetooth tracker constantly scans for devices, playback of audio on the Bluetooth speaker may be disrupted / become choppy while scanning. These custom components work together to ensure only one of them is accessing Bluetooth at any given time.

The flow is something like this:

  • Bluetooth tracker component continually scans for devices (presence detection)
  • TTS service gets called to play something on the Bluetooth speaker
  • TTS Bluetooth speaker component disables Bluetooth tracker component
  • Bluetooth tracker component terminates any running Bluetooth scans
  • TTS Bluetooth speaker component plays the TTS MP3 file
  • TTS Bluetooth speaker component enables Bluetooth tracker component
  • Bluetooth tracker component continues scanning for devices (presence detection)

Getting Started

1) Install Pulse Audio (with Bluetooth support), MPlayer and SoX (with MP3 support)

sudo apt-get install pulseaudio pulseaudio-module-bluetooth bluez mplayer sox libsox-fmt-mp3

2) Add HA and pi user to 'pulse-access' group (pi user for testing, homeassistant for the service)

sudo adduser pi pulse-access
sudo adduser homeassistant pulse-access

3) Add Bluetooth discovery to Pulse Audio

In /etc/pulse/system.pa, add the following to the bottom of the file:

### Bluetooth Support
.ifexists module-bluetooth-discover.so
load-module module-bluetooth-discover
.endif

#set-card-profile bluez_card.00_2F_AD_12_0D_42 a2dp_sink

The last part is to persist the setting for a2dp, in case your bluetooth seems to default to a different profile. I have commented it out because it seems to be flakey.

You may want to uncomment this line if your audio is getting cut off:

### Automatically suspend sinks/sources that become idle for too long
#load-module module-suspend-on-idle

4) Create a service to run Pulse Audio at startup

Create the file /etc/systemd/system/pulseaudio.service and add the following to it:

[Unit]
Description=Pulse Audio

[Service]
Type=simple
Environment=DBUS_SESSION_BUS_ADDRESS=unix:path=/run/dbus/system_bus_socket
ExecStart=/usr/bin/pulseaudio --system --disallow-exit --disable-shm --exit-idle-time=-1

[Install]
WantedBy=multi-user.target

Enable the service to start at boot time.

sudo systemctl daemon-reload
sudo systemctl enable pulseaudio.service

Give pulse user access to bluetooth interfaces

edit /etc/dbus-1/system.d/bluetooth.conf

add the following lines:

  <policy user="pulse">
    <allow send_destination="org.bluez"/>
    <allow send_interface="org.bluez.MediaEndpoint1"/>
  </policy>

5) Create a script to pair the Bluetooth speaker at startup

sudo bluetoothctl
scan on
pair 00:2F:AD:12:0D:42
trust 00:2F:AD:12:0D:42
connect 00:2F:AD:12:0D:42
quit

Create the file [PATH_TO_YOUR_HOME_ASSSISTANT]/scripts/pair_bluetooth.sh and add the following to it. Make sure to replace the Bluetooth address with that of your Bluetooth speaker.

#!/bin/bash

bluetoothctl << EOF
connect 00:2F:AD:12:0D:42
EOF

Make sure to grant execute permissions for the script.

sudo chmod a+x [PATH_TO_YOUR_HOME_ASSSISTANT]/scripts/pair_bluetooth.sh

In /etc/rc.local, add the following to the end of the file to run the script at startup:

# Pair Bluetooth devices
[PATH_TO_YOUR_HOME_ASSSISTANT]/scripts/pair_bluetooth.sh

exit 0

6) Add the TTS Bluetooth Speaker to HA

Copy the TTS Bluetooth Speaker component (from this GitHub repo) and save it to your Home Assistant config directory.

custom_components/tts_bluetooth_speaker/media_player.py

7) Optional - Add the (new) Bluetooth Tracker to HA

This step only applies if you're using the Bluetooth tracker.

Copy the Bluetooth Tracker component and save it to your Home Assistant config directory.

custom_components/bluetooth_tracker/device_tracker.py

8) Validate audio sink is available

pactl list sinks

You should see something like:

Sink #1
        State: SUSPENDED
        Name: bluez_sink.00_2F_AD_12_0D_42.a2dp_sink

If it instead says headset_head_unit, you can switch to a2dp profile as follows:

pactl set-card-profile bluez_card.00_2F_AD_12_0D_42 a2dp_sink

Check again and validate it is using a2dp.

Test using command line if mplayer can stream to a2dp

mplayer -ao pulse::bluez_sink.00_2F_AD_12_0D_42.a2dp_sink -channels 2 -volume 100 /some/mp3file.mp3

9) Start using it in HA

By this stage (after a reboot), you should be able to start using the TTS Bluetooth speaker in HA.

Below is an example of how the component is configured. You need to specify the Bluetooth address of your speaker, and optionally set the volume level (must be between 0 and 1). If you find your speaker is not playing the first part of the audio (i.e. first second is missing when played back), then you can optionally add some silence before and/or after the original TTS audio hsing the pre_silence_duration and post_silence_duration options (must be between 0 and 60 seconds). If you've change your TTS cache directory (in your TTS config), then you should set the cache_dir here to match.

media_player:
  - platform: tts_bluetooth_speaker
    address: [BLUETOOTH_ADDRESS]   # Required - for example, 00:2F:AD:12:0D:42
    volume: 0.45                   # Optional - default is 0.5
#    pre_silence_duration: 1       # Optional - No. of seconds silence before the TTS (default is 0)
#    post_silence_duration: 0.5    # Optional - No. of seconds silence after the TTS (default is 0)
#    cache_dir: /tmp/tts           # Optional - make sure it matches the same setting in TTS config

If you're using the Bluetooth tracker, you probably already have this in your config:

device_tracker:
  - platform: bluetooth_tracker

To test that it's all working, you can use Developer Tools > Services in the HA frontend to play a TTS message through your Bluetooth speaker:

image

{ "entity_id": "media_player.tts_bluetooth_speaker", "message": "Hello" }

Another way to test it is to add an automation that plays a TTS message whenever HA is started:

automation: 
  - alias: Home Assistant Start
    trigger:
      platform: homeassistant
      event: start
    action:
      - delay: '00:00:10'
      - service: tts.google_translate_say
        data:
          entity_id: media_player.tts_bluetooth_speaker
          message: 'Home Assistant has started'
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].