Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions homeassistant/helpers/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
"name",
}

DEFAULT_RATE_LIMIT = timedelta(minutes=1)
ALL_STATES_RATE_LIMIT = timedelta(minutes=1)
DOMAIN_STATES_RATE_LIMIT = timedelta(seconds=1)


@bind_hass
Expand Down Expand Up @@ -240,12 +241,11 @@ def _freeze_sets(self) -> None:
def _freeze(self) -> None:
self._freeze_sets()

if self.rate_limit is None and (
self.domains or self.domains_lifecycle or self.all_states or self.exception
):
# If the template accesses all states or an entire
# domain, and no rate limit is set, we use the default.
self.rate_limit = DEFAULT_RATE_LIMIT
if self.rate_limit is None:
if self.all_states or self.exception:
self.rate_limit = ALL_STATES_RATE_LIMIT
elif self.domains or self.domains_lifecycle:
self.rate_limit = DOMAIN_STATES_RATE_LIMIT

if self.exception:
return
Expand Down
20 changes: 10 additions & 10 deletions tests/helpers/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def test_iterating_all_states(hass):

info = render_to_info(hass, tmpl_str)
assert_result_info(info, "", all_states=True)
assert info.rate_limit == template.DEFAULT_RATE_LIMIT
assert info.rate_limit == template.ALL_STATES_RATE_LIMIT

hass.states.async_set("test.object", "happy")
hass.states.async_set("sensor.temperature", 10)
Expand All @@ -168,7 +168,7 @@ def test_iterating_all_states_unavailable(hass):
info = render_to_info(hass, tmpl_str)

assert info.all_states is True
assert info.rate_limit == template.DEFAULT_RATE_LIMIT
assert info.rate_limit == template.ALL_STATES_RATE_LIMIT

hass.states.async_set("test.object", "unknown")
hass.states.async_set("sensor.temperature", 10)
Expand All @@ -183,7 +183,7 @@ def test_iterating_domain_states(hass):

info = render_to_info(hass, tmpl_str)
assert_result_info(info, "", domains=["sensor"])
assert info.rate_limit == template.DEFAULT_RATE_LIMIT
assert info.rate_limit == template.DOMAIN_STATES_RATE_LIMIT

hass.states.async_set("test.object", "happy")
hass.states.async_set("sensor.back_door", "open")
Expand Down Expand Up @@ -1420,7 +1420,7 @@ async def test_expand(hass):
hass, "{{ expand(states.group) | map(attribute='entity_id') | join(', ') }}"
)
assert_result_info(info, "", [], ["group"])
assert info.rate_limit == template.DEFAULT_RATE_LIMIT
assert info.rate_limit == template.DOMAIN_STATES_RATE_LIMIT

assert await async_setup_component(hass, "group", {})
await hass.async_block_till_done()
Expand All @@ -1437,7 +1437,7 @@ async def test_expand(hass):
hass, "{{ expand(states.group) | map(attribute='entity_id') | join(', ') }}"
)
assert_result_info(info, "test.object", {"test.object"}, ["group"])
assert info.rate_limit == template.DEFAULT_RATE_LIMIT
assert info.rate_limit == template.DOMAIN_STATES_RATE_LIMIT

info = render_to_info(
hass,
Expand Down Expand Up @@ -1588,7 +1588,7 @@ def test_async_render_to_info_with_complex_branching(hass):
)

assert_result_info(info, ["sensor.a"], {"light.a", "light.b"}, {"sensor"})
assert info.rate_limit == template.DEFAULT_RATE_LIMIT
assert info.rate_limit == template.DOMAIN_STATES_RATE_LIMIT


async def test_async_render_to_info_with_wildcard_matching_entity_id(hass):
Expand All @@ -1610,7 +1610,7 @@ async def test_async_render_to_info_with_wildcard_matching_entity_id(hass):
assert info.domains == {"cover"}
assert info.entities == set()
assert info.all_states is False
assert info.rate_limit == template.DEFAULT_RATE_LIMIT
assert info.rate_limit == template.DOMAIN_STATES_RATE_LIMIT


async def test_async_render_to_info_with_wildcard_matching_state(hass):
Expand All @@ -1635,15 +1635,15 @@ async def test_async_render_to_info_with_wildcard_matching_state(hass):
assert not info.domains
assert info.entities == set()
assert info.all_states is True
assert info.rate_limit == template.DEFAULT_RATE_LIMIT
assert info.rate_limit == template.ALL_STATES_RATE_LIMIT

hass.states.async_set("binary_sensor.door", "closed")
info = render_to_info(hass, template_complex_str)

assert not info.domains
assert info.entities == set()
assert info.all_states is True
assert info.rate_limit == template.DEFAULT_RATE_LIMIT
assert info.rate_limit == template.ALL_STATES_RATE_LIMIT

template_cover_str = """

Expand All @@ -1660,7 +1660,7 @@ async def test_async_render_to_info_with_wildcard_matching_state(hass):
assert info.domains == {"cover"}
assert info.entities == set()
assert info.all_states is False
assert info.rate_limit == template.DEFAULT_RATE_LIMIT
assert info.rate_limit == template.DOMAIN_STATES_RATE_LIMIT


def test_nested_async_render_to_info_case(hass):
Expand Down