Skip to content

Jinja templating improvements#88575

Closed
depoll wants to merge 3 commits into
home-assistant:devfrom
depoll:template-improvements
Closed

Jinja templating improvements#88575
depoll wants to merge 3 commits into
home-assistant:devfrom
depoll:template-improvements

Conversation

@depoll
Copy link
Copy Markdown
Contributor

@depoll depoll commented Feb 22, 2023

Breaking change

Proposed change

I've made some minor changes to the Jinja environment to add a few things that make it possible to improve performance and/or code reuse:

All of this enables the following code:

In shared.jinja:

{% macro some_macro() -%}
A macro
{%- endmacro %}
{%- set some_constant = "A constant" %}

Elsewhere:

{% set mutableDict = {} | as_mutable %}
{% do mutableDict.update({'foo': 'bar'}) %}
{% set mutableList = [] | as_mutable %}
{% do mutableList.append("baz") %}

{{ mutableDict }}
{{ mutableList }}

{%- import "shared.jinja" as shared %}
{{ shared.some_constant }}
{{ shared.some_macro() }}

With the following output:

{'foo': 'bar'}
['baz']
A constant
A macro

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:

I'm happy to update the documentation as well but wanted to first clear that this would be a welcome change.

Checklist

  • 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
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

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 link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

Turns on as_mutable, expression statements, for loop controls,
and include/import
@depoll
Copy link
Copy Markdown
Contributor Author

depoll commented Feb 22, 2023

For context, I've added this based on use cases in my home. I'd love to be able to have some shared utilities/macros/constants to use across my automations. I also ended up with some performance problems due to a large number of incoming events from a slew of esphome devices across my home. I have a template sensor intercepting those events, but the recalculation is substantially slower than it needs to be because I'm constantly rebuilding lists and dictionaries (using a namespace, then making concatenating over and over again) instead of just updating a particular value (turning O(1) operations into O(n) and O(n^2) in many cases).

Copy link
Copy Markdown
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

Please split this up to multiple PRs, where each PR adds a single new template feature.

@home-assistant home-assistant Bot marked this pull request as draft February 22, 2023 08:04
@home-assistant
Copy link
Copy Markdown
Contributor

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@depoll
Copy link
Copy Markdown
Contributor Author

depoll commented Feb 22, 2023

Broke this out into 3 pull requests for each bit of functionality: #88624 #88625 #88626

@depoll depoll closed this Feb 22, 2023
@github-actions github-actions Bot locked and limited conversation to collaborators Feb 23, 2023
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.

2 participants