Log invalid templates in script delays (#13423)
* Log invalid templates in script delays * Abort on error * Remove unused import
This commit is contained in:
parent
0d48a8eec6
commit
a08293cff7
2 changed files with 36 additions and 5 deletions
|
@ -97,11 +97,16 @@ class Script():
|
||||||
|
|
||||||
delay = action[CONF_DELAY]
|
delay = action[CONF_DELAY]
|
||||||
|
|
||||||
|
try:
|
||||||
if isinstance(delay, template.Template):
|
if isinstance(delay, template.Template):
|
||||||
delay = vol.All(
|
delay = vol.All(
|
||||||
cv.time_period,
|
cv.time_period,
|
||||||
cv.positive_timedelta)(
|
cv.positive_timedelta)(
|
||||||
delay.async_render(variables))
|
delay.async_render(variables))
|
||||||
|
except (TemplateError, vol.Invalid) as ex:
|
||||||
|
_LOGGER.error("Error rendering '%s' delay template: %s",
|
||||||
|
self.name, ex)
|
||||||
|
break
|
||||||
|
|
||||||
unsub = async_track_point_in_utc_time(
|
unsub = async_track_point_in_utc_time(
|
||||||
self.hass, async_script_delay,
|
self.hass, async_script_delay,
|
||||||
|
|
|
@ -218,6 +218,32 @@ class TestScriptHelper(unittest.TestCase):
|
||||||
assert not script_obj.is_running
|
assert not script_obj.is_running
|
||||||
assert len(events) == 2
|
assert len(events) == 2
|
||||||
|
|
||||||
|
def test_delay_invalid_template(self):
|
||||||
|
"""Test the delay as a template that fails."""
|
||||||
|
event = 'test_event'
|
||||||
|
events = []
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def record_event(event):
|
||||||
|
"""Add recorded event to set."""
|
||||||
|
events.append(event)
|
||||||
|
|
||||||
|
self.hass.bus.listen(event, record_event)
|
||||||
|
|
||||||
|
script_obj = script.Script(self.hass, cv.SCRIPT_SCHEMA([
|
||||||
|
{'event': event},
|
||||||
|
{'delay': '{{ invalid_delay }}'},
|
||||||
|
{'delay': {'seconds': 5}},
|
||||||
|
{'event': event}]))
|
||||||
|
|
||||||
|
with mock.patch.object(script, '_LOGGER') as mock_logger:
|
||||||
|
script_obj.run()
|
||||||
|
self.hass.block_till_done()
|
||||||
|
assert mock_logger.error.called
|
||||||
|
|
||||||
|
assert not script_obj.is_running
|
||||||
|
assert len(events) == 1
|
||||||
|
|
||||||
def test_cancel_while_delay(self):
|
def test_cancel_while_delay(self):
|
||||||
"""Test the cancelling while the delay is present."""
|
"""Test the cancelling while the delay is present."""
|
||||||
event = 'test_event'
|
event = 'test_event'
|
||||||
|
|
Loading…
Add table
Reference in a new issue