Allow wait template to run the remainder of the script (#15836)

* Adding new feature to allow a wait template to run the remainer of the script on timeout

* Styling changes

* Fixing file permissions, adding test for new code

* changed variable name, refactored script to pass information into async_set_timeout

* Changing the default behaviour to continue to run the script after timeout
This commit is contained in:
Hovo (Luke) 2018-08-13 19:23:27 +10:00 committed by Paulus Schoutsen
parent 2342709803
commit 6aee535d7c
3 changed files with 94 additions and 9 deletions

View file

@ -375,8 +375,84 @@ class TestScriptHelper(unittest.TestCase):
assert script_obj.can_cancel
assert len(events) == 2
def test_wait_template_timeout(self):
"""Test the wait template."""
def test_wait_template_timeout_halt(self):
"""Test the wait template, halt on timeout."""
event = 'test_event'
events = []
@callback
def record_event(event):
"""Add recorded event to set."""
events.append(event)
self.hass.bus.listen(event, record_event)
self.hass.states.set('switch.test', 'on')
script_obj = script.Script(self.hass, cv.SCRIPT_SCHEMA([
{'event': event},
{
'wait_template': "{{states.switch.test.state == 'off'}}",
'continue_on_timeout': False,
'timeout': 5
},
{'event': event}]))
script_obj.run()
self.hass.block_till_done()
assert script_obj.is_running
assert script_obj.can_cancel
assert script_obj.last_action == event
assert len(events) == 1
future = dt_util.utcnow() + timedelta(seconds=5)
fire_time_changed(self.hass, future)
self.hass.block_till_done()
assert not script_obj.is_running
assert len(events) == 1
def test_wait_template_timeout_continue(self):
"""Test the wait template with continuing the script."""
event = 'test_event'
events = []
@callback
def record_event(event):
"""Add recorded event to set."""
events.append(event)
self.hass.bus.listen(event, record_event)
self.hass.states.set('switch.test', 'on')
script_obj = script.Script(self.hass, cv.SCRIPT_SCHEMA([
{'event': event},
{
'wait_template': "{{states.switch.test.state == 'off'}}",
'timeout': 5,
'continue_on_timeout': True
},
{'event': event}]))
script_obj.run()
self.hass.block_till_done()
assert script_obj.is_running
assert script_obj.can_cancel
assert script_obj.last_action == event
assert len(events) == 1
future = dt_util.utcnow() + timedelta(seconds=5)
fire_time_changed(self.hass, future)
self.hass.block_till_done()
assert not script_obj.is_running
assert len(events) == 2
def test_wait_template_timeout_default(self):
"""Test the wait template with default contiune."""
event = 'test_event'
events = []
@ -410,7 +486,7 @@ class TestScriptHelper(unittest.TestCase):
self.hass.block_till_done()
assert not script_obj.is_running
assert len(events) == 1
assert len(events) == 2
def test_wait_template_variables(self):
"""Test the wait template with variables."""