diff --git a/homeassistant/components/script.py b/homeassistant/components/script.py index df46fb5a03d..05cbc5d0a80 100644 --- a/homeassistant/components/script.py +++ b/homeassistant/components/script.py @@ -31,6 +31,7 @@ CONF_SEQUENCE = "sequence" ATTR_VARIABLES = 'variables' ATTR_LAST_ACTION = 'last_action' +ATTR_LAST_TRIGGERED = 'last_triggered' ATTR_CAN_CANCEL = 'can_cancel' _LOGGER = logging.getLogger(__name__) @@ -155,6 +156,7 @@ class ScriptEntity(ToggleEntity): def state_attributes(self): """Return the state attributes.""" attrs = {} + attrs[ATTR_LAST_TRIGGERED] = self.script.last_triggered if self.script.can_cancel: attrs[ATTR_CAN_CANCEL] = self.script.can_cancel if self.script.last_action: diff --git a/homeassistant/helpers/script.py b/homeassistant/helpers/script.py index 4d6a2b01df7..46703d86450 100644 --- a/homeassistant/helpers/script.py +++ b/homeassistant/helpers/script.py @@ -46,6 +46,7 @@ class Script(): self._change_listener = change_listener self._cur = -1 self.last_action = None + self.last_triggered = None self.can_cancel = any(CONF_DELAY in action for action in self.sequence) self._async_unsub_delay_listener = None @@ -68,6 +69,7 @@ class Script(): This method is a coroutine. """ + self.last_triggered = date_util.utcnow() if self._cur == -1: self._log('Running script') self._cur = 0 diff --git a/tests/helpers/test_script.py b/tests/helpers/test_script.py index 8787ff7b514..6eee484097b 100644 --- a/tests/helpers/test_script.py +++ b/tests/helpers/test_script.py @@ -353,3 +353,22 @@ class TestScriptHelper(unittest.TestCase): script_obj.run() self.hass.block_till_done() assert len(script_obj._config_cache) == 2 + + def test_last_triggered(self): + """Test the last_triggered.""" + event = 'test_event' + + script_obj = script.Script(self.hass, cv.SCRIPT_SCHEMA([ + {'event': event}, + {'delay': {'seconds': 5}}, + {'event': event}])) + + assert script_obj.last_triggered is None + + time = dt_util.utcnow() + with mock.patch('homeassistant.helpers.script.date_util.utcnow', + return_value=time): + script_obj.run() + self.hass.block_till_done() + + assert script_obj.last_triggered == time