From 77b95ae8a1e78d85b3f65be957e7540581df2d66 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 19 Oct 2020 03:18:25 -0500 Subject: [PATCH] Reduce domain states rate limit to 1 per second (#42004) --- homeassistant/helpers/template.py | 14 +++++++------- tests/helpers/test_template.py | 20 ++++++++++---------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index 8a971f6805e..592fc1fccd1 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -73,7 +73,8 @@ _COLLECTABLE_STATE_ATTRIBUTES = { "name", } -DEFAULT_RATE_LIMIT = timedelta(minutes=1) +ALL_STATES_RATE_LIMIT = timedelta(minutes=1) +DOMAIN_STATES_RATE_LIMIT = timedelta(seconds=1) @bind_hass @@ -240,12 +241,11 @@ class RenderInfo: 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 diff --git a/tests/helpers/test_template.py b/tests/helpers/test_template.py index 3b1e7c94a29..5f67dc2e372 100644 --- a/tests/helpers/test_template.py +++ b/tests/helpers/test_template.py @@ -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) @@ -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) @@ -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") @@ -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() @@ -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, @@ -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): @@ -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): @@ -1635,7 +1635,7 @@ 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) @@ -1643,7 +1643,7 @@ 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 template_cover_str = """ @@ -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):