From 74c9417581e75a2bc89caedd2970f6124e2e10ce Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Wed, 7 Oct 2026 08:00:37 +0200 Subject: [PATCH] Don't end the ESPHome voice audio stream on an empty frame (#184583) --- .../components/esphome/assist_satellite.py | 6 ++- .../esphome/test_assist_satellite.py | 45 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/esphome/assist_satellite.py b/homeassistant/components/esphome/assist_satellite.py index 94ad3438c1e2..c696fde999ae 100644 --- a/homeassistant/components/esphome/assist_satellite.py +++ b/homeassistant/components/esphome/assist_satellite.py @@ -782,9 +782,13 @@ class EsphomeAssistSatellite( """Yield audio chunks from the queue until None.""" while True: chunk = await self._audio_queue.get() - if not chunk: + if chunk is None: break + # The device can send an empty frame in the middle of the audio + if not chunk: + continue + yield chunk def _stop_pipeline(self) -> None: diff --git a/tests/components/esphome/test_assist_satellite.py b/tests/components/esphome/test_assist_satellite.py index 412ebcfd948f..6c93c8ec55c0 100644 --- a/tests/components/esphome/test_assist_satellite.py +++ b/tests/components/esphome/test_assist_satellite.py @@ -2369,6 +2369,51 @@ async def test_multichannel_audio( await pipeline_finished.wait() +async def test_empty_audio_frame_does_not_end_stream( + hass: HomeAssistant, + mock_client: APIClient, + mock_esphome_device: MockESPHomeDeviceType, +) -> None: + """Test an empty audio frame from the device doesn't end the audio stream.""" + mock_device = await mock_esphome_device( + mock_client=mock_client, + device_info={ + "voice_assistant_feature_flags": VoiceAssistantFeature.VOICE_ASSISTANT + | VoiceAssistantFeature.API_AUDIO + }, + ) + await hass.async_block_till_done() + + satellite = get_satellite_entity(hass, mock_device.device_info.mac_address) + assert satellite is not None + + received_chunks: list[bytes] = [] + pipeline_finished = asyncio.Event() + + async def async_pipeline_from_audio_stream(*args, **kwargs): + received_chunks.extend([chunk async for chunk in kwargs["stt_stream"]]) + pipeline_finished.set() + + with patch( + "homeassistant.components.assist_satellite.entity.async_pipeline_from_audio_stream", + new=async_pipeline_from_audio_stream, + ): + async with asyncio.timeout(1): + await satellite.handle_pipeline_start( + conversation_id="", + flags=VoiceAssistantCommandFlag(0), # stt + audio_settings=VoiceAssistantAudioSettings(), + wake_word_phrase=None, + ) + await satellite.handle_audio(b"before") + await satellite.handle_audio(b"") + await satellite.handle_audio(b"after") + await satellite.handle_pipeline_stop(abort=False) + await pipeline_finished.wait() + + assert received_chunks == [b"before", b"after"] + + async def test_multichannel_audio_fallback_channel_0( hass: HomeAssistant, mock_client: APIClient,