Reduce domain states rate limit to 1 per second (#42004)

This commit is contained in:
J. Nick Koston 2020-10-19 03:18:25 -05:00 committed by GitHub
parent 3a9b2392f8
commit 77b95ae8a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 17 deletions

View File

@ -73,7 +73,8 @@ _COLLECTABLE_STATE_ATTRIBUTES = {
"name", "name",
} }
DEFAULT_RATE_LIMIT = timedelta(minutes=1) ALL_STATES_RATE_LIMIT = timedelta(minutes=1)
DOMAIN_STATES_RATE_LIMIT = timedelta(seconds=1)
@bind_hass @bind_hass
@ -240,12 +241,11 @@ class RenderInfo:
def _freeze(self) -> None: def _freeze(self) -> None:
self._freeze_sets() self._freeze_sets()
if self.rate_limit is None and ( if self.rate_limit is None:
self.domains or self.domains_lifecycle or self.all_states or self.exception if self.all_states or self.exception:
): self.rate_limit = ALL_STATES_RATE_LIMIT
# If the template accesses all states or an entire elif self.domains or self.domains_lifecycle:
# domain, and no rate limit is set, we use the default. self.rate_limit = DOMAIN_STATES_RATE_LIMIT
self.rate_limit = DEFAULT_RATE_LIMIT
if self.exception: if self.exception:
return return

View File

@ -150,7 +150,7 @@ def test_iterating_all_states(hass):
info = render_to_info(hass, tmpl_str) info = render_to_info(hass, tmpl_str)
assert_result_info(info, "", all_states=True) 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("test.object", "happy")
hass.states.async_set("sensor.temperature", 10) 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) info = render_to_info(hass, tmpl_str)
assert info.all_states is True 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("test.object", "unknown")
hass.states.async_set("sensor.temperature", 10) hass.states.async_set("sensor.temperature", 10)
@ -183,7 +183,7 @@ def test_iterating_domain_states(hass):
info = render_to_info(hass, tmpl_str) info = render_to_info(hass, tmpl_str)
assert_result_info(info, "", domains=["sensor"]) 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("test.object", "happy")
hass.states.async_set("sensor.back_door", "open") 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(', ') }}" hass, "{{ expand(states.group) | map(attribute='entity_id') | join(', ') }}"
) )
assert_result_info(info, "", [], ["group"]) 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", {}) assert await async_setup_component(hass, "group", {})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -1437,7 +1437,7 @@ async def test_expand(hass):
hass, "{{ expand(states.group) | map(attribute='entity_id') | join(', ') }}" hass, "{{ expand(states.group) | map(attribute='entity_id') | join(', ') }}"
) )
assert_result_info(info, "test.object", {"test.object"}, ["group"]) 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( info = render_to_info(
hass, 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_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): 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.domains == {"cover"}
assert info.entities == set() assert info.entities == set()
assert info.all_states is False 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): 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 not info.domains
assert info.entities == set() assert info.entities == set()
assert info.all_states is True 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") hass.states.async_set("binary_sensor.door", "closed")
info = render_to_info(hass, template_complex_str) 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 not info.domains
assert info.entities == set() assert info.entities == set()
assert info.all_states is True 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 = """ 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.domains == {"cover"}
assert info.entities == set() assert info.entities == set()
assert info.all_states is False 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): def test_nested_async_render_to_info_case(hass):