From 16598ddd60fe5cb7d4e826e7aeee18c91ae11832 Mon Sep 17 00:00:00 2001 From: rikroe Date: Sun, 6 Aug 2023 22:52:05 +0200 Subject: [PATCH] Add tests for python_script response --- tests/components/python_script/test_init.py | 114 ++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/tests/components/python_script/test_init.py b/tests/components/python_script/test_init.py index 4744c065ede0..eda1deeee006 100644 --- a/tests/components/python_script/test_init.py +++ b/tests/components/python_script/test_init.py @@ -449,3 +449,117 @@ time.sleep(5) await hass.async_block_till_done() assert caplog.text.count("time.sleep") == 1 + + +async def test_execute_with_output( + hass: HomeAssistant, caplog: pytest.LogCaptureFixture +) -> None: + """Test executing a script with a return value.""" + caplog.set_level(logging.WARNING) + + scripts = [ + "/some/config/dir/python_scripts/hello.py", + ] + with patch( + "homeassistant.components.python_script.os.path.isdir", return_value=True + ), patch("homeassistant.components.python_script.glob.iglob", return_value=scripts): + await async_setup_component(hass, "python_script", {}) + + source = """ +output = {"result": f"hello {data.get('name', 'World')}"} + """ + + with patch( + "homeassistant.components.python_script.open", + mock_open(read_data=source), + create=True, + ): + response = await hass.services.async_call( + "python_script", + "hello", + {"name": "paulus"}, + blocking=True, + return_response=True, + ) + + assert isinstance(response, dict) + assert len(response) == 1 + assert response["result"] == "hello paulus" + + # No errors logged = good + assert caplog.text == "" + + +async def test_execute_no_output( + hass: HomeAssistant, caplog: pytest.LogCaptureFixture +) -> None: + """Test executing a script without a return value.""" + caplog.set_level(logging.WARNING) + + scripts = [ + "/some/config/dir/python_scripts/hello.py", + ] + with patch( + "homeassistant.components.python_script.os.path.isdir", return_value=True + ), patch("homeassistant.components.python_script.glob.iglob", return_value=scripts): + await async_setup_component(hass, "python_script", {}) + + source = """ +no_output = {"result": f"hello {data.get('name', 'World')}"} + """ + + with patch( + "homeassistant.components.python_script.open", + mock_open(read_data=source), + create=True, + ): + response = await hass.services.async_call( + "python_script", + "hello", + {"name": "paulus"}, + blocking=True, + return_response=True, + ) + + assert isinstance(response, dict) + assert len(response) == 0 + + # No errors logged = good + assert caplog.text == "" + + +async def test_execute_wrong_output_type( + hass: HomeAssistant, caplog: pytest.LogCaptureFixture +) -> None: + """Test executing a script without a return value.""" + caplog.set_level(logging.WARNING) + + scripts = [ + "/some/config/dir/python_scripts/hello.py", + ] + with patch( + "homeassistant.components.python_script.os.path.isdir", return_value=True + ), patch("homeassistant.components.python_script.glob.iglob", return_value=scripts): + await async_setup_component(hass, "python_script", {}) + + source = """ +output = f"hello {data.get('name', 'World')}" + """ + + with patch( + "homeassistant.components.python_script.open", + mock_open(read_data=source), + create=True, + ): + response = await hass.services.async_call( + "python_script", + "hello", + {"name": "paulus"}, + blocking=True, + return_response=True, + ) + + assert isinstance(response, dict) + assert len(response) == 0 + + assert "Expected `output` to be a dictionary, was " in caplog.text