Improve template reload#169480
Conversation
|
Hey there @Petro31, @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
There was a problem hiding this comment.
Pull request overview
This PR improves the Template integration’s reload behavior by ensuring long-running scripts created by template trigger coordinators and template entity actions are stopped when templates are reloaded/removed.
Changes:
- Replace the trigger template coordinator’s previous removal hook with an
async_shutdown()that unsubscribes triggers and stops the coordinator script. - Stop template entity action scripts during entity removal via
async_will_remove_from_hass(). - Add tests covering reload behavior (and shutdown behavior) for coordinator scripts and entity action scripts.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
homeassistant/components/template/__init__.py |
Uses await coordinator.async_shutdown() when replacing YAML-driven coordinators during reload. |
homeassistant/components/template/coordinator.py |
Implements coordinator async_shutdown() to unsubscribe triggers and stop the coordinator action script. |
homeassistant/components/template/entity.py |
Stops entity action scripts when a template entity is removed from HA. |
tests/components/template/test_trigger_entity.py |
Adds tests for stopping coordinator scripts and unsubscribing triggers on reload (plus a shutdown behavior test). |
tests/components/template/test_entity.py |
Adds parametrized test ensuring entity action scripts are stopped when template entities are removed on reload. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR improves the template integration’s YAML reload behavior to ensure long-running scripts are stopped during reload, preventing leftover coordinator/entity action scripts from continuing after configuration changes.
Changes:
- Stop trigger-based template coordinator scripts during
template.reloadby introducing/using coordinator shutdown cleanup. - Stop template entity action scripts when entities are removed from Home Assistant (e.g., during reload).
- Add regression tests covering reload/shutdown behavior for coordinator scripts and entity action scripts.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
homeassistant/components/template/__init__.py |
Switch coordinator cleanup on config processing from a sync remove to an awaited shutdown that can stop scripts. |
homeassistant/components/template/coordinator.py |
Implement coordinator async_shutdown() to unsubscribe triggers and stop the coordinator action script. |
homeassistant/components/template/entity.py |
Stop template entity action scripts in async_will_remove_from_hass() so reload/entity removal cleans up running scripts. |
tests/components/template/test_trigger_entity.py |
Add tests ensuring coordinator scripts stop on shutdown/reload and triggers unsubscribe on reload. |
tests/components/template/test_entity.py |
Add parametrized test ensuring reload stops template entity action scripts (state- and trigger-based). |
| # Fire the trigger to start the action script, then yield without | ||
| # waiting for the script to finish | ||
| hass.bus.async_fire("test_event", {"value": "hello"}) | ||
| await asyncio.sleep(0) | ||
|
|
||
| # Script should be running (stuck on delay) | ||
| coordinators = hass.data[DATA_COORDINATORS] | ||
| assert len(coordinators) == 1 | ||
| assert coordinators[0]._script.is_running |
| # Fire the trigger to start the action script | ||
| hass.bus.async_fire("test_event", {"value": "hello"}) | ||
| await asyncio.sleep(0) | ||
|
|
||
| # Script should be running | ||
| coordinators = hass.data[DATA_COORDINATORS] | ||
| assert len(coordinators) == 1 | ||
| coordinator = coordinators[0] | ||
| assert coordinator._script.is_running |
| hass.async_create_task( | ||
| hass.services.async_call("light", "turn_on", {"entity_id": "light.test_light"}) | ||
| ) | ||
| await asyncio.sleep(0) |
|
Looks like there's a related test failure. |
|
The PR changes cleanup in the template integration, the failing test is testing the template helper. |
Which test was it? I'm not seeing it in the history |
Proposed change
Improve template reload (
SERVICE_RELOAD):Type of change
Additional information
Checklist
ruff format homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all.To help with the load of incoming pull requests: