Jinja templating improvements#88575
Conversation
Turns on as_mutable, expression statements, for loop controls, and include/import
|
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). |
emontnemery
left a comment
There was a problem hiding this comment.
Thanks for the PR!
Please split this up to multiple PRs, where each PR adds a single new template feature.
|
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
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:
breakandcontinuefor for loopscustom_jinjafolder in your config folderas_mutablefilter so that one can efficiently mutate a list or dictionary without rebuilding it from scratch in a loopdoextension to make it possible to mutate lists/dicts.All of this enables the following code:
In shared.jinja:
Elsewhere:
With the following output:
Type of change
Additional information
I'm happy to update the documentation as well but wanted to first clear that this would be a welcome change.
Checklist
black --fast 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..coveragerc.To help with the load of incoming pull requests: