Skip TTS events entirely with empty text (#105617)

This commit is contained in:
Michael Hansen 2023-12-12 23:21:16 -06:00 committed by GitHub
parent 431a44ab67
commit a73e86a741
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 40 deletions

View file

@ -369,6 +369,7 @@ class PipelineStage(StrEnum):
STT = "stt" STT = "stt"
INTENT = "intent" INTENT = "intent"
TTS = "tts" TTS = "tts"
END = "end"
PIPELINE_STAGE_ORDER = [ PIPELINE_STAGE_ORDER = [
@ -1024,35 +1025,32 @@ class PipelineRun:
) )
) )
if tts_input := tts_input.strip(): try:
try: # Synthesize audio and get URL
# Synthesize audio and get URL tts_media_id = tts_generate_media_source_id(
tts_media_id = tts_generate_media_source_id( self.hass,
self.hass, tts_input,
tts_input, engine=self.tts_engine,
engine=self.tts_engine, language=self.pipeline.tts_language,
language=self.pipeline.tts_language, options=self.tts_options,
options=self.tts_options, )
) tts_media = await media_source.async_resolve_media(
tts_media = await media_source.async_resolve_media( self.hass,
self.hass, tts_media_id,
tts_media_id, None,
None, )
) except Exception as src_error:
except Exception as src_error: _LOGGER.exception("Unexpected error during text-to-speech")
_LOGGER.exception("Unexpected error during text-to-speech") raise TextToSpeechError(
raise TextToSpeechError( code="tts-failed",
code="tts-failed", message="Unexpected error during text-to-speech",
message="Unexpected error during text-to-speech", ) from src_error
) from src_error
_LOGGER.debug("TTS result %s", tts_media) _LOGGER.debug("TTS result %s", tts_media)
tts_output = { tts_output = {
"media_id": tts_media_id, "media_id": tts_media_id,
**asdict(tts_media), **asdict(tts_media),
} }
else:
tts_output = {}
self.process_event( self.process_event(
PipelineEvent(PipelineEventType.TTS_END, {"tts_output": tts_output}) PipelineEvent(PipelineEventType.TTS_END, {"tts_output": tts_output})
@ -1345,7 +1343,11 @@ class PipelineInput:
self.conversation_id, self.conversation_id,
self.device_id, self.device_id,
) )
current_stage = PipelineStage.TTS if tts_input.strip():
current_stage = PipelineStage.TTS
else:
# Skip TTS
current_stage = PipelineStage.END
if self.run.end_stage != PipelineStage.INTENT: if self.run.end_stage != PipelineStage.INTENT:
# text-to-speech # text-to-speech

View file

@ -662,15 +662,33 @@
# --- # ---
# name: test_pipeline_empty_tts_output.1 # name: test_pipeline_empty_tts_output.1
dict({ dict({
'engine': 'test', 'conversation_id': None,
'language': 'en-US', 'device_id': None,
'tts_input': '', 'engine': 'homeassistant',
'voice': 'james_earl_jones', 'intent_input': 'never mind',
'language': 'en',
}) })
# --- # ---
# name: test_pipeline_empty_tts_output.2 # name: test_pipeline_empty_tts_output.2
dict({ dict({
'tts_output': dict({ 'intent_output': dict({
'conversation_id': None,
'response': dict({
'card': dict({
}),
'data': dict({
'failed': list([
]),
'success': list([
]),
'targets': list([
]),
}),
'language': 'en',
'response_type': 'action_done',
'speech': dict({
}),
}),
}), }),
}) })
# --- # ---

View file

@ -2467,10 +2467,10 @@ async def test_pipeline_empty_tts_output(
await client.send_json_auto_id( await client.send_json_auto_id(
{ {
"type": "assist_pipeline/run", "type": "assist_pipeline/run",
"start_stage": "tts", "start_stage": "intent",
"end_stage": "tts", "end_stage": "tts",
"input": { "input": {
"text": "", "text": "never mind",
}, },
} }
) )
@ -2486,16 +2486,15 @@ async def test_pipeline_empty_tts_output(
assert msg["event"]["data"] == snapshot assert msg["event"]["data"] == snapshot
events.append(msg["event"]) events.append(msg["event"])
# text-to-speech # intent
msg = await client.receive_json() msg = await client.receive_json()
assert msg["event"]["type"] == "tts-start" assert msg["event"]["type"] == "intent-start"
assert msg["event"]["data"] == snapshot assert msg["event"]["data"] == snapshot
events.append(msg["event"]) events.append(msg["event"])
msg = await client.receive_json() msg = await client.receive_json()
assert msg["event"]["type"] == "tts-end" assert msg["event"]["type"] == "intent-end"
assert msg["event"]["data"] == snapshot assert msg["event"]["data"] == snapshot
assert not msg["event"]["data"]["tts_output"]
events.append(msg["event"]) events.append(msg["event"])
# run end # run end