Avoid lingering timer on script shutdown (#89753)
parent
c707ddbf7c
commit
913156b0e0
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in New Issue