Commit Graph

280 Commits (2eea658fd8c1b7d1bcac3d7c65299e7b1a11f4a6)

Author SHA1 Message Date
J. Nick Koston fb0dad66db
Add jinja_pass_arg to reserved template names (#107822) 2024-01-12 09:34:49 +01:00
Lars R bf6b9175a1
Add 'bitwise_xor' filter to jinja templates (#104942)
Co-authored-by: Robert Resch <robert@resch.dev>
2024-01-10 09:40:52 +01:00
J. Nick Koston d04e2d56da
Add support for JSON fragments (#107213) 2024-01-07 17:36:31 -10:00
Jan-Philipp Benecke e80138dfdf
Remove duplicate assignment of `median` and `statistical_mode` jinja2 filter (#106953) 2024-01-05 17:36:55 +01:00
Jan-Philipp Benecke 9d36b716e7
Use call_soon_threadsafe in render_will_timeout of template helper (#106514) 2023-12-30 13:55:53 +01:00
Martijn van der Pol a823edf1c2
Jinja filter and function for `median` and `statistical_mode` (#105554)
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
Co-authored-by: Franck Nijhof <git@frenck.dev>
2023-12-27 15:14:20 +01:00
J. Nick Koston 5c503683b7
Bump lru-dict to 1.3.0 (#105914)
* Bump lru-dict to 1.3.0

changelog: https://github.com/amitdev/lru-dict/compare/v1.2.0...v1.3.0

* fix typing

* quote types

* quote types
2023-12-18 00:16:31 +01:00
Jan Bouwhuis 4bb0e13cda
Workaround `to_json` template filter in parsing dict key (#105327)
* Work-a-round orjson for `to_json` fiter in case dict key is str subclass

* Add option instead

* Remove json.dumps work-a-round

* Update homeassistant/helpers/template.py

* Fix test

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
2023-12-08 20:57:53 +01:00
Marc Mueller a70ec64408
Update mypy to 1.7.0 (#103800) 2023-11-11 23:31:04 +01:00
J. Nick Koston 68471b6da5
Reduce template render overhead (#103343)
The contextmanager decorator creates a new context manager every
time its run, but since we only have a single context var, we can
use the same one every time. Creating the contextmanager was roughly
20% of the time time of the template render

I was a bit suprised to find it creates a new context manager
object every time https://stackoverflow.com/questions/34872535/why-contextmanager-is-slow
2023-11-04 09:46:01 +01:00
Raman Gupta 35d18a9a3e
Add tests for types and functions for type conversions in templates (#100807)
Co-authored-by: Robert Resch <robert@resch.dev>
2023-10-25 13:20:34 +02:00
Marc Mueller 0b2b486754
Update mypy to 1.6.0 (#101780) 2023-10-11 13:25:11 +02:00
J. Nick Koston 547f32818c
Make core States use cached_property (#100312)
Need to validate this is worth removing __slots__
2023-09-13 20:33:25 -04:00
Erik Montnemery 09ad1a9a36
Remove unnecessary block use of pylint disable in components p-z (#100192) 2023-09-12 20:47:48 +02:00
Erik Montnemery 48f7924e9e
Allow specifying a custom log function for template render (#99572)
* Allow specifying a custom log function for template render

* Bypass template cache when reporting errors + fix tests

* Send errors as events

* Fix logic for creating new TemplateEnvironment

* Add strict mode back

* Only send error events if report_errors is True

* Force test of websocket_api only

* Debug test

* Run pytest with higher verbosity

* Timeout after 1 minute, enable syslog output

* Adjust timeout

* Add debug logs

* Fix unsafe call to WebSocketHandler._send_message

* Remove debug code

* Improve test coverage

* Revert accidental change

* Include severity in error events

* Remove redundant information from error events
2023-09-06 10:03:35 +02:00
Erik Montnemery de73cafc8b
Small cleanup of TemplateEnvironment (#99571)
* Small cleanup of TemplateEnvironment

* Fix typo
2023-09-04 22:19:40 +02:00
Marc Mueller a9ade1f84d
Use asyncio.timeout [core] (#98447) 2023-08-15 08:36:05 -05:00
Erik Montnemery e1f0b44ba4
Use math.isfinite instead of explicitly checking for both nan and inf (#98103) 2023-08-09 14:13:57 +02:00
J. Nick Koston aa937b1640
Use slots for template RenderInfo (#93337) 2023-05-22 08:52:49 +02:00
J. Nick Koston 3314eed8d1
Switch to using a ContextVar for template RenderInfo (#93016)
The ContextVar is about 40% faster than the attr
and dict lookups
2023-05-14 12:23:31 -04:00
J. Nick Koston 24284fe379
Speed up validating domains in templates (#92975)
This path gets called quite a bit since most templates
access the state via states.DOMAIN...
2023-05-12 14:57:51 +02:00
David Poll ea12d7a86f
Add pretty printing, key sorting, and better performance to to_json in Jinja (#91253)
Co-authored-by: J. Nick Koston <nick@koston.org>
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
2023-04-13 00:32:13 +02:00
Erik Montnemery 37661fe79f
Update template environment from the event loop (#90758) 2023-04-04 09:52:47 +02:00
J. Nick Koston edd93e989e
Add render count to templates repr (#90753) 2023-04-03 19:38:15 -10:00
J. Nick Koston 17719663f0
Fix memory churn in state templates (#90685)
* Fix memory churn in state templates

The LRU for state templates was limited to 512 states. As soon
as it was exaused, system performance would tank as each template
that iterated all states would have to create and GC any state
> 512

* does it scale?

* avoid copy on all

* comment

* preen

* cover

* cover

* comments

* comments

* comments

* preen

* preen
2023-04-02 20:51:25 -04:00
J. Nick Koston f4c341253b
Avoid sorting domain/all states in templates (#90608) 2023-03-31 11:27:55 -10:00
Petro31 642984a042
Fix for is_hidden_entity when using it in select, selectattr, reject, and rejectattr (#90512)
fix
2023-03-30 09:14:58 -04:00
Erik Montnemery 5bc9545b81
Rename custom_jinja to custom_templates (#90473)
Co-authored-by: Franck Nijhof <git@frenck.dev>
2023-03-29 21:58:25 +02:00
ehendrix23 e45eab600f
Add has_value function/test to Jinja2 template (#79550) 2023-03-28 17:04:29 +02:00
Petro31 2123600039
Add minutely updates to relative_time and today_at template functions (#86815)
* add minutely update

* fix mypy
2023-03-28 15:10:28 +02:00
David Poll 0457bb2717
Add is_hidden_entity test for Jinja templates (#89011) 2023-03-13 18:20:33 +01:00
David Poll 7284af6a3e
Add an in-memory-preloading loader for Jinja imports (#88850)
* Adds a loader to enable jinja imports.

* Switch to in-memory

* Move loading custom_jinja off of the event loop

* Raise TemplateNotFound if template doesn't exist

* Fix docstring

* Adds a service to reload custom jinja

* Remove IO from test setup

* Improve coverage and small refactor

* Incorporate feedback and use .jinja extension

* Check the loaded sources in test.

* Incorporate PR feedback.

* Update homeassistant/helpers/template.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
2023-03-13 11:00:05 +01:00
Lucas Mindêllo de Andrade eed16dc185
Add list areas function to template (#88441) 2023-03-09 22:32:30 +01:00
J. Nick Koston cefba7c638
Avoid falling back to listening for all states when a template render raises an exception (#89392)
When a template render raised an exception we would start listening for
all states until the template did not raise an exception anymore. This
was not needed since the entity that is causing the exception was
already in the tracker.

Re-rendering on all state changes can be extremely expensive and can
bring an instance into a sluggish or unresponsive state when updating
from a much older version that did not raise ValueError when a default
was missing.
2023-03-08 15:50:34 -05:00
David Poll af49b98475
Enable jinja loop controls (break/continue) (#88625)
Enables jinja loop controls (break/continue)
2023-02-23 22:14:28 -05:00
epenet ba23816a0c
Inverse json import logic (#88099)
* Fix helpers and util

* Adjust components

* Move back errors

* Add report

* mypy

* mypy

* Assert deprecation messages

* Move test_json_loads_object

* Adjust tests

* Fix rebase

* Adjust pylint plugin

* Fix plugin

* Adjust references

* Adjust backup tests
2023-02-16 11:37:57 +01:00
Franck Nijhof 9030ca05b1
Enable Ruff SIM118 (#87772) 2023-02-15 12:39:12 +01:00
J. Nick Koston ac97097167
Speed up template lru_caches (#87942)
By avoiding the argument unpacking these functions are faster
and reduce stack overhead
2023-02-12 14:13:13 -05:00
J. Nick Koston e19f8595fa
Small cleanups to template helper (#87944)
- reduce dict lookups
- avoid split_entity_id
2023-02-12 14:11:48 -05:00
J. Nick Koston aa8927c98c
Speed up loops with jinja templates (#87945) 2023-02-12 18:40:55 +01:00
Erik Montnemery 2d2ff19949
Round value in state_with_unit template function (#87619) 2023-02-09 12:54:59 +01:00
Vaarlion 3007e0259d
Add a `contains` jinja filter and test (#86390) 2023-01-25 11:51:47 +01:00
Marc Mueller 45b4b0e990
Import `ParamSpec` from typing [Py310] (#86413)
* Import ParamSpec from typing [Py310]

* Update additional imports
2023-01-23 07:28:43 +01:00
J. Nick Koston ca4d7634a8
Switch an `asyncio.wait_for` in the template helper to `async_timeout` (#86349)
Switch an asyncio.wait_for in the template helper to async_timeout

Eliminates the extra task when calling async_render_will_timeout
2023-01-22 00:02:03 +02:00
Franck Nijhof 79b52a2b41
Stricter pylint message control (#86154) 2023-01-20 13:47:55 +01:00
Franck Nijhof 64c2340fab
Core code styling improvements (#85963) 2023-01-15 23:00:51 +01:00
Franck Nijhof 06a35fb7db
Code styling tweaks to core helpers (#85441) 2023-01-08 13:44:09 -10:00
Franck Nijhof d4f69a3652
String formatting and max line length - Part 7 (#84532)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2022-12-27 11:18:56 +01:00
Michaël Arnauts 7142b4ecac
Fixes some grammar mistakes (#84283) 2022-12-20 11:10:31 +01:00
epenet 9f7fd8956f
Use new unit enums in helpers (#83387) 2022-12-06 22:20:17 +01:00