Improve performance of counting and iterating states in templates (#40250)

Co-authored-by: Anders Melchiorsen <amelchio@nogoto.net>
This commit is contained in:
J. Nick Koston 2020-09-26 11:36:47 -05:00 committed by GitHub
parent 1d41f024cf
commit 35533407fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 11 deletions

View file

@ -2420,3 +2420,14 @@ For loop example getting 3 entity values:
assert "sensor0" in result
assert "sensor1" in result
assert "sun" in result
async def test_slice_states(hass):
"""Test iterating states with a slice."""
hass.states.async_set("sensor.test", "23")
tpl = template.Template(
"{% for states in states | slice(1) -%}{% set state = states | first %}{{ state.entity_id }}{%- endfor %}",
hass,
)
assert tpl.async_render() == "sensor.test"