Commit Graph

190 Commits

Author SHA1 Message Date
Erik Montnemery
03d3a87f23 Small cleanup of legacy groups (#102918)
* Small cleanup of legacy groups

* Update tests which create groups
2023-10-28 17:16:41 +02: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
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
c7b4d4f361 Adjust helpers tests which create devices (#98214) 2023-08-10 19:28:16 +02:00
Marc Mueller
f39a35c4ef Fix jinja2 DeprecationWarnings (#97728) 2023-08-04 11:25:08 +02:00
Meow
6fd60024cc Refactored deprecated UNITS (#97368) 2023-08-03 21:12:01 +02:00
J. Nick Koston
b2e708834f Add slots to the StateMachine class (#95849) 2023-07-05 14:00:37 +02:00
J. Nick Koston
b24c6adc75 Avoid regex for negative zero check in sensor (#95691)
* Avoid regex for negative zero check in sensor

We can avoid calling the regex for every sensor value
since most of the time values are not negative zero

* tweak

* tweak

* Apply suggestions from code review

* simpler

* cover

* safer and still fast

* safer and still fast

* prep for py3.11

* fix check

* add missing cover

* more coverage

* coverage

* coverage
2023-07-02 21:53:50 -04:00
epenet
6836e15d98 Add type hints to tests (#92477) 2023-05-04 11:25:35 +02:00
Franck Nijhof
65d7e48815 Update ruff to v0.0.262 (#91767) 2023-04-21 08:15:41 +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
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
5f25b71df7 Add type hints to core tests (#88478) 2023-02-20 11:42:56 +01:00
Franck Nijhof
ed79265843 Enable Ruff PT006 (#88165)
* Enable Ruff PT006

* Adjust existing cases

* Fix tests

* Remove unneeded parentheses
2023-02-15 14:09:50 +01:00
Erik Montnemery
2d2ff19949 Round value in state_with_unit template function (#87619) 2023-02-09 12:54:59 +01:00
Franck Nijhof
80bf632e2d Clean up unnecessary registry mocks from helpers (#87734) 2023-02-08 20:15:29 -05:00
Franck Nijhof
89c0b27b42 Migrates tests to use UnitOfPressure enum (#86785) 2023-01-27 12:13:27 +01:00
Franck Nijhof
49148421cb Migrates tests to use UnitOfSpeed enum (#86777) 2023-01-27 10:58:55 +01:00
Franck Nijhof
e50a531cd9 Code styling tweaks to the tests - Part 2 (#86662)
Co-authored-by: jjlawren <jjlawren@users.noreply.github.com>
2023-01-26 01:23:53 +01:00
Vaarlion
3007e0259d Add a contains jinja filter and test (#86390) 2023-01-25 11:51:47 +01:00
epenet
30bf0634fe Add per-file-ignore to pylint (#86289) 2023-01-22 17:26:24 +01:00
Mike Degatano
b3deb476ef Allow is_state to accept a list of values (#81877)
* Allow is_state to accept a list of values

* Slightly more efficient

* Fix typing of state

Co-authored-by: Franck Nijhof <git@frenck.dev>
2022-11-30 22:54:14 +01:00
On Freund
9dd1b85cbb Allow device_attr and is_device_attr to be used as a filter and a test (respectively) (#81924)
Co-authored-by: Franck Nijhof <frenck@frenck.nl>
2022-11-24 08:31:17 +01:00
epenet
6b85d17e7c Add type hints to template tests (#82566) 2022-11-23 20:28:52 +01:00
Petro31
f73fc9e355 Adds states and state_attr as a filter, adds is_state and is_state_attr as a test. (#79473) 2022-10-25 19:49:51 +02:00
Erik Montnemery
aea0067e49 Add additional rules for converting distances (#80940)
* Add additional rules for converting distances

* Convert in to mm

* Adjust existing tests

* Add test
2022-10-25 12:51:23 +02:00
Franck Nijhof
326344db12 Rename entry_id template method to config_entry_id (#80935) 2022-10-25 12:11:14 +02:00
Erik Montnemery
6979cd95b0 Add suggested_unit_of_measurement attribute to sensors (#80638)
* Add suggested_unit_of_measurement attribute to sensors

* Lazy calculation of initial entity options

* Add type alias for entity options

* Small tweak

* Add tests

* Store suggested_unit_of_measurement in its own option key

* Adapt to renaming of IMPERIAL_SYSTEM

* Fix rebase mistakes

* Apply suggestions from code review

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
2022-10-24 16:08:02 +02:00
epenet
8a1cc05e0c Enforce kwargs in unit system initialisation (#80620)
* Enforce kwargs in unit system initialisation

* Fix tests

* Sort kwargs in unit_system
2022-10-20 10:43:32 +02:00
ehendrix23
47b40e1e61 Add optional default value to average template function/filter (#77499)
* Return None on empty list

* Updated to use default value

* Update comment.
2022-10-03 10:07:19 +02:00
Rami Mosleh
e7764b8bf1 Add ConfigEntry template function (#78030)
* Add ConfigEntry template function

* Remove looking up entry_id by entry title
2022-09-29 12:41:59 +02:00
Joakim Sørensen
8dbe293ae2 Add version to templates (#78484) 2022-09-15 15:01:40 +02:00
Erik Montnemery
1bc8770b51 Remove area_id from entity_registry.async_get_or_create (#77700)
* Remove area_id from entity_registry.async_get_or_create

* Adjust tests

* Fix lying comment in test
2022-09-02 13:31:05 +02:00
[pʲɵs]
0e9164b082 Add bool template filter and function (#74068)
Co-authored-by: Erik <erik@montnemery.com>
2022-06-28 16:22:09 +02:00
J. Nick Koston
949922ef2c Fix exception when as_dict is called on a TemplateState (#73984) 2022-06-25 09:19:11 -07:00
J. Nick Koston
32e0d9f47c Speed up generation of template states (#73728)
* Speed up generation of template states

* tweak

* cache

* cache hash

* weaken

* Revert "weaken"

This reverts commit 4856f50080.

* lower cache size as it tends to be the same ones over and over

* lower cache size as it tends to be the same ones over and over

* lower cache size as it tends to be the same ones over and over

* cover

* Update homeassistant/helpers/template.py

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>

* id reuse is possible

* account for iterting all sensors

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
2022-06-24 17:28:26 -04:00
Raphaël Beamonte
88129dbe91 Allow log template function to return specified default on math domain error (#72960)
Fix regression for logarithm template
2022-06-03 15:27:10 +02:00
Kevin Stillhammer
90e5d69184 Add template as_timedelta (#71801) 2022-05-23 19:32:22 +02:00
Erik Montnemery
4885331509 Fail template functions when no default specified (#71687) 2022-05-13 09:46:49 -07:00