voiceOnly
This commit is contained in:
@@ -0,0 +1,244 @@
|
||||
# Shared voice assistant service (HA-side wake word via openWakeWord).
|
||||
# Required device substitutions: WS, SCK, SD (full pin names, e.g. GPIO2),
|
||||
# SPKR (HA media_player for replies), listenTimeout (max command length, e.g. 5s)
|
||||
# Optional microphone tuning, defaults below; override in the device file.
|
||||
substitutions:
|
||||
MIC_BITS: 16bit # 16bit or 32bit, depends on the mic module
|
||||
MIC_DC_OFFSET: "true" # remove DC offset from the mic signal
|
||||
MIC_GAIN: "4" # gain_factor applied before sending audio to HA
|
||||
MIC_AUTO_GAIN: 31dBFS # target level for automatic gain (0 dBFS disables)
|
||||
|
||||
globals:
|
||||
# Phase of the voice assistant: 1 = idle, 2 = listening, 3 = processing, 4 = disconnected/error
|
||||
- id: voice_assistant_phase
|
||||
type: int
|
||||
restore_value: no
|
||||
initial_value: "1"
|
||||
- id: stt_vad_started
|
||||
type: bool
|
||||
restore_value: no
|
||||
initial_value: "false"
|
||||
|
||||
i2s_audio:
|
||||
- id: i2s_in
|
||||
i2s_lrclk_pin: ${WS} #WS
|
||||
i2s_bclk_pin: ${SCK} #SCK
|
||||
|
||||
microphone:
|
||||
- platform: i2s_audio
|
||||
id: mic_id
|
||||
adc_type: external
|
||||
i2s_audio_id: i2s_in
|
||||
i2s_din_pin: ${SD} #SD
|
||||
channel: right
|
||||
sample_rate: 16000
|
||||
bits_per_sample: ${MIC_BITS}
|
||||
correct_dc_offset: ${MIC_DC_OFFSET}
|
||||
|
||||
########################################################################
|
||||
voice_assistant:
|
||||
id: va
|
||||
microphone:
|
||||
microphone: mic_id
|
||||
channels: 0
|
||||
gain_factor: ${MIC_GAIN}
|
||||
noise_suppression_level: 2
|
||||
auto_gain: ${MIC_AUTO_GAIN}
|
||||
|
||||
on_start:
|
||||
|
||||
- light.turn_on:
|
||||
id: led
|
||||
red: 0%
|
||||
green: 100%
|
||||
blue: 0%
|
||||
brightness: 15%
|
||||
effect: none
|
||||
|
||||
|
||||
on_listening:
|
||||
- light.turn_on:
|
||||
id: led
|
||||
blue: 100%
|
||||
red: 0%
|
||||
green: 0%
|
||||
effect: none
|
||||
- lambda: id(voice_assistant_phase) = 2;
|
||||
- lambda: id(stt_vad_started) = false;
|
||||
# Wake word detected, command recording starts -> start the max-length timer
|
||||
- script.execute: listening_timeout
|
||||
|
||||
on_stt_vad_start:
|
||||
# Turn off the script if speech is detected
|
||||
- lambda: id(stt_vad_started) = true;
|
||||
|
||||
on_stt_vad_end:
|
||||
- lambda: id(voice_assistant_phase) = 3; # recording finished, HA is processing
|
||||
- script.stop: listening_timeout
|
||||
#- logger.log: "===============================VAD is the END =================================="
|
||||
- light.turn_on:
|
||||
id: led
|
||||
blue: 100%
|
||||
red: 0%
|
||||
green: 100%
|
||||
effect: none
|
||||
|
||||
on_stt_end:
|
||||
- lambda: id(voice_assistant_phase) = 3;
|
||||
- script.stop: listening_timeout
|
||||
- logger.log:
|
||||
format: "STT text: %s"
|
||||
args: ["x.c_str()"]
|
||||
|
||||
on_tts_start:
|
||||
then:
|
||||
- light.turn_on:
|
||||
id: led
|
||||
blue: 100%
|
||||
red: 100%
|
||||
green: 100%
|
||||
brightness: 30%
|
||||
effect: none
|
||||
|
||||
on_tts_end:
|
||||
- homeassistant.action:
|
||||
action: media_player.play_media
|
||||
data:
|
||||
entity_id: ${SPKR}
|
||||
media_content_id: !lambda "return x;"
|
||||
media_content_type: music
|
||||
announce: "true"
|
||||
|
||||
on_end:
|
||||
# No local speaker/media_player, so ESPHome never reports playback as finished
|
||||
# and HA keeps the satellite in "responding". Report it ourselves.
|
||||
- lambda: |-
|
||||
auto *conn = id(va).get_api_connection();
|
||||
if (conn != nullptr) {
|
||||
api::VoiceAssistantAnnounceFinished msg;
|
||||
msg.success = true;
|
||||
if (!conn->send_message(msg)) {
|
||||
ESP_LOGW("va", "Could not send announce-finished to HA");
|
||||
}
|
||||
}
|
||||
- lambda: id(voice_assistant_phase) = 1;
|
||||
- lambda: id(stt_vad_started) = false;
|
||||
- script.execute: start_wake_word
|
||||
|
||||
|
||||
on_error:
|
||||
- if:
|
||||
condition:
|
||||
lambda: |-
|
||||
return message.find("duplicate_wake_up_detected") != std::string::npos;
|
||||
then:
|
||||
- voice_assistant.stop:
|
||||
- delay: 350ms
|
||||
- script.execute: start_wake_word
|
||||
else:
|
||||
- lambda: id(voice_assistant_phase) = 4;
|
||||
- light.turn_on:
|
||||
id: led
|
||||
red: 100%
|
||||
green: 0%
|
||||
blue: 0%
|
||||
brightness: 100%
|
||||
effect: none
|
||||
- delay: 2s
|
||||
- script.execute: rboot
|
||||
#- script.execute: reset_led
|
||||
on_client_connected:
|
||||
- lambda: id(voice_assistant_phase) = 1; # connected again: clear "disconnected" state
|
||||
- script.stop: rboot # cancel the pending reboot
|
||||
- delay: 1s # Give the api server time to settle
|
||||
- script.execute: start_wake_word
|
||||
on_client_disconnected:
|
||||
- lambda: id(voice_assistant_phase) = 4;
|
||||
- script.execute: stop_wake_word
|
||||
- script.execute: rboot
|
||||
|
||||
interval:
|
||||
- interval: 30s
|
||||
then:
|
||||
- if:
|
||||
condition:
|
||||
and:
|
||||
- api.connected:
|
||||
- not:
|
||||
- voice_assistant.is_running:
|
||||
- switch.is_off: stop_listen
|
||||
then:
|
||||
- script.execute: start_wake_word
|
||||
|
||||
script:
|
||||
- id: rboot
|
||||
mode: restart
|
||||
then:
|
||||
- delay: 180s
|
||||
- if:
|
||||
condition:
|
||||
and:
|
||||
- lambda: 'return id(voice_assistant_phase) == 4;'
|
||||
- not:
|
||||
- api.connected: # only reboot if HA is still not connected
|
||||
then:
|
||||
- switch.turn_on:
|
||||
id: rbt
|
||||
- id: listening_timeout
|
||||
mode: restart
|
||||
then:
|
||||
- delay: ${listenTimeout}
|
||||
# - logger.log:
|
||||
# format: "In Script >>>> C >>>> voice_assistant_phase=%d "
|
||||
# args: [ 'id(voice_assistant_phase)' ]
|
||||
- if:
|
||||
condition:
|
||||
lambda: |-
|
||||
return id(voice_assistant_phase) == 2;
|
||||
then:
|
||||
- if:
|
||||
condition:
|
||||
lambda: 'return id(stt_vad_started);'
|
||||
then:
|
||||
# Speech is still going after the max time: stop the mic.
|
||||
# HA processes the audio received so far; on_end restarts the wake word.
|
||||
- logger.log: "Max listen time reached, stopping audio stream"
|
||||
- lambda: id(voice_assistant_phase) = 3;
|
||||
- voice_assistant.stop:
|
||||
else:
|
||||
# No speech at all: abort and return to wake word listening
|
||||
- logger.log: "No speech detected, back to wake word"
|
||||
- light.turn_off:
|
||||
id: led
|
||||
- voice_assistant.stop:
|
||||
- delay: 1s
|
||||
- script.execute: start_wake_word
|
||||
|
||||
- id: start_wake_word
|
||||
then:
|
||||
- if:
|
||||
condition:
|
||||
- not:
|
||||
- voice_assistant.is_running:
|
||||
- switch.is_off: stop_listen
|
||||
then:
|
||||
- lambda: id(va).set_use_wake_word(true);
|
||||
- voice_assistant.start_continuous:
|
||||
|
||||
- id: stop_wake_word
|
||||
then:
|
||||
- lambda: id(va).set_use_wake_word(false);
|
||||
- voice_assistant.stop:
|
||||
switch:
|
||||
- platform: template
|
||||
name: Stop Listen
|
||||
id: stop_listen
|
||||
optimistic: true
|
||||
restore_mode: RESTORE_DEFAULT_OFF
|
||||
#entity_category: config
|
||||
on_turn_on:
|
||||
# Stop streaming to HA completely instead of sending silence
|
||||
- script.execute: stop_wake_word
|
||||
- light.turn_off: led
|
||||
on_turn_off:
|
||||
- script.execute: start_wake_word
|
||||
Reference in New Issue
Block a user