From e5c7ff6a5b55e62fe1b8ae3526b2c758a3c2aaf2 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 1 Jul 2024 17:57:40 +0200 Subject: [PATCH] Use service_calls fixture in conversation tests (#120906) --- tests/components/conversation/test_trigger.py | 58 +++++++++---------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/tests/components/conversation/test_trigger.py b/tests/components/conversation/test_trigger.py index c5d4382e917..3c3e58e7136 100644 --- a/tests/components/conversation/test_trigger.py +++ b/tests/components/conversation/test_trigger.py @@ -11,16 +11,9 @@ from homeassistant.core import Context, HomeAssistant, ServiceCall from homeassistant.helpers import trigger from homeassistant.setup import async_setup_component -from tests.common import async_mock_service from tests.typing import WebSocketGenerator -@pytest.fixture -def calls(hass: HomeAssistant) -> list[ServiceCall]: - """Track calls to a mock service.""" - return async_mock_service(hass, "test", "automation") - - @pytest.fixture(autouse=True) async def setup_comp(hass: HomeAssistant) -> None: """Initialize components.""" @@ -29,7 +22,7 @@ async def setup_comp(hass: HomeAssistant) -> None: async def test_if_fires_on_event( - hass: HomeAssistant, calls: list[ServiceCall], setup_comp: None + hass: HomeAssistant, service_calls: list[ServiceCall] ) -> None: """Test the firing of events.""" assert await async_setup_component( @@ -62,8 +55,10 @@ async def test_if_fires_on_event( assert service_response["response"]["speech"]["plain"]["speech"] == "Done" await hass.async_block_till_done() - assert len(calls) == 1 - assert calls[0].data["data"] == { + assert len(service_calls) == 2 + assert service_calls[1].domain == "test" + assert service_calls[1].service == "automation" + assert service_calls[1].data["data"] == { "alias": None, "id": "0", "idx": "0", @@ -75,7 +70,7 @@ async def test_if_fires_on_event( } -async def test_response(hass: HomeAssistant, setup_comp) -> None: +async def test_response(hass: HomeAssistant) -> None: """Test the conversation response action.""" response = "I'm sorry, Dave. I'm afraid I can't do that" assert await async_setup_component( @@ -106,7 +101,7 @@ async def test_response(hass: HomeAssistant, setup_comp) -> None: assert service_response["response"]["speech"]["plain"]["speech"] == response -async def test_empty_response(hass: HomeAssistant, setup_comp) -> None: +async def test_empty_response(hass: HomeAssistant) -> None: """Test the conversation response action with an empty response.""" assert await async_setup_component( hass, @@ -137,7 +132,7 @@ async def test_empty_response(hass: HomeAssistant, setup_comp) -> None: async def test_response_same_sentence( - hass: HomeAssistant, calls: list[ServiceCall], setup_comp: None + hass: HomeAssistant, service_calls: list[ServiceCall] ) -> None: """Test the conversation response action with multiple triggers using the same sentence.""" assert await async_setup_component( @@ -186,8 +181,10 @@ async def test_response_same_sentence( assert service_response["response"]["speech"]["plain"]["speech"] == "response 1" # Service should still have been called - assert len(calls) == 1 - assert calls[0].data["data"] == { + assert len(service_calls) == 2 + assert service_calls[1].domain == "test" + assert service_calls[1].service == "automation" + assert service_calls[1].data["data"] == { "alias": None, "id": "trigger1", "idx": "0", @@ -201,8 +198,6 @@ async def test_response_same_sentence( async def test_response_same_sentence_with_error( hass: HomeAssistant, - calls: list[ServiceCall], - setup_comp: None, caplog: pytest.LogCaptureFixture, ) -> None: """Test the conversation response action with multiple triggers using the same sentence and an error.""" @@ -253,7 +248,7 @@ async def test_response_same_sentence_with_error( async def test_subscribe_trigger_does_not_interfere_with_responses( - hass: HomeAssistant, setup_comp, hass_ws_client: WebSocketGenerator + hass: HomeAssistant, hass_ws_client: WebSocketGenerator ) -> None: """Test that subscribing to a trigger from the websocket API does not interfere with responses.""" websocket_client = await hass_ws_client() @@ -310,7 +305,7 @@ async def test_subscribe_trigger_does_not_interfere_with_responses( async def test_same_trigger_multiple_sentences( - hass: HomeAssistant, calls: list[ServiceCall], setup_comp: None + hass: HomeAssistant, service_calls: list[ServiceCall] ) -> None: """Test matching of multiple sentences from the same trigger.""" assert await async_setup_component( @@ -341,8 +336,10 @@ async def test_same_trigger_multiple_sentences( # Only triggers once await hass.async_block_till_done() - assert len(calls) == 1 - assert calls[0].data["data"] == { + assert len(service_calls) == 2 + assert service_calls[1].domain == "test" + assert service_calls[1].service == "automation" + assert service_calls[1].data["data"] == { "alias": None, "id": "0", "idx": "0", @@ -355,7 +352,7 @@ async def test_same_trigger_multiple_sentences( async def test_same_sentence_multiple_triggers( - hass: HomeAssistant, calls: list[ServiceCall], setup_comp: None + hass: HomeAssistant, service_calls: list[ServiceCall] ) -> None: """Test use of the same sentence in multiple triggers.""" assert await async_setup_component( @@ -403,11 +400,12 @@ async def test_same_sentence_multiple_triggers( ) await hass.async_block_till_done() - assert len(calls) == 2 + assert len(service_calls) == 3 # The calls may come in any order call_datas: set[tuple[str, str, str]] = set() - for call in calls: + service_calls.pop(0) # First call is the call to conversation.process + for call in service_calls: call_data = call.data["data"] call_datas.add((call_data["id"], call_data["platform"], call_data["sentence"])) @@ -474,9 +472,7 @@ async def test_fails_on_no_sentences(hass: HomeAssistant) -> None: ) -async def test_wildcards( - hass: HomeAssistant, calls: list[ServiceCall], setup_comp: None -) -> None: +async def test_wildcards(hass: HomeAssistant, service_calls: list[ServiceCall]) -> None: """Test wildcards in trigger sentences.""" assert await async_setup_component( hass, @@ -507,8 +503,10 @@ async def test_wildcards( ) await hass.async_block_till_done() - assert len(calls) == 1 - assert calls[0].data["data"] == { + assert len(service_calls) == 2 + assert service_calls[1].domain == "test" + assert service_calls[1].service == "automation" + assert service_calls[1].data["data"] == { "alias": None, "id": "0", "idx": "0", @@ -536,8 +534,6 @@ async def test_wildcards( async def test_trigger_with_device_id(hass: HomeAssistant) -> None: """Test that a trigger receives a device_id.""" - assert await async_setup_component(hass, "homeassistant", {}) - assert await async_setup_component(hass, "conversation", {}) assert await async_setup_component( hass, "automation",