Check each ESPHome announcement format value before using it (#187047)

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Paulus Schoutsen
2026-10-30 15:53:32 +01:00
committed by GitHub
co-authored by Claude
parent 9483a65700
commit 1ff201cf66
2 changed files with 40 additions and 10 deletions
@@ -695,12 +695,12 @@ class EsphomeAssistSatellite(
supported_format.sample_rate
)
if supported_format.sample_rate > 0:
if supported_format.num_channels > 0:
self._attr_tts_options[tts.ATTR_PREFERRED_SAMPLE_CHANNELS] = (
supported_format.num_channels
)
if supported_format.sample_rate > 0:
if supported_format.sample_bytes > 0:
self._attr_tts_options[tts.ATTR_PREFERRED_SAMPLE_BYTES] = (
supported_format.sample_bytes
)
@@ -1019,12 +1019,43 @@ async def test_tts_format_from_media_player(
}
async def test_tts_minimal_format_from_media_player(
@pytest.mark.parametrize(
("sample_rate", "num_channels", "sample_bytes", "tts_audio_output"),
[
pytest.param(0, 0, 0, {tts.ATTR_PREFERRED_FORMAT: "mp3"}, id="codec_only"),
pytest.param(
48000,
0,
0,
{
tts.ATTR_PREFERRED_FORMAT: "mp3",
tts.ATTR_PREFERRED_SAMPLE_RATE: 48000,
},
id="rate_only",
),
pytest.param(
0,
1,
2,
{
tts.ATTR_PREFERRED_FORMAT: "mp3",
tts.ATTR_PREFERRED_SAMPLE_CHANNELS: 1,
tts.ATTR_PREFERRED_SAMPLE_BYTES: 2,
},
id="channels_and_width_only",
),
],
)
async def test_tts_partial_format_from_media_player(
hass: HomeAssistant,
mock_client: APIClient,
mock_esphome_device: MockESPHomeDeviceType,
sample_rate: int,
num_channels: int,
sample_bytes: int,
tts_audio_output: dict[str, str | int],
) -> None:
"""Test text-to-speech format when media player only specifies the codec."""
"""Test text-to-speech format when media player leaves some values unset."""
mock_device = await mock_esphome_device(
mock_client=mock_client,
entity_info=[
@@ -1042,12 +1073,13 @@ async def test_tts_minimal_format_from_media_player(
sample_bytes=2,
),
# This is the format that should be used for tts
# 0 means the value of the source audio
MediaPlayerSupportedFormat(
format="mp3",
sample_rate=0, # source rate
num_channels=0, # source channels
sample_rate=sample_rate,
num_channels=num_channels,
purpose=MediaPlayerFormatPurpose.ANNOUNCEMENT,
sample_bytes=0, # source width
sample_bytes=sample_bytes,
),
],
)
@@ -1077,9 +1109,7 @@ async def test_tts_minimal_format_from_media_player(
kwargs = mock_pipeline_from_audio_stream.call_args_list[0].kwargs
# Should be ANNOUNCEMENT format from media player
assert kwargs.get("tts_audio_output") == {
tts.ATTR_PREFERRED_FORMAT: "mp3",
}
assert kwargs.get("tts_audio_output") == tts_audio_output
async def test_announce_message(