Skip to content

Extract state template functions into a state Jinja2 extension#169034

Merged
edenhaus merged 4 commits into
devfrom
frenck-2026-0425
Apr 29, 2026
Merged

Extract state template functions into a state Jinja2 extension#169034
edenhaus merged 4 commits into
devfrom
frenck-2026-0425

Conversation

@frenck
Copy link
Copy Markdown
Member

@frenck frenck commented Apr 24, 2026

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: a pass_context flag on TemplateFunction that allows requires_hass=True functions to opt out of _pass_context wrapping. 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 manual self.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

  • 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:

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

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 StateExtension to 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 TemplateFunction with a pass_context flag to support callable class instances that must not be wrapped with pass_context.
  • Moved state-function tests from tests/helpers/template/test_init.py into a new tests/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.

Comment thread tests/helpers/template/test_init.py Outdated
@frenck frenck marked this pull request as ready for review April 24, 2026 08:30
@frenck frenck requested a review from a team as a code owner April 24, 2026 08:30
Copilot AI review requested due to automatic review settings April 24, 2026 08:30
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

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings April 28, 2026 07:43
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

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.

Copy link
Copy Markdown
Member

@edenhaus edenhaus left a comment

Choose a reason for hiding this comment

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

Thanks @frenck 👍

@edenhaus edenhaus merged commit 71fc725 into dev Apr 29, 2026
52 checks passed
@edenhaus edenhaus deleted the frenck-2026-0425 branch April 29, 2026 08:03
@frenck
Copy link
Copy Markdown
Member Author

frenck commented Apr 29, 2026

Thanks 🫶

@github-actions github-actions Bot locked and limited conversation to collaborators Apr 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants