Code cleanups for async_track_template_result (#40737)

This commit is contained in:
J. Nick Koston 2020-09-30 06:00:21 -05:00 committed by GitHub
parent d6df1527f1
commit 205cf57a77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -560,7 +560,6 @@ class _TrackTemplateResultInfo:
self._listeners: Dict[str, Callable] = {} self._listeners: Dict[str, Callable] = {}
self._last_result: Dict[Template, Union[str, TemplateError]] = {} self._last_result: Dict[Template, Union[str, TemplateError]] = {}
self._last_info: Dict[Template, RenderInfo] = {}
self._info: Dict[Template, RenderInfo] = {} self._info: Dict[Template, RenderInfo] = {}
self._last_domains: Set = set() self._last_domains: Set = set()
self._last_entities: Set = set() self._last_entities: Set = set()
@ -581,7 +580,6 @@ class _TrackTemplateResultInfo:
exc_info=self._info[template].exception, exc_info=self._info[template].exception,
) )
self._last_info = self._info.copy()
self._create_listeners() self._create_listeners()
_LOGGER.debug( _LOGGER.debug(
"Template group %s listens for %s", "Template group %s listens for %s",
@ -600,28 +598,23 @@ class _TrackTemplateResultInfo:
@property @property
def _needs_all_listener(self) -> bool: def _needs_all_listener(self) -> bool:
for track_template_ in self._track_templates: for info in self._info.values():
template = track_template_.template
# Tracking all states # Tracking all states
if ( if info.all_states or info.all_states_lifecycle:
self._info[template].all_states
or self._info[template].all_states_lifecycle
):
return True return True
# Previous call had an exception # Previous call had an exception
# so we do not know which states # so we do not know which states
# to track # to track
if self._info[template].exception: if info.exception:
return True return True
return False return False
@property @property
def _all_templates_are_static(self) -> bool: def _all_templates_are_static(self) -> bool:
for track_template_ in self._track_templates: for info in self._info.values():
if not self._info[track_template_.template].is_static: if not info.is_static:
return False return False
return True return True
@ -712,9 +705,8 @@ class _TrackTemplateResultInfo:
@callback @callback
def async_remove(self) -> None: def async_remove(self) -> None:
"""Cancel the listener.""" """Cancel the listener."""
self._cancel_listener(_TEMPLATE_ALL_LISTENER) for key in list(self._listeners):
self._cancel_listener(_TEMPLATE_DOMAINS_LISTENER) self._listeners.pop(key)()
self._cancel_listener(_TEMPLATE_ENTITIES_LISTENER)
@callback @callback
def async_refresh(self) -> None: def async_refresh(self) -> None:
@ -722,31 +714,32 @@ class _TrackTemplateResultInfo:
self._refresh(None) self._refresh(None)
@callback @callback
def _refresh(self, event: Optional[Event]) -> None: def _event_triggers_template(self, template: Template, event: Event) -> bool:
entity_id = event and event.data.get(ATTR_ENTITY_ID) """Determine if a template should be re-rendered from an event."""
lifecycle_event = event and ( entity_id = event.data.get(ATTR_ENTITY_ID)
event.data.get("new_state") is None or event.data.get("old_state") is None return (
self._info[template].filter(entity_id)
or event.data.get("new_state") is None
or event.data.get("old_state") is None
and self._info[template].filter_lifecycle(entity_id)
) )
@callback
def _refresh(self, event: Optional[Event]) -> None:
updates = [] updates = []
info_changed = False info_changed = False
for track_template_ in self._track_templates: for track_template_ in self._track_templates:
template = track_template_.template template = track_template_.template
if ( if event:
entity_id if not self._event_triggers_template(template, event):
and not self._last_info[template].filter(entity_id) continue
and (
not lifecycle_event
or not self._last_info[template].filter_lifecycle(entity_id)
)
):
continue
_LOGGER.debug( _LOGGER.debug(
"Template update %s triggered by event: %s", "Template update %s triggered by event: %s",
template.template, template.template,
event, event,
) )
self._info[template] = template.async_render_to_info( self._info[template] = template.async_render_to_info(
track_template_.variables track_template_.variables
@ -778,7 +771,6 @@ class _TrackTemplateResultInfo:
self._track_templates, self._track_templates,
self.listeners, self.listeners,
) )
self._last_info = self._info.copy()
if not updates: if not updates:
return return