From 51da605b9f35d29e9b00fbf9a4eae0aca9123c3c Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 4 Oct 2020 15:40:04 -0500 Subject: [PATCH] Remove manual rate_limit control directive from templates (#41225) Increase default rate limit for all states and entire domain states to one minute Ensure specifically referenced entities are excluded from the rate limit --- homeassistant/helpers/event.py | 21 +++- homeassistant/helpers/template.py | 25 +--- tests/helpers/test_event.py | 196 +++++------------------------- tests/helpers/test_template.py | 27 +--- 4 files changed, 51 insertions(+), 218 deletions(-) diff --git a/homeassistant/helpers/event.py b/homeassistant/helpers/event.py index d143ed1d106..b6d59bb500c 100644 --- a/homeassistant/helpers/event.py +++ b/homeassistant/helpers/event.py @@ -802,7 +802,7 @@ class _TrackTemplateResultInfo: if self._rate_limit.async_schedule_action( template, - info.rate_limit or track_template_.rate_limit, + _rate_limit_for_event(event, info, track_template_), now, self._refresh, event, @@ -1376,3 +1376,22 @@ def _event_triggers_rerender(event: Event, info: RenderInfo) -> bool: return False return bool(info.filter_lifecycle(entity_id)) + + +@callback +def _rate_limit_for_event( + event: Event, info: RenderInfo, track_template_: TrackTemplate +) -> Optional[timedelta]: + """Determine the rate limit for an event.""" + entity_id = event.data.get(ATTR_ENTITY_ID) + + # Specifically referenced entities are excluded + # from the rate limit + if entity_id in info.entities: + return None + + if track_template_.rate_limit is not None: + return track_template_.rate_limit + + rate_limit: Optional[timedelta] = info.rate_limit + return rate_limit diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index b877e0b0e12..c0ceb17fdd7 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -72,7 +72,7 @@ _COLLECTABLE_STATE_ATTRIBUTES = { "name", } -DEFAULT_RATE_LIMIT = timedelta(seconds=1) +DEFAULT_RATE_LIMIT = timedelta(minutes=1) @bind_hass @@ -489,26 +489,6 @@ class Template: return 'Template("' + self.template + '")' -class RateLimit: - """Class to control update rate limits.""" - - def __init__(self, hass: HomeAssistantType): - """Initialize rate limit.""" - self._hass = hass - - def __call__(self, *args: Any, **kwargs: Any) -> str: - """Handle a call to the class.""" - render_info = self._hass.data.get(_RENDER_INFO) - if render_info is not None: - render_info.rate_limit = timedelta(*args, **kwargs) - - return "" - - def __repr__(self) -> str: - """Representation of a RateLimit.""" - return "