mirror of
https://github.com/home-assistant/core.git
synced 2025-08-02 12:15:08 +02:00
Small performance improvement in tracking template results (#110622)
- Avoid inner function creation each refresh - remove extra unneeded checks from ratelimit
This commit is contained in:
@@ -1106,6 +1106,24 @@ class TrackTemplateResultInfo:
|
|||||||
|
|
||||||
return result_as_boolean(result)
|
return result_as_boolean(result)
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _apply_update(
|
||||||
|
self,
|
||||||
|
updates: list[TrackTemplateResult],
|
||||||
|
update: bool | TrackTemplateResult,
|
||||||
|
template: Template,
|
||||||
|
) -> bool:
|
||||||
|
"""Handle updates of a tracked template."""
|
||||||
|
if not update:
|
||||||
|
return False
|
||||||
|
|
||||||
|
self._setup_time_listener(template, self._info[template].has_time)
|
||||||
|
|
||||||
|
if isinstance(update, TrackTemplateResult):
|
||||||
|
updates.append(update)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _refresh(
|
def _refresh(
|
||||||
self,
|
self,
|
||||||
@@ -1129,20 +1147,6 @@ class TrackTemplateResultInfo:
|
|||||||
info_changed = False
|
info_changed = False
|
||||||
now = event.time_fired if not replayed and event else dt_util.utcnow()
|
now = event.time_fired if not replayed and event else dt_util.utcnow()
|
||||||
|
|
||||||
def _apply_update(
|
|
||||||
update: bool | TrackTemplateResult, template: Template
|
|
||||||
) -> bool:
|
|
||||||
"""Handle updates of a tracked template."""
|
|
||||||
if not update:
|
|
||||||
return False
|
|
||||||
|
|
||||||
self._setup_time_listener(template, self._info[template].has_time)
|
|
||||||
|
|
||||||
if isinstance(update, TrackTemplateResult):
|
|
||||||
updates.append(update)
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
block_updates = False
|
block_updates = False
|
||||||
super_template = self._track_templates[0] if self._has_super_template else None
|
super_template = self._track_templates[0] if self._has_super_template else None
|
||||||
|
|
||||||
@@ -1151,7 +1155,7 @@ class TrackTemplateResultInfo:
|
|||||||
# Update the super template first
|
# Update the super template first
|
||||||
if super_template is not None:
|
if super_template is not None:
|
||||||
update = self._render_template_if_ready(super_template, now, event)
|
update = self._render_template_if_ready(super_template, now, event)
|
||||||
info_changed |= _apply_update(update, super_template.template)
|
info_changed |= self._apply_update(updates, update, super_template.template)
|
||||||
|
|
||||||
if isinstance(update, TrackTemplateResult):
|
if isinstance(update, TrackTemplateResult):
|
||||||
super_result = update.result
|
super_result = update.result
|
||||||
@@ -1182,7 +1186,9 @@ class TrackTemplateResultInfo:
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
update = self._render_template_if_ready(track_template_, now, event)
|
update = self._render_template_if_ready(track_template_, now, event)
|
||||||
info_changed |= _apply_update(update, track_template_.template)
|
info_changed |= self._apply_update(
|
||||||
|
updates, update, track_template_.template
|
||||||
|
)
|
||||||
|
|
||||||
if info_changed:
|
if info_changed:
|
||||||
assert self._track_state_changes
|
assert self._track_state_changes
|
||||||
|
@@ -30,9 +30,7 @@ class KeyedRateLimit:
|
|||||||
@callback
|
@callback
|
||||||
def async_has_timer(self, key: Hashable) -> bool:
|
def async_has_timer(self, key: Hashable) -> bool:
|
||||||
"""Check if a rate limit timer is running."""
|
"""Check if a rate limit timer is running."""
|
||||||
if not self._rate_limit_timers:
|
return bool(self._rate_limit_timers and key in self._rate_limit_timers)
|
||||||
return False
|
|
||||||
return key in self._rate_limit_timers
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_triggered(self, key: Hashable, now: datetime | None = None) -> None:
|
def async_triggered(self, key: Hashable, now: datetime | None = None) -> None:
|
||||||
@@ -43,7 +41,7 @@ class KeyedRateLimit:
|
|||||||
@callback
|
@callback
|
||||||
def async_cancel_timer(self, key: Hashable) -> None:
|
def async_cancel_timer(self, key: Hashable) -> None:
|
||||||
"""Cancel a rate limit time that will call the action."""
|
"""Cancel a rate limit time that will call the action."""
|
||||||
if not self._rate_limit_timers or not self.async_has_timer(key):
|
if not self._rate_limit_timers or key not in self._rate_limit_timers:
|
||||||
return
|
return
|
||||||
|
|
||||||
self._rate_limit_timers.pop(key).cancel()
|
self._rate_limit_timers.pop(key).cancel()
|
||||||
|
Reference in New Issue
Block a user