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

@ -30,6 +30,7 @@ CONF_EVENT_DATA = 'event_data'
CONF_EVENT_DATA_TEMPLATE = 'event_data_template'
CONF_DELAY = 'delay'
CONF_WAIT_TEMPLATE = 'wait_template'
CONF_CONTINUE = 'continue_on_timeout'
def call_from_config(hass: HomeAssistant, config: ConfigType,
@ -143,7 +144,8 @@ class Script():
self.hass.async_add_job(self._change_listener)
if CONF_TIMEOUT in action:
self._async_set_timeout(action, variables)
self._async_set_timeout(
action, variables, action.get(CONF_CONTINUE, True))
return
@ -214,17 +216,23 @@ class Script():
self._log("Test condition {}: {}".format(self.last_action, check))
return check
def _async_set_timeout(self, action, variables):
"""Schedule a timeout to abort script."""
def _async_set_timeout(self, action, variables, continue_on_timeout=True):
"""Schedule a timeout to abort or continue script."""
timeout = action[CONF_TIMEOUT]
unsub = None
@callback
def async_script_timeout(now):
"""Call after timeout is retrieve stop script."""
"""Call after timeout is retrieve."""
self._async_listener.remove(unsub)
self._log("Timeout reached, abort script.")
self.async_stop()
# Check if we want to continue to execute
# the script after the timeout
if continue_on_timeout:
self.hass.async_add_job(self.async_run(variables))
else:
self._log("Timeout reached, abort script.")
self.async_stop()
unsub = async_track_point_in_utc_time(
self.hass, async_script_timeout,