Allow streaming text into TTS ResultStream objects (#143745)

Allow streaming messages into TTS ResultStream
This commit is contained in:
Paulus Schoutsen
2025-04-30 08:21:19 -04:00
committed by GitHub
parent ae118da5a1
commit 5dab9ba01b
2 changed files with 77 additions and 1 deletions

View File

@@ -1842,6 +1842,7 @@ async def test_default_engine_prefer_cloud_entity(
async def test_stream(hass: HomeAssistant, mock_tts_entity: MockTTSEntity) -> None:
"""Test creating streams."""
await mock_config_entry_setup(hass, mock_tts_entity)
stream = tts.async_create_stream(hass, mock_tts_entity.entity_id)
assert stream.language == mock_tts_entity.default_language
assert stream.options == (mock_tts_entity.default_options or {})
@@ -1850,6 +1851,33 @@ async def test_stream(hass: HomeAssistant, mock_tts_entity: MockTTSEntity) -> No
result_data = b"".join([chunk async for chunk in stream.async_stream_result()])
assert result_data == MOCK_DATA
async def async_stream_tts_audio(
request: tts.TTSAudioRequest,
) -> tts.TTSAudioResponse:
"""Mock stream TTS audio."""
async def gen_data():
async for msg in request.message_gen:
yield msg.encode()
return tts.TTSAudioResponse(
extension="mp3",
data_gen=gen_data(),
)
mock_tts_entity.async_stream_tts_audio = async_stream_tts_audio
async def stream_message():
"""Mock stream message."""
yield "he"
yield "ll"
yield "o"
stream = tts.async_create_stream(hass, mock_tts_entity.entity_id)
stream.async_set_message_stream(stream_message())
result_data = b"".join([chunk async for chunk in stream.async_stream_result()])
assert result_data == b"hello"
data = b"beer"
stream2 = MockResultStream(hass, "wav", data)
assert tts.async_get_stream(hass, stream2.token) is stream2