diff --git a/homeassistant/helpers/script.py b/homeassistant/helpers/script.py index 02fa9dc7806..9ba4e7a9d88 100644 --- a/homeassistant/helpers/script.py +++ b/homeassistant/helpers/script.py @@ -66,6 +66,7 @@ from homeassistant.const import ( from homeassistant.core import ( SERVICE_CALL_LIMIT, Context, + Event, HassJob, HomeAssistant, callback, @@ -1074,7 +1075,17 @@ class _QueuedScriptRun(_ScriptRun): super()._finish() -async def _async_stop_scripts_after_shutdown(hass, point_in_time): +@callback +def _schedule_stop_scripts_after_shutdown(hass: HomeAssistant) -> None: + """Stop running Script objects started after shutdown.""" + async_call_later( + hass, _SHUTDOWN_MAX_WAIT, partial(_async_stop_scripts_after_shutdown, hass) + ) + + +async def _async_stop_scripts_after_shutdown( + hass: HomeAssistant, point_in_time: datetime +) -> None: """Stop running Script objects started after shutdown.""" hass.data[DATA_NEW_SCRIPT_RUNS_NOT_ALLOWED] = None running_scripts = [ @@ -1091,11 +1102,9 @@ async def _async_stop_scripts_after_shutdown(hass, point_in_time): ) -async def _async_stop_scripts_at_shutdown(hass, event): +async def _async_stop_scripts_at_shutdown(hass: HomeAssistant, event: Event) -> None: """Stop running Script objects started before shutdown.""" - async_call_later( - hass, _SHUTDOWN_MAX_WAIT, partial(_async_stop_scripts_after_shutdown, hass) - ) + _schedule_stop_scripts_after_shutdown(hass) running_scripts = [ script diff --git a/tests/components/automation/test_init.py b/tests/components/automation/test_init.py index ff4ede357a4..b8a1c52f473 100644 --- a/tests/components/automation/test_init.py +++ b/tests/components/automation/test_init.py @@ -2208,6 +2208,7 @@ async def test_trigger_condition_explicit_id(hass: HomeAssistant, calls) -> None (SCRIPT_MODE_SINGLE, "script1: Already running"), ), ) +@pytest.mark.parametrize("wait_for_stop_scripts_after_shutdown", [True]) async def test_recursive_automation_starting_script( hass: HomeAssistant, automation_mode, @@ -2318,6 +2319,7 @@ async def test_recursive_automation_starting_script( @pytest.mark.parametrize("automation_mode", SCRIPT_MODE_CHOICES) +@pytest.mark.parametrize("wait_for_stop_scripts_after_shutdown", [True]) async def test_recursive_automation( hass: HomeAssistant, automation_mode, caplog: pytest.LogCaptureFixture ) -> None: diff --git a/tests/components/script/test_init.py b/tests/components/script/test_init.py index 8712e408011..dc88ae2f0f2 100644 --- a/tests/components/script/test_init.py +++ b/tests/components/script/test_init.py @@ -1127,6 +1127,7 @@ async def test_recursive_script_indirect( @pytest.mark.parametrize( "script_mode", [SCRIPT_MODE_PARALLEL, SCRIPT_MODE_QUEUED, SCRIPT_MODE_RESTART] ) +@pytest.mark.parametrize("wait_for_stop_scripts_after_shutdown", [True]) async def test_recursive_script_turn_on( hass: HomeAssistant, script_mode, caplog: pytest.LogCaptureFixture ) -> None: diff --git a/tests/conftest.py b/tests/conftest.py index 32b6f98ca72..c4530bd5381 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -263,6 +263,33 @@ def expected_lingering_tasks() -> bool: return False +@pytest.fixture +def wait_for_stop_scripts_after_shutdown() -> bool: + """Add ability to bypass _schedule_stop_scripts_after_shutdown. + + _schedule_stop_scripts_after_shutdown leaves a lingering timer. + + Parametrize to True to bypass the pytest failure. + @pytest.mark.parametrize("wait_for_stop_scripts_at_shutdown", [True]) + """ + return False + + +@pytest.fixture(autouse=True) +def skip_stop_scripts( + wait_for_stop_scripts_after_shutdown: bool, +) -> Generator[None, None, None]: + """Add ability to bypass _schedule_stop_scripts_after_shutdown.""" + if wait_for_stop_scripts_after_shutdown: + yield + return + with patch( + "homeassistant.helpers.script._schedule_stop_scripts_after_shutdown", + AsyncMock(), + ): + yield + + @pytest.fixture(autouse=True) def verify_cleanup( event_loop: asyncio.AbstractEventLoop, expected_lingering_tasks: bool diff --git a/tests/helpers/test_script.py b/tests/helpers/test_script.py index 0521bc722cd..5affce5d501 100644 --- a/tests/helpers/test_script.py +++ b/tests/helpers/test_script.py @@ -4213,6 +4213,7 @@ async def test_shutdown_at( assert_action_trace(expected_trace) +@pytest.mark.parametrize("wait_for_stop_scripts_after_shutdown", [True]) async def test_shutdown_after( hass: HomeAssistant, caplog: pytest.LogCaptureFixture ) -> None: @@ -4251,6 +4252,7 @@ async def test_shutdown_after( assert_action_trace(expected_trace) +@pytest.mark.parametrize("wait_for_stop_scripts_after_shutdown", [True]) async def test_start_script_after_shutdown( hass: HomeAssistant, caplog: pytest.LogCaptureFixture ) -> None: