mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +00:00
Avoid lingering timer on script shutdown (#89753)
This commit is contained in:
parent
c707ddbf7c
commit
913156b0e0
@ -66,6 +66,7 @@ from homeassistant.const import (
|
|||||||
from homeassistant.core import (
|
from homeassistant.core import (
|
||||||
SERVICE_CALL_LIMIT,
|
SERVICE_CALL_LIMIT,
|
||||||
Context,
|
Context,
|
||||||
|
Event,
|
||||||
HassJob,
|
HassJob,
|
||||||
HomeAssistant,
|
HomeAssistant,
|
||||||
callback,
|
callback,
|
||||||
@ -1074,7 +1075,17 @@ class _QueuedScriptRun(_ScriptRun):
|
|||||||
super()._finish()
|
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."""
|
"""Stop running Script objects started after shutdown."""
|
||||||
hass.data[DATA_NEW_SCRIPT_RUNS_NOT_ALLOWED] = None
|
hass.data[DATA_NEW_SCRIPT_RUNS_NOT_ALLOWED] = None
|
||||||
running_scripts = [
|
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."""
|
"""Stop running Script objects started before shutdown."""
|
||||||
async_call_later(
|
_schedule_stop_scripts_after_shutdown(hass)
|
||||||
hass, _SHUTDOWN_MAX_WAIT, partial(_async_stop_scripts_after_shutdown, hass)
|
|
||||||
)
|
|
||||||
|
|
||||||
running_scripts = [
|
running_scripts = [
|
||||||
script
|
script
|
||||||
|
@ -2208,6 +2208,7 @@ async def test_trigger_condition_explicit_id(hass: HomeAssistant, calls) -> None
|
|||||||
(SCRIPT_MODE_SINGLE, "script1: Already running"),
|
(SCRIPT_MODE_SINGLE, "script1: Already running"),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
@pytest.mark.parametrize("wait_for_stop_scripts_after_shutdown", [True])
|
||||||
async def test_recursive_automation_starting_script(
|
async def test_recursive_automation_starting_script(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
automation_mode,
|
automation_mode,
|
||||||
@ -2318,6 +2319,7 @@ async def test_recursive_automation_starting_script(
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("automation_mode", SCRIPT_MODE_CHOICES)
|
@pytest.mark.parametrize("automation_mode", SCRIPT_MODE_CHOICES)
|
||||||
|
@pytest.mark.parametrize("wait_for_stop_scripts_after_shutdown", [True])
|
||||||
async def test_recursive_automation(
|
async def test_recursive_automation(
|
||||||
hass: HomeAssistant, automation_mode, caplog: pytest.LogCaptureFixture
|
hass: HomeAssistant, automation_mode, caplog: pytest.LogCaptureFixture
|
||||||
) -> None:
|
) -> None:
|
||||||
|
@ -1127,6 +1127,7 @@ async def test_recursive_script_indirect(
|
|||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"script_mode", [SCRIPT_MODE_PARALLEL, SCRIPT_MODE_QUEUED, SCRIPT_MODE_RESTART]
|
"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(
|
async def test_recursive_script_turn_on(
|
||||||
hass: HomeAssistant, script_mode, caplog: pytest.LogCaptureFixture
|
hass: HomeAssistant, script_mode, caplog: pytest.LogCaptureFixture
|
||||||
) -> None:
|
) -> None:
|
||||||
|
@ -263,6 +263,33 @@ def expected_lingering_tasks() -> bool:
|
|||||||
return False
|
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)
|
@pytest.fixture(autouse=True)
|
||||||
def verify_cleanup(
|
def verify_cleanup(
|
||||||
event_loop: asyncio.AbstractEventLoop, expected_lingering_tasks: bool
|
event_loop: asyncio.AbstractEventLoop, expected_lingering_tasks: bool
|
||||||
|
@ -4213,6 +4213,7 @@ async def test_shutdown_at(
|
|||||||
assert_action_trace(expected_trace)
|
assert_action_trace(expected_trace)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("wait_for_stop_scripts_after_shutdown", [True])
|
||||||
async def test_shutdown_after(
|
async def test_shutdown_after(
|
||||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
||||||
) -> None:
|
) -> None:
|
||||||
@ -4251,6 +4252,7 @@ async def test_shutdown_after(
|
|||||||
assert_action_trace(expected_trace)
|
assert_action_trace(expected_trace)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("wait_for_stop_scripts_after_shutdown", [True])
|
||||||
async def test_start_script_after_shutdown(
|
async def test_start_script_after_shutdown(
|
||||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
||||||
) -> None:
|
) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user