New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add tests for types and functions for type conversions in templates #100807
Conversation
There's a typo in the description. |
🤦♂️ thanks |
Suggestion: Given Also does |
|
I have seen tuples reported by Home Assistant's native typing but I can't recall encountering sets. Does native typing ever return a set? Attempting to define a set in the Template Editor like this results in an error: {% set x = {"cat", "dog"} %} Is there any reason they are implemented as filters instead of tests? The addition of {{ {'qty': 56, 'color': 'red'} is mapping }}
{{ 5 is number }}
{{ 5 is integer }}
{{ 5.6 is float }}
{{ 'cat' is string }}
{{ false is boolean }}
{{ (1,2) is iterable and (1,2) is not string }}
{{ 'cat' is iterable and 'cat' is not string }} |
they're implemented as filters, functions, and tests, to be consistent with regarding |
Could |
Not sure what I was doing when I said this but I can't reproduce so you are right. Regardless, identifiers and connections for a device entry are returned as |
Understood. For future reference, it would be useful if there was more support for sets in Home Assistant's implementation of Jinja2. At the moment there's not much one can do other than iterate through a set's values. {% for i in device_attr('9bf07320ae9bc506dd6486d8071a0582', 'identifiers') %}
{{i}}
{% endfor %} I can't confirm if useful set methods like Anyway, thanks for the addition of the useful filters/tests/functions. |
sorry, didn't realize I repeated myself about the device entries 😅 Regarding thinking about this some more, |
native |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update the PR description to explain why the new functionality is useful.
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
I suppose we don't... I was following the pattern for Anyway, should I remove the filters and functions? |
Co-authored-by: Robert Resch <robert@resch.dev>
Co-authored-by: Robert Resch <robert@resch.dev>
Co-authored-by: Robert Resch <robert@resch.dev>
Co-authored-by: Robert Resch <robert@resch.dev>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small doc improvements.
Otherwise fine to go
Co-authored-by: Robert Resch <robert@resch.dev>
Co-authored-by: Robert Resch <robert@resch.dev>
Co-authored-by: Robert Resch <robert@resch.dev>
Co-authored-by: Robert Resch <robert@resch.dev>
Co-authored-by: Robert Resch <robert@resch.dev>
Co-authored-by: Robert Resch <robert@resch.dev>
Co-authored-by: Robert Resch <robert@resch.dev>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @raman325 👍
Proposed change
Jinja has native tests for checking if a value is a
mapping
,string
,float
,int
, ornumber
, but it is missing tests for other types, specifically non string iterables. To check if something is alist
, according to https://stackoverflow.com/questions/11947325/how-to-test-for-a-list-in-jinja2 you have to write an, in my opinion, long and awkward check. Instead, we can add these convenience functions to make things easier for users. There is also no easy check fordatetime
which is a common enough type for such a test to be usefulWe also added the ability to convert a value to a
set
ortuple
. Offhand, I am not sure of the value of supporting converting to atuple
beyond giving users the ultimate flexibility, but aset
is useful because you can easily performintersection
,issubset
, anddifference
functions between two setsAdds new functions to help template users convert inputs into a specific type:
set
tuple
Adds new tests to help template users tests for certain types:
list
set
tuple
string_like
datetime
Type of change
Additional information
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: