diff --git a/homeassistant/components/wyoming/assist_satellite.py b/homeassistant/components/wyoming/assist_satellite.py index 1910a94f7608..6d5e86e75884 100644 --- a/homeassistant/components/wyoming/assist_satellite.py +++ b/homeassistant/components/wyoming/assist_satellite.py @@ -112,6 +112,7 @@ class WyomingAssistSatellite(WyomingSatelliteEntity, AssistSatelliteEntity): self._chunk_converter = AudioChunkConverter(rate=16000, width=2, channels=1) self._is_pipeline_running = False self._pipeline_error = False + self._wake_word_detected = False self._pipeline_ended_event = asyncio.Event() self._audio_queue: asyncio.Queue[bytes | None] = asyncio.Queue() self._pipeline_id: str | None = None @@ -220,6 +221,7 @@ class WyomingAssistSatellite(WyomingSatelliteEntity, AssistSatelliteEntity): # Wake word detection # Inform client of wake word detection if event.data and (wake_word_output := event.data.get("wake_word_output")): + self._wake_word_detected = True detection = Detection( name=wake_word_output["wake_word_id"], timestamp=wake_word_output.get("timestamp"), @@ -318,9 +320,11 @@ class WyomingAssistSatellite(WyomingSatelliteEntity, AssistSatelliteEntity): f"{self.entity_id} {event.type}", ) elif event.type == assist_pipeline.PipelineEventType.ERROR: - # Pipeline error. Prevents an "always on" satellite from restarting - # a failing pipeline in a tight loop (e.g. on a config error). - self._pipeline_error = True + # Prevents an "always on" satellite from restarting a failing + # pipeline in a tight loop (e.g. on a config error). After a wake + # word, the next run is gated by the user speaking it again. + if not self._wake_word_detected: + self._pipeline_error = True if event.data: self.config_entry.async_create_background_task( self.hass, @@ -627,16 +631,6 @@ class WyomingAssistSatellite(WyomingSatelliteEntity, AssistSatelliteEntity): # Clear last wake word detection wake_word_phrase = None - if ( - (run_pipeline is not None) - and run_pipeline.restart_on_end - and not self._pipeline_error - ): - # Automatically restart pipeline. - # Used with "always on" streaming satellites. - self._run_pipeline_once(run_pipeline) - continue - if self._pipeline_error: # Don't restart a failing pipeline in a tight loop; wait # for the satellite to request a new run. @@ -644,6 +638,10 @@ class WyomingAssistSatellite(WyomingSatelliteEntity, AssistSatelliteEntity): "Not restarting pipeline after error; " "waiting for next satellite request" ) + elif (run_pipeline is not None) and run_pipeline.restart_on_end: + # Automatically restart pipeline. + # Used with "always on" streaming satellites. + self._run_pipeline_once(run_pipeline) if client_event_task not in done: continue @@ -753,6 +751,7 @@ class WyomingAssistSatellite(WyomingSatelliteEntity, AssistSatelliteEntity): self._is_pipeline_running = True self._pipeline_error = False + self._wake_word_detected = False self._pipeline_ended_event.clear() self.config_entry.async_create_background_task( self.hass, diff --git a/tests/components/wyoming/test_satellite.py b/tests/components/wyoming/test_satellite.py index 3a28720586b8..84d79cd60b80 100644 --- a/tests/components/wyoming/test_satellite.py +++ b/tests/components/wyoming/test_satellite.py @@ -546,6 +546,109 @@ async def test_satellite_pipeline_error_no_restart(hass: HomeAssistant) -> None: await hass.async_block_till_done() +async def test_satellite_pipeline_error_after_wake_word_restarts( + hass: HomeAssistant, +) -> None: + """Test that an error after a wake word still auto-restarts an "always on" satellite.""" + assert await async_setup_component(hass, assist_pipeline.DOMAIN, {}) + + events = [ + RunPipeline( + start_stage=PipelineStage.WAKE, + end_stage=PipelineStage.TTS, + restart_on_end=True, + ).event(), + ] + + pipeline_event_callback: Callable[[assist_pipeline.PipelineEvent], None] | None = ( + None + ) + run_pipeline_called = asyncio.Event() + + async def async_pipeline_from_audio_stream( + hass: HomeAssistant, + context, + event_callback, + stt_metadata, + stt_stream, + **kwargs, + ) -> None: + nonlocal pipeline_event_callback + pipeline_event_callback = event_callback + run_pipeline_called.set() + + with ( + patch( + "homeassistant.components.wyoming.data.load_wyoming_info", + return_value=SATELLITE_INFO, + ), + patch( + "homeassistant.components.wyoming.assist_satellite.AsyncTcpClient", + SatelliteAsyncTcpClient(events), + ) as mock_client, + patch( + "homeassistant.components.assist_satellite.entity.async_pipeline_from_audio_stream", + async_pipeline_from_audio_stream, + ), + patch("homeassistant.components.wyoming.assist_satellite._PING_SEND_DELAY", 0), + ): + entry = await setup_config_entry(hass) + + async with asyncio.timeout(1): + await mock_client.connect_event.wait() + await mock_client.run_satellite_event.wait() + await run_pipeline_called.wait() + + assert pipeline_event_callback is not None + run_pipeline_called.clear() + + # Wake word is detected, but the user doesn't say anything + pipeline_event_callback( + assist_pipeline.PipelineEvent( + assist_pipeline.PipelineEventType.WAKE_WORD_END, + {"wake_word_output": {"wake_word_id": "test_wake_word"}}, + ) + ) + pipeline_event_callback( + assist_pipeline.PipelineEvent( + assist_pipeline.PipelineEventType.ERROR, + {"code": "stt-no-text-recognized", "message": "No text recognized"}, + ) + ) + pipeline_event_callback( + assist_pipeline.PipelineEvent(assist_pipeline.PipelineEventType.RUN_END) + ) + + # Pipeline should automatically restart to listen for the wake word again + async with asyncio.timeout(1): + await run_pipeline_called.wait() + + run_pipeline_called.clear() + + # The wake word detection doesn't carry over into the restarted run, so an + # error before the next wake word must not restart it + pipeline_event_callback( + assist_pipeline.PipelineEvent( + assist_pipeline.PipelineEventType.ERROR, + {"code": "test-code", "message": "test-message"}, + ) + ) + pipeline_event_callback( + assist_pipeline.PipelineEvent(assist_pipeline.PipelineEventType.RUN_END) + ) + + # A ping/pong round trip guarantees the loop has processed the run end + mock_client.inject_event(Ping("test-ping").event()) + async with asyncio.timeout(1): + await mock_client.pong_event.wait() + + assert not run_pipeline_called.is_set() + + # Stop the satellite + await hass.config_entries.async_unload(entry.entry_id) + await hass.async_block_till_done() + + async def test_satellite_muted(hass: HomeAssistant) -> None: """Test callback for a satellite that has been muted.""" on_muted_event = asyncio.Event()