diff --git a/homeassistant/components/esphome/voice_assistant.py b/homeassistant/components/esphome/voice_assistant.py index 94ba957e6f7..15b580a0601 100644 --- a/homeassistant/components/esphome/voice_assistant.py +++ b/homeassistant/components/esphome/voice_assistant.py @@ -160,10 +160,10 @@ class VoiceAssistantUDPServer(asyncio.DatagramProtocol): async def _iterate_packets(self) -> AsyncIterable[bytes]: """Iterate over incoming packets.""" - if not self.is_running: - raise RuntimeError("Not running") - while data := await self.queue.get(): + if not self.is_running: + break + yield data def _event_callback(self, event: PipelineEvent) -> None: diff --git a/tests/components/esphome/test_voice_assistant.py b/tests/components/esphome/test_voice_assistant.py index f6665c4ad91..427cd1dbc8f 100644 --- a/tests/components/esphome/test_voice_assistant.py +++ b/tests/components/esphome/test_voice_assistant.py @@ -224,9 +224,13 @@ async def test_udp_server_queue( voice_assistant_udp_server_v1.close() - with pytest.raises(RuntimeError): - async for data in voice_assistant_udp_server_v1._iterate_packets(): - assert data == bytes(1024) + # Stopping the UDP server should cause _iterate_packets to break out + # immediately without yielding any data. + has_data = False + async for _data in voice_assistant_udp_server_v1._iterate_packets(): + has_data = True + + assert not has_data, "Server was stopped" async def test_error_calls_handle_finished(