From 1ff201cf66dc5c03152bad5533be82ed792a3922 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Fri, 30 Oct 2026 10:53:32 -0400 Subject: [PATCH] Check each ESPHome announcement format value before using it (#187047) Co-authored-by: Claude --- .../components/esphome/assist_satellite.py | 4 +- .../esphome/test_assist_satellite.py | 46 +++++++++++++++---- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/esphome/assist_satellite.py b/homeassistant/components/esphome/assist_satellite.py index 03f1c56383bb..6e42fbd0b09e 100644 --- a/homeassistant/components/esphome/assist_satellite.py +++ b/homeassistant/components/esphome/assist_satellite.py @@ -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 ) diff --git a/tests/components/esphome/test_assist_satellite.py b/tests/components/esphome/test_assist_satellite.py index e428a463e68d..f2e443f54174 100644 --- a/tests/components/esphome/test_assist_satellite.py +++ b/tests/components/esphome/test_assist_satellite.py @@ -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(