Ensure template tracking can recover after the template generates an exception (#39256)

This commit is contained in:
J. Nick Koston 2020-08-25 17:33:08 -05:00 committed by GitHub
parent 90842fcb84
commit c87e03ee6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 0 deletions

View File

@ -562,6 +562,11 @@ class _TrackTemplateResultInfo:
entities = set(self._info.entities)
for entity_id in self.hass.states.async_entity_ids(self._info.domains):
entities.add(entity_id)
# Entities has changed to none
if not entities:
return
self._entities_listener = async_track_state_change_event(
self.hass, entities, self._refresh
)
@ -570,6 +575,10 @@ class _TrackTemplateResultInfo:
def _setup_domains_listener(self) -> None:
assert self._info
# Domains has changed to none
if not self._info.domains:
return
self._domains_listener = async_track_state_added_domain(
self.hass, self._info.domains, self._refresh
)

View File

@ -543,6 +543,33 @@ async def test_track_template_error(hass, caplog):
assert "TemplateAssertionError" not in caplog.text
async def test_track_template_error_can_recover(hass, caplog):
"""Test tracking template with error."""
hass.states.async_set("switch.data_system", "cow", {"opmode": 0})
template_error = Template(
"{{ states.sensor.data_system.attributes['opmode'] == '0' }}", hass
)
error_calls = []
@ha.callback
def error_callback(entity_id, old_state, new_state):
error_calls.append((entity_id, old_state, new_state))
async_track_template(hass, template_error, error_callback)
await hass.async_block_till_done()
assert not error_calls
hass.states.async_remove("switch.data_system")
assert "UndefinedError" in caplog.text
hass.states.async_set("switch.data_system", "cow", {"opmode": 0})
caplog.clear()
assert "UndefinedError" not in caplog.text
async def test_track_template_result(hass):
"""Test tracking template."""
specific_runs = []