Skip to content

Improve template reload#169480

Draft
emontnemery wants to merge 3 commits intodevfrom
template_improve_shutdown
Draft

Improve template reload#169480
emontnemery wants to merge 3 commits intodevfrom
template_improve_shutdown

Conversation

@emontnemery
Copy link
Copy Markdown
Contributor

@emontnemery emontnemery commented Apr 29, 2026

Proposed change

Improve template reload (SERVICE_RELOAD):

  • Coordinator scripts are now stopped
  • Template entity action scripts are now stopped

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies a diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

@emontnemery emontnemery added this to the 2026.5.0b0 milestone Apr 29, 2026
Copilot AI review requested due to automatic review settings April 29, 2026 15:36
@emontnemery emontnemery requested a review from a team as a code owner April 29, 2026 15:36
@home-assistant home-assistant Bot added bugfix cla-signed has-tests integration: template small-pr PRs with less than 30 lines. Top 100 Integration is ranked within the top 100 by usage Top 200 Integration is ranked within the top 200 by usage Top 50 Integration is ranked within the top 50 by usage labels Apr 29, 2026
@home-assistant
Copy link
Copy Markdown
Contributor

Hey there @Petro31, @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (template) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of template can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign template Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread tests/components/template/test_trigger_entity.py Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 29, 2026 15:43
@frenck frenck modified the milestones: 2026.5.0b0, 2026.5.0 Apr 29, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.reload by 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).

Comment on lines +275 to +283
# 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
Comment on lines +326 to +334
# 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
Comment on lines +67 to +70
hass.async_create_task(
hass.services.async_call("light", "turn_on", {"entity_id": "light.test_light"})
)
await asyncio.sleep(0)
@MartinHjelmare
Copy link
Copy Markdown
Member

Looks like there's a related test failure.

@MartinHjelmare MartinHjelmare marked this pull request as draft April 29, 2026 16:14
@emontnemery
Copy link
Copy Markdown
Contributor Author

The PR changes cleanup in the template integration, the failing test is testing the template helper.
I think the test is just flaky, and it failing not related to this PR.

@Petro31
Copy link
Copy Markdown
Contributor

Petro31 commented Apr 30, 2026

testing the template helper.

Which test was it? I'm not seeing it in the history

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix by-code-owner cla-signed has-tests integration: template Quality Scale: internal small-pr PRs with less than 30 lines. Top 50 Integration is ranked within the top 50 by usage Top 100 Integration is ranked within the top 100 by usage Top 200 Integration is ranked within the top 200 by usage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants