Add last triggered to script (#5261)
* Add last triggered to script * Add tests for script last_triggeredpull/3734/merge
parent
82d037a828
commit
467cb18625
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue