Extract state template functions into a state Jinja2 extension#169034
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Refactors Home Assistant’s template system by extracting state-related template functions into a dedicated Jinja2 extension and moving associated tests into the extensions test suite.
Changes:
- Added
StateExtensionto provide state template globals/filters/tests (closest,distance,expand,has_value,is_state,is_state_attr,state_attr,states,state_translated,state_attr_translated). - Extended
TemplateFunctionwith apass_contextflag to support callable class instances that must not be wrapped withpass_context. - Moved state-function tests from
tests/helpers/template/test_init.pyinto a newtests/helpers/template/extensions/test_state.py.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
tests/helpers/template/test_init.py |
Removes most state-function tests and related imports; keeps a couple of closest/group render-info tests. |
tests/helpers/template/extensions/test_state.py |
New test module covering state template functions now provided by the extension. |
homeassistant/helpers/template/extensions/state.py |
Implements the new StateExtension and registers state globals/filters/tests. |
homeassistant/helpers/template/extensions/base.py |
Adds TemplateFunction.pass_context and updates wrapping logic accordingly. |
homeassistant/helpers/template/extensions/__init__.py |
Exports StateExtension from the extensions package. |
homeassistant/helpers/template/__init__.py |
Removes inline state-function registrations and loads StateExtension via add_extension. |
edenhaus
approved these changes
Apr 29, 2026
Member
Author
|
Thanks 🫶 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed change
This PR is part of the ongoing effort to refactor our huge single template monolith into something more maintainable. In previous PRs I've moved many categories of template methods out of the monolith template file into their own Jinja2 extension.
This PR moves the state template functions (
closest,distance,expand,has_value,is_state,is_state_attr,state_attr,states,state_translated,state_attr_translated) into their own Jinja2 extension. Moves all tests as well.The callable class instances (
AllStates,StateTranslated,StateAttrTranslated) required a small addition to the base extension: apass_contextflag onTemplateFunctionthat allowsrequires_hass=Truefunctions to opt out of_pass_contextwrapping. This allows these class instances to go through the standard pipeline (getting automatic limited template handling) without being wrapped as context functions.With this PR,
TemplateEnvironment.__init__no longer contains any manualself.globals[...]/self.filters[...]/self.tests[...]registrations, it only loads extensions.There is no functional change in this PR, it is only moving these methods and tests into an extension.
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: