From 949c88930f562d58dd10a58a688e51bb506c9207 Mon Sep 17 00:00:00 2001 From: tronikos Date: Tue, 24 Jan 2023 08:54:23 -0800 Subject: [PATCH] Google Assistant SDK: Allow multiple commands in the same conversation context (#85423) * Allow multiple commands in the same conversation * fix test * Apply suggestions from code review Co-authored-by: Paulus Schoutsen * Add missing cv import * Update service description * Fix test after merging dev Co-authored-by: Paulus Schoutsen --- .../google_assistant_sdk/__init__.py | 6 ++-- .../google_assistant_sdk/services.yaml | 2 +- .../google_assistant_sdk/test_init.py | 29 +++++++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/google_assistant_sdk/__init__.py b/homeassistant/components/google_assistant_sdk/__init__.py index a414239b69f..c25db0a856e 100644 --- a/homeassistant/components/google_assistant_sdk/__init__.py +++ b/homeassistant/components/google_assistant_sdk/__init__.py @@ -38,7 +38,7 @@ SERVICE_SEND_TEXT_COMMAND_FIELD_MEDIA_PLAYER = "media_player" SERVICE_SEND_TEXT_COMMAND_SCHEMA = vol.All( { vol.Required(SERVICE_SEND_TEXT_COMMAND_FIELD_COMMAND): vol.All( - str, vol.Length(min=1) + cv.ensure_list, [vol.All(str, vol.Length(min=1))] ), vol.Optional(SERVICE_SEND_TEXT_COMMAND_FIELD_MEDIA_PLAYER): cv.comp_entity_ids, }, @@ -106,11 +106,11 @@ async def async_setup_service(hass: HomeAssistant) -> None: async def send_text_command(call: ServiceCall) -> None: """Send a text command to Google Assistant SDK.""" - command: str = call.data[SERVICE_SEND_TEXT_COMMAND_FIELD_COMMAND] + commands: list[str] = call.data[SERVICE_SEND_TEXT_COMMAND_FIELD_COMMAND] media_players: list[str] | None = call.data.get( SERVICE_SEND_TEXT_COMMAND_FIELD_MEDIA_PLAYER ) - await async_send_text_commands(hass, [command], media_players) + await async_send_text_commands(hass, commands, media_players) hass.services.async_register( DOMAIN, diff --git a/homeassistant/components/google_assistant_sdk/services.yaml b/homeassistant/components/google_assistant_sdk/services.yaml index c010843ed92..fc2a3ad264f 100644 --- a/homeassistant/components/google_assistant_sdk/services.yaml +++ b/homeassistant/components/google_assistant_sdk/services.yaml @@ -4,7 +4,7 @@ send_text_command: fields: command: name: Command - description: Command to send to Google Assistant. + description: Command(s) to send to Google Assistant. example: turn off kitchen TV selector: text: diff --git a/tests/components/google_assistant_sdk/test_init.py b/tests/components/google_assistant_sdk/test_init.py index 01993389c80..e01af4cbc57 100644 --- a/tests/components/google_assistant_sdk/test_init.py +++ b/tests/components/google_assistant_sdk/test_init.py @@ -145,6 +145,35 @@ async def test_send_text_command( mock_text_assistant.assert_has_calls([call().__enter__().assist(command)]) +async def test_send_text_commands( + hass: HomeAssistant, + setup_integration: ComponentSetup, +) -> None: + """Test service call send_text_command calls TextAssistant.""" + await setup_integration() + + entries = hass.config_entries.async_entries(DOMAIN) + assert len(entries) == 1 + assert entries[0].state is ConfigEntryState.LOADED + + command1 = "open the garage door" + command2 = "1234" + with patch( + "homeassistant.components.google_assistant_sdk.helpers.TextAssistant" + ) as mock_text_assistant: + await hass.services.async_call( + DOMAIN, + "send_text_command", + {"command": [command1, command2]}, + blocking=True, + ) + mock_text_assistant.assert_called_once_with( + ExpectedCredentials(), "en-US", audio_out=False + ) + mock_text_assistant.assert_has_calls([call().__enter__().assist(command1)]) + mock_text_assistant.assert_has_calls([call().__enter__().assist(command2)]) + + @pytest.mark.parametrize( "status,requires_reauth", [