원본: https://github.com/pkozul/ha-tts-bluetooth-speaker
이 프로젝트는 Bluetooth 스피커를 통해 TTS(텍스트 음성 변환)를 재생하는 Home Assistant용 미디어 플레이어(사용자 정의 구성 요소)를 제공합니다.
HA의 Bluetooth 장치 추적기(존재 감지용)를 사용하는 경우, 이 프로젝트는 두 구성 요소가 원활하게 작동할 수 있도록 하는 대체 Bluetooth 추적기도 제공합니다.
Bluetooth 추적기는 지속적으로 장치를 스캔하기 때문에 Bluetooth 스피커에서 오디오 재생이 중단되거나 스캔하는 동안 끊길 수 있습니다. 이러한 사용자 지정 구성 요소는 함께 작동하여 주어진 시간에 그 중 하나만 Bluetooth에 액세스하도록 합니다.
흐름은 다음과 같습니다.
• Bluetooth 추적 구성 요소는 장치를 지속적으로 스캔합니다(존재 감지)
• TTS 서비스가 Bluetooth 스피커에서 무언가를 재생하도록 호출됩니다.
• TTS Bluetooth 스피커 구성 요소가 Bluetooth 추적 구성 요소를 비활성화합니다.
• Bluetooth 추적 구성 요소는 실행 중인 모든 Bluetooth 스캔을 종료합니다.
• TTS 블루투스 스피커 구성 요소는 TTS MP3 파일을 재생합니다.
• TTS Bluetooth 스피커 구성 요소는 Bluetooth 추적 구성 요소를 활성화합니다.
• Bluetooth 추적기 구성 요소는 장치(존재 감지)를 계속 스캔합니다.
시작하기
1) Pulse Audio(Bluetooth 지원), MPlayer 및 SoX(MP3 지원) 설치
apt-get install -y pulseaudio pulseaudio-module-bluetooth bluez mplayer sox libsox-fmt-mp3
2) 'pulse-access' 그룹에 HA 및 pi 사용자 추가(테스트용 pi 사용자, 서비스용 homeassistant)
adduser pi pulse-access
adduser homeassistant pulse-access
3) Pulse Audio에 Bluetooth 검색 추가
/etc/pulse/system.pa 에 다음을 파일 맨 아래에 추가합니다.
### 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
마지막 부분은 블루투스가 다른 프로필로 기본 설정된 경우를 대비해 a2dp 설정을 유지하는 것입니다. 불안정해 보여서 주석 처리했습니다.
오디오가 끊기는 경우 이 줄의 주석 처리를 해제하세요.
### Automatically suspend sinks/sources that become idle for too long
#load-module module-suspend-on-idle
4) 시작 시 Pulse Audio를 실행하는 서비스를 만듭니다.
파일을 만들고 /etc/systemd/system/pulseaudio.service다음을 추가합니다.
[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
부팅할 때 서비스가 시작되도록 설정합니다.
systemctl daemon-reload
systemctl enable pulseaudio.service
Pulse 사용자에게 블루투스 인터페이스 액세스 권한 부여
편집하다/etc/dbus-1/system.d/bluetooth.conf
다음 줄을 추가합니다:
<policy user="pulse">
<allow send_destination="org.bluez"/>
<allow send_interface="org.bluez.MediaEndpoint1"/>
</policy>
5) 시작 시 Bluetooth 스피커를 페어링하기 위한 스크립트 생성
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
파일을 만들고 /var/hass/scripts/pair_bluetooth.sh 에 다음을 추가합니다. Bluetooth 주소를 Bluetooth 스피커의 주소로 바꿔야 합니다.
#!/bin/bash
bluetoothctl << EOF
connect 00:2F:AD:12:0D:42
EOF
스크립트에 대한 실행 권한을 부여하세요.
chmod a+x /var/hass/scripts/pair_bluetooth.sh
에서 /etc/rc.local 시작 시 스크립트를 실행하려면 다음을 파일 끝에 추가하세요.
# Pair Bluetooth devices
/var/hass/scripts/pair_bluetooth.sh
exit 0
6) HA에 TTS 블루투스 스피커 추가
TTS Bluetooth 스피커 구성 요소를 복사(이 GitHub 저장소에서)하여 Home Assistant 구성 디렉토리에 저장합니다.
custom_components/tts_bluetooth_speaker/media_player.py
7) 선택 사항 - HA에 (새로운) Bluetooth 추적기 추가
이 단계는 Bluetooth 추적기를 사용하는 경우에만 적용됩니다.
Bluetooth Tracker 구성 요소를 복사하여 Home Assistant 구성 디렉토리에 저장합니다.
custom_components/bluetooth_tracker/device_tracker.py
8) 오디오 싱크가 사용 가능한지 확인
pactl list sinks
다음과 같은 내용이 표시되어야 합니다.
Sink #1
State: SUSPENDED
Name: bluez_sink.00_2F_AD_12_0D_42.a2dp_sink
대신 headset_head_unit이라고 적혀있는 경우 다음과 같이 a2dp 프로필로 전환할 수 있습니다.
pactl set-card-profile bluez_card.00_2F_AD_12_0D_42 a2dp_sink
다시 확인하고 a2dp를 사용하는지 확인하세요.
명령줄을 사용하여 mplayer가 a2dp로 스트리밍할 수 있는지 테스트합니다.
mplayer -ao pulse::bluez_sink.00_2F_AD_12_0D_42.a2dp_sink -channels 2 -volume 100 /some/mp3file.mp3
9) HA에서 사용을 시작하세요
이 단계(리부팅 후)에는 HA에서 TTS Bluetooth 스피커를 사용할 수 있어야 합니다.
아래는 구성 요소가 구성된 방법의 예입니다. 스피커의 Bluetooth 주소를 지정해야 하며, 선택적으로 volume레벨을 설정해야 합니다(0~1 사이여야 함). 스피커가 오디오의 첫 번째 부분을 재생하지 않는 경우(즉, 재생 시 첫 번째 두 번째가 누락된 경우) 선택적으로 원래 TTS 오디오 hsing 및 pre_silence_duration옵션 post_silence_duration(0~60초 사이여야 함)을 사용하여 일부 무음을 추가할 수 있습니다. TTS 캐시 디렉토리(TTS 구성에서)를 변경한 경우 cache_dir여기를 일치하도록 설정해야 합니다.
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
Bluetooth 추적기를 사용하고 있다면 아마도 이미 설정에 다음이 포함되어 있을 것입니다.
device_tracker:
- platform: bluetooth_tracker
모든 것이 제대로 작동하는지 테스트하려면 HA 프런트엔드에서 개발자 도구 > 서비스를 사용하여 Bluetooth 스피커를 통해 TTS 메시지를 재생할 수 있습니다.
{ "entity_id": "media_player.tts_bluetooth_speaker", "message": "Hello" }
이를 테스트하는 또 다른 방법은 HA가 시작될 때마다 TTS 메시지를 재생하는 자동화를 추가하는 것입니다.
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'