Allow TTS streams to generate temporary media source IDs (#145080)

* Allow TTS streams to generate temporary media source IDs

* Update tests/components/tts/test_media_source.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update assist snapshots

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Paulus Schoutsen
2025-05-19 12:04:19 -04:00
committed by GitHub
co-authored by Copilot
parent cadbe885d1
commit e09dde2ea9
8 changed files with 71 additions and 37 deletions
@@ -84,7 +84,7 @@
dict({
'data': dict({
'tts_output': dict({
'media_id': "media-source://tts/tts.test?message=Sorry,+I+couldn't+understand+that&language=en_US&tts_options=%7B%7D",
'media_id': 'media-source://tts/-stream-/test_token.mp3',
'mime_type': 'audio/mpeg',
'token': 'test_token.mp3',
'url': '/api/tts_proxy/test_token.mp3',
@@ -183,7 +183,7 @@
dict({
'data': dict({
'tts_output': dict({
'media_id': "media-source://tts/test?message=Sorry,+I+couldn't+understand+that&language=en-US&tts_options=%7B%22voice%22:%22Arnold+Schwarzenegger%22%7D",
'media_id': 'media-source://tts/-stream-/test_token.mp3',
'mime_type': 'audio/mpeg',
'token': 'test_token.mp3',
'url': '/api/tts_proxy/test_token.mp3',
@@ -282,7 +282,7 @@
dict({
'data': dict({
'tts_output': dict({
'media_id': "media-source://tts/test?message=Sorry,+I+couldn't+understand+that&language=en-US&tts_options=%7B%22voice%22:%22Arnold+Schwarzenegger%22%7D",
'media_id': 'media-source://tts/-stream-/test_token.mp3',
'mime_type': 'audio/mpeg',
'token': 'test_token.mp3',
'url': '/api/tts_proxy/test_token.mp3',
@@ -405,7 +405,7 @@
dict({
'data': dict({
'tts_output': dict({
'media_id': "media-source://tts/tts.test?message=Sorry,+I+couldn't+understand+that&language=en_US&tts_options=%7B%7D",
'media_id': 'media-source://tts/-stream-/test_token.mp3',
'mime_type': 'audio/mpeg',
'token': 'test_token.mp3',
'url': '/api/tts_proxy/test_token.mp3',
@@ -139,7 +139,7 @@
dict({
'data': dict({
'tts_output': dict({
'media_id': 'media-source://tts/tts.test?message=hello,+how+are+you?&language=en_US&tts_options=%7B%7D',
'media_id': 'media-source://tts/-stream-/mocked-token.mp3',
'mime_type': 'audio/mpeg',
'token': 'mocked-token.mp3',
'url': '/api/tts_proxy/mocked-token.mp3',
@@ -80,7 +80,7 @@
# name: test_audio_pipeline.6
dict({
'tts_output': dict({
'media_id': "media-source://tts/tts.test?message=Sorry,+I+couldn't+understand+that&language=en_US&tts_options=%7B%7D",
'media_id': 'media-source://tts/-stream-/test_token.mp3',
'mime_type': 'audio/mpeg',
'token': 'test_token.mp3',
'url': '/api/tts_proxy/test_token.mp3',
@@ -171,7 +171,7 @@
# name: test_audio_pipeline_debug.6
dict({
'tts_output': dict({
'media_id': "media-source://tts/tts.test?message=Sorry,+I+couldn't+understand+that&language=en_US&tts_options=%7B%7D",
'media_id': 'media-source://tts/-stream-/test_token.mp3',
'mime_type': 'audio/mpeg',
'token': 'test_token.mp3',
'url': '/api/tts_proxy/test_token.mp3',
@@ -274,7 +274,7 @@
# name: test_audio_pipeline_with_enhancements.6
dict({
'tts_output': dict({
'media_id': "media-source://tts/tts.test?message=Sorry,+I+couldn't+understand+that&language=en_US&tts_options=%7B%7D",
'media_id': 'media-source://tts/-stream-/test_token.mp3',
'mime_type': 'audio/mpeg',
'token': 'test_token.mp3',
'url': '/api/tts_proxy/test_token.mp3',
@@ -387,7 +387,7 @@
# name: test_audio_pipeline_with_wake_word_no_timeout.8
dict({
'tts_output': dict({
'media_id': "media-source://tts/tts.test?message=Sorry,+I+couldn't+understand+that&language=en_US&tts_options=%7B%7D",
'media_id': 'media-source://tts/-stream-/test_token.mp3',
'mime_type': 'audio/mpeg',
'token': 'test_token.mp3',
'url': '/api/tts_proxy/test_token.mp3',
+12
View File
@@ -17,6 +17,7 @@ from homeassistant.setup import async_setup_component
from .common import (
DEFAULT_LANG,
MockResultStream,
MockTTSEntity,
MockTTSProvider,
mock_config_entry_setup,
@@ -198,6 +199,17 @@ async def test_resolving(
assert language == "de_DE"
assert mock_get_tts_audio.mock_calls[0][2]["options"] == {"voice": "Paulus"}
# Test with result stream
stream = MockResultStream(hass, "wav", b"")
media = await media_source.async_resolve_media(hass, stream.media_source_id, None)
assert media.url == stream.url
assert media.mime_type == stream.content_type
with pytest.raises(media_source.Unresolvable):
await media_source.async_resolve_media(
hass, "media-source://tts/-stream-/not-a-valid-token", None
)
@pytest.mark.parametrize(
("mock_provider", "mock_tts_entity"),