diff --git a/homeassistant/components/assist_pipeline/pipeline.py b/homeassistant/components/assist_pipeline/pipeline.py index d90424d52d3..96beaf792a7 100644 --- a/homeassistant/components/assist_pipeline/pipeline.py +++ b/homeassistant/components/assist_pipeline/pipeline.py @@ -1032,39 +1032,37 @@ class PipelineRun: agent_id=self.intent_agent, ) - # Sentence triggers override conversation agent - if ( - trigger_response_text - := await conversation.async_handle_sentence_triggers( - self.hass, user_input - ) - ): - # Sentence trigger matched - trigger_response = intent.IntentResponse( - self.pipeline.conversation_language - ) - trigger_response.async_set_speech(trigger_response_text) - conversation_result = conversation.ConversationResult( - response=trigger_response, - conversation_id=user_input.conversation_id, - ) - # Try local intents first, if preferred. - # Skip this step if the default agent is already used. - elif ( - self.pipeline.prefer_local_intents - and (user_input.agent_id != conversation.HOME_ASSISTANT_AGENT) - and ( + conversation_result: conversation.ConversationResult | None = None + if user_input.agent_id != conversation.HOME_ASSISTANT_AGENT: + # Sentence triggers override conversation agent + if ( + trigger_response_text + := await conversation.async_handle_sentence_triggers( + self.hass, user_input + ) + ): + # Sentence trigger matched + trigger_response = intent.IntentResponse( + self.pipeline.conversation_language + ) + trigger_response.async_set_speech(trigger_response_text) + conversation_result = conversation.ConversationResult( + response=trigger_response, + conversation_id=user_input.conversation_id, + ) + # Try local intents first, if preferred. + elif self.pipeline.prefer_local_intents and ( intent_response := await conversation.async_handle_intents( self.hass, user_input ) - ) - ): - # Local intent matched - conversation_result = conversation.ConversationResult( - response=intent_response, - conversation_id=user_input.conversation_id, - ) - else: + ): + # Local intent matched + conversation_result = conversation.ConversationResult( + response=intent_response, + conversation_id=user_input.conversation_id, + ) + + if conversation_result is None: # Fall back to pipeline conversation agent conversation_result = await conversation.async_converse( hass=self.hass, diff --git a/tests/components/assist_pipeline/test_init.py b/tests/components/assist_pipeline/test_init.py index bdca27d527f..a8ada11fb45 100644 --- a/tests/components/assist_pipeline/test_init.py +++ b/tests/components/assist_pipeline/test_init.py @@ -941,7 +941,7 @@ async def test_sentence_trigger_overrides_conversation_agent( init_components, pipeline_data: assist_pipeline.pipeline.PipelineData, ) -> None: - """Test that sentence triggers are checked before the conversation agent.""" + """Test that sentence triggers are checked before a non-default conversation agent.""" assert await async_setup_component( hass, "automation", @@ -975,9 +975,16 @@ async def test_sentence_trigger_overrides_conversation_agent( start_stage=assist_pipeline.PipelineStage.INTENT, end_stage=assist_pipeline.PipelineStage.INTENT, event_callback=events.append, + intent_agent="test-agent", # not the default agent ), ) - await pipeline_input.validate() + + # Ensure prepare succeeds + with patch( + "homeassistant.components.assist_pipeline.pipeline.conversation.async_get_agent_info", + return_value=conversation.AgentInfo(id="test-agent", name="Test Agent"), + ): + await pipeline_input.validate() with patch( "homeassistant.components.assist_pipeline.pipeline.conversation.async_converse"