Detect self-referencing loops in template entities and log a warning (#39897)

This commit is contained in:
J. Nick Koston authored and GitHub committed 2020-09-10 20:50:11 +02:00
1 parent bedc1e5672
commit fd8a4182d9
3 files changed
+68

No files matched your search

@@ -121,6 +121,7 @@ class TemplateEntity(Entity):
"""Template Entity."""
self._template_attrs = {}
self._async_update = None
self._async_update_entity_ids_filter = None
self._attribute_templates = attribute_templates
self._attributes = {}
self._availability_template = availability_template
@@ -231,6 +232,9 @@ class TemplateEntity(Entity):
event, update.template, update.last_result, update.result
)
if self._async_update_entity_ids_filter:
self._async_update_entity_ids_filter({self.entity_id})
if self._async_update:
self.async_write_ha_state()
@@ -245,8 +249,12 @@ class TemplateEntity(Entity):
)
self.async_on_remove(result_info.async_remove)
result_info.async_refresh()
result_info.async_update_entity_ids_filter({self.entity_id})
self.async_write_ha_state()
self._async_update = result_info.async_refresh
self._async_update_entity_ids_filter = (
result_info.async_update_entity_ids_filter
)
async def async_added_to_hass(self) -> None:
"""Run when entity about to be added to hass."""
+16
View File
@@ -508,6 +508,7 @@ class _TrackTemplateResultInfo:
self._info: Dict[Template, RenderInfo] = {}
self._last_domains: Set = set()
self._last_entities: Set = set()
self._entity_ids_filter: Set = set()
def async_setup(self) -> None:
"""Activation of template tracking."""
@@ -659,12 +660,27 @@ class _TrackTemplateResultInfo:
"""Force recalculate the template."""
self._refresh(None)
@callback
def async_update_entity_ids_filter(self, entity_ids: Set) -> None:
"""Update the filtered entity_ids."""
self._entity_ids_filter = entity_ids
@callback
def _refresh(self, event: Optional[Event]) -> None:
entity_id = event and event.data.get(ATTR_ENTITY_ID)
updates = []
info_changed = False
if entity_id and entity_id in self._entity_ids_filter:
# Skip self-referencing updates
for track_template_ in self._track_templates:
_LOGGER.warning(
"Template loop detected while processing event: %s, skipping template render for Template[%s]",
event,
track_template_.template.template,
)
return
for track_template_ in self._track_templates:
template = track_template_.template
if (