Unload scripts and conditions created by template entities#169366
Unload scripts and conditions created by template entities#169366
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 aims to improve lifecycle management for resources created by template (trigger-based) entities by unloading associated scripts/conditions and by switching condition evaluation to the explicit ConditionsChecker.async_check() API.
Changes:
- Add coordinator shutdown cleanup for the trigger-based template coordinator and evaluate conditions via
ConditionsChecker.async_check(...). - Unload action scripts when a template entity is removed from Home Assistant.
- Add tests covering coordinator shutdown cleanup and entity removal script unloading.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
tests/components/template/test_trigger_entity.py |
Adds regression tests asserting scripts/conditions are unloaded during coordinator shutdown and entity removal. |
homeassistant/components/template/entity.py |
Adds async_will_remove_from_hass to unload per-entity action scripts on removal. |
homeassistant/components/template/coordinator.py |
Types _cond_func as ConditionsChecker, uses async_check, and introduces async_shutdown cleanup. |
|
The bot comments about making sure the script is stopped before it's unloaded is valid. Set to draft until fixed. |
|
Changes which addressed pre-existing behavior split out to #169480 |
| mock_script = Mock(spec=Script) | ||
| mock_cond = Mock(spec=condition.ConditionsChecker) | ||
| coordinator._script = mock_script | ||
| coordinator._cond_func = mock_cond | ||
|
|
||
| await coordinator.async_shutdown() | ||
|
|
||
| mock_script.async_unload.assert_called_once() | ||
| mock_cond.async_unload.assert_called_once() |
Proposed change
Unload scripts and conditions created by template entities, and use the modern explicit API to evaluate conditions.
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: