From 96533e4c8f7f837c13642f3144b8e47330c9e315 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 22 Dec 2022 20:19:37 -0500 Subject: [PATCH] Test conversation WS API (#84466) --- .../components/conversation/__init__.py | 2 +- tests/components/conversation/test_init.py | 54 ++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/conversation/__init__.py b/homeassistant/components/conversation/__init__.py index 54b69a7ad52..b95b9361624 100644 --- a/homeassistant/components/conversation/__init__.py +++ b/homeassistant/components/conversation/__init__.py @@ -89,7 +89,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: { "type": "conversation/process", "text": str, - vol.Optional("conversation_id"): str, + vol.Optional("conversation_id"): vol.Any(str, None), vol.Optional("language"): str, } ) diff --git a/tests/components/conversation/test_init.py b/tests/components/conversation/test_init.py index f25bdd42466..c25df45ce78 100644 --- a/tests/components/conversation/test_init.py +++ b/tests/components/conversation/test_init.py @@ -1,6 +1,6 @@ """The tests for the Conversation component.""" from http import HTTPStatus -from unittest.mock import patch +from unittest.mock import ANY, patch import pytest @@ -481,3 +481,55 @@ async def test_custom_agent(hass, hass_client, hass_admin_user): assert calls[0][1].user_id == hass_admin_user.id assert calls[0][2] == "test-conv-id" assert calls[0][3] == "test-language" + + +@pytest.mark.parametrize( + "payload", + [ + { + "text": "Test Text", + }, + { + "text": "Test Text", + "language": "test-language", + }, + { + "text": "Test Text", + "conversation_id": "test-conv-id", + }, + { + "text": "Test Text", + "conversation_id": None, + }, + { + "text": "Test Text", + "conversation_id": "test-conv-id", + "language": "test-language", + }, + ], +) +async def test_ws_api(hass, hass_ws_client, payload): + """Test the Websocket conversation API.""" + assert await async_setup_component(hass, "conversation", {}) + client = await hass_ws_client(hass) + + await client.send_json({"id": 5, "type": "conversation/process", **payload}) + + msg = await client.receive_json() + + assert msg["success"] + assert msg["result"] == { + "response": { + "response_type": "error", + "card": {}, + "speech": { + "plain": { + "extra_data": None, + "speech": "Sorry, I didn't understand that", + } + }, + "language": payload.get("language", hass.config.language), + "data": {"code": "no_intent_match"}, + }, + "conversation_id": payload.get("conversation_id") or ANY, + }