From 0f784f93d94155c11f80bf40f64889345de77570 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 25 Jun 2023 22:57:02 -0400 Subject: [PATCH] Mobile app to use Assist Pipeline --- .../components/mobile_app/manifest.json | 9 ++++- .../components/mobile_app/webhook.py | 35 ++++++++++++++----- tests/components/mobile_app/test_webhook.py | 20 ++++++----- 3 files changed, 47 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/mobile_app/manifest.json b/homeassistant/components/mobile_app/manifest.json index aeab576a7cd..6e870260f5f 100644 --- a/homeassistant/components/mobile_app/manifest.json +++ b/homeassistant/components/mobile_app/manifest.json @@ -4,7 +4,14 @@ "after_dependencies": ["cloud", "camera", "conversation", "notify"], "codeowners": ["@home-assistant/core"], "config_flow": true, - "dependencies": ["http", "webhook", "person", "tag", "websocket_api"], + "dependencies": [ + "assist_pipeline", + "http", + "person", + "tag", + "webhook", + "websocket_api" + ], "documentation": "https://www.home-assistant.io/integrations/mobile_app", "iot_class": "local_push", "loggers": ["nacl"], diff --git a/homeassistant/components/mobile_app/webhook.py b/homeassistant/components/mobile_app/webhook.py index 90e244aaf06..c46c6ea86f4 100644 --- a/homeassistant/components/mobile_app/webhook.py +++ b/homeassistant/components/mobile_app/webhook.py @@ -16,9 +16,9 @@ from nacl.secret import SecretBox import voluptuous as vol from homeassistant.components import ( + assist_pipeline, camera, cloud, - conversation, notify as hass_notify, tag, ) @@ -317,7 +317,7 @@ async def webhook_fire_event( @validate_schema( { vol.Required("text"): cv.string, - vol.Optional("language"): cv.string, + vol.Optional("pipeline_id"): cv.string, vol.Optional("conversation_id"): cv.string, } ) @@ -325,14 +325,33 @@ async def webhook_conversation_process( hass: HomeAssistant, config_entry: ConfigEntry, data: dict[str, Any] ) -> Response: """Handle a conversation process webhook.""" - result = await conversation.async_converse( - hass, - text=data["text"], - language=data.get("language"), - conversation_id=data.get("conversation_id"), + result: dict | None = None + + def event_callback(event: assist_pipeline.PipelineEvent) -> None: + """Handle assist event.""" + nonlocal result + if event.type == assist_pipeline.PipelineEventType.INTENT_END: + assert event.data is not None + result = event.data["intent_output"] + + await assist_pipeline.async_pipeline_from_text( + hass=hass, context=registration_context(config_entry.data), + event_callback=event_callback, + intent_input=data["text"], + conversation_id=data.get("conversation_id"), + device_id=config_entry.data[ATTR_DEVICE_ID], + pipeline_id=data.get("pipeline_id"), ) - return webhook_response(result.as_dict(), registration=config_entry.data) + + if result is None: + return webhook_response( + {"success": False}, + registration=config_entry.data, + status=HTTPStatus.INTERNAL_SERVER_ERROR, + ) + + return webhook_response(result, registration=config_entry.data) @WEBHOOK_COMMANDS.register("stream_camera") diff --git a/tests/components/mobile_app/test_webhook.py b/tests/components/mobile_app/test_webhook.py index 02c9ace7cd4..ce1dc19319a 100644 --- a/tests/components/mobile_app/test_webhook.py +++ b/tests/components/mobile_app/test_webhook.py @@ -1026,15 +1026,19 @@ async def test_webhook_handle_conversation_process( """Test that we can converse.""" webhook_client.server.app.router._frozen = False - resp = await webhook_client.post( - "/api/webhook/{}".format(create_registrations[1]["webhook_id"]), - json={ - "type": "conversation_process", - "data": { - "text": "Turn the kitchen light off", + with patch( + "homeassistant.components.conversation.AgentManager.async_get_agent", + return_value=mock_agent, + ): + resp = await webhook_client.post( + "/api/webhook/{}".format(create_registrations[1]["webhook_id"]), + json={ + "type": "conversation_process", + "data": { + "text": "Turn the kitchen light off", + }, }, - }, - ) + ) assert resp.status == HTTPStatus.OK json = await resp.json()