From f837da6fe389beeaf991ff40247f086fb1161d9a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 22 Sep 2020 09:28:02 -0500 Subject: [PATCH] Defer template tracking setup until template entity start (#40388) --- .../components/template/template_entity.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/template/template_entity.py b/homeassistant/components/template/template_entity.py index 632eeea8926..53d9d2f1a33 100644 --- a/homeassistant/components/template/template_entity.py +++ b/homeassistant/components/template/template_entity.py @@ -213,7 +213,6 @@ class TemplateEntity(Entity): attribute = _TemplateAttribute( self, attribute, template, validator, on_update, none_on_template_error ) - attribute.async_setup() self._template_attrs.setdefault(template, []) self._template_attrs[template].append(attribute) @@ -252,22 +251,21 @@ class TemplateEntity(Entity): event, update.template, update.last_result, update.result ) - if self._async_update: - self.async_write_ha_state() + self.async_write_ha_state() async def _async_template_startup(self, *_) -> None: - # _handle_results will not write state until "_async_update" is set - template_var_tups = [ - TrackTemplate(template, None) for template in self._template_attrs - ] + template_var_tups = [] + for template, attributes in self._template_attrs.items(): + template_var_tups.append(TrackTemplate(template, None)) + for attribute in attributes: + attribute.async_setup() result_info = async_track_template_result( self.hass, template_var_tups, self._handle_results ) self.async_on_remove(result_info.async_remove) - result_info.async_refresh() - self.async_write_ha_state() self._async_update = result_info.async_refresh + result_info.async_refresh() async def async_added_to_hass(self) -> None: """Run when entity about to be added to hass."""