From 9776dd660f11f9d7a3e2a46d7c4bdbcdf0ca9671 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Thu, 6 Jul 2023 13:43:17 +0200 Subject: [PATCH 1/2] Make script services always respond when asked --- homeassistant/components/script/__init__.py | 2 +- tests/components/script/test_init.py | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/script/__init__.py b/homeassistant/components/script/__init__.py index f8d41db0e11616..8530aa3b04c143 100644 --- a/homeassistant/components/script/__init__.py +++ b/homeassistant/components/script/__init__.py @@ -608,7 +608,7 @@ async def _service_handler(self, service: ServiceCall) -> ServiceResponse: variables=service.data, context=service.context, wait=True ) if service.return_response: - return response + return response or {} return None async def async_added_to_hass(self) -> None: diff --git a/tests/components/script/test_init.py b/tests/components/script/test_init.py index 199c3e08942daf..9c2c3c4ecc480e 100644 --- a/tests/components/script/test_init.py +++ b/tests/components/script/test_init.py @@ -26,7 +26,7 @@ callback, split_entity_id, ) -from homeassistant.exceptions import HomeAssistantError, ServiceNotFound +from homeassistant.exceptions import ServiceNotFound from homeassistant.helpers import entity_registry as er, template from homeassistant.helpers.event import async_track_state_change from homeassistant.helpers.script import ( @@ -1645,10 +1645,12 @@ async def test_responses_error(hass: HomeAssistant) -> None: }, ) - with pytest.raises(HomeAssistantError): - assert await hass.services.async_call( + assert ( + await hass.services.async_call( DOMAIN, "test", {"greeting": "world"}, blocking=True, return_response=True ) + == {} + ) # Validate we can also call it without return_response assert ( await hass.services.async_call( From 05637062224c61e2d1b89a6fd16cd0ab2a1c8349 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Thu, 6 Jul 2023 14:01:40 +0200 Subject: [PATCH 2/2] Update test_init.py --- tests/components/script/test_init.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/components/script/test_init.py b/tests/components/script/test_init.py index 9c2c3c4ecc480e..cc41b6c404cd24 100644 --- a/tests/components/script/test_init.py +++ b/tests/components/script/test_init.py @@ -1625,7 +1625,7 @@ async def test_responses(hass: HomeAssistant, response: Any) -> None: ) -async def test_responses_error(hass: HomeAssistant) -> None: +async def test_responses_no_response(hass: HomeAssistant) -> None: """Test response variable not set.""" mock_restore_cache(hass, ()) assert await async_setup_component( @@ -1645,6 +1645,7 @@ async def test_responses_error(hass: HomeAssistant) -> None: }, ) + # Validate we can call it with return_response assert ( await hass.services.async_call( DOMAIN, "test", {"greeting": "world"}, blocking=True, return_response=True