Only turn on scripts that are not currently running.

Prevents an errant API call from advancing a currently executing delay
step before it should.
This commit is contained in:
Andrew Thigpen 2016-02-21 11:11:35 -06:00
parent ff9568ad26
commit b961b5037f
2 changed files with 43 additions and 2 deletions

View File

@ -70,8 +70,12 @@ def setup(hass, config):
""" Execute a service call to script.<script name>. """
entity_id = ENTITY_ID_FORMAT.format(service.service)
script = component.entities.get(entity_id)
if script:
script.turn_on()
if not script:
return
if script.is_on:
_LOGGER.warning("Script %s already running.", entity_id)
return
script.turn_on()
for object_id, cfg in config[DOMAIN].items():
if object_id != slugify(object_id):

View File

@ -232,3 +232,40 @@ class TestScript(unittest.TestCase):
self.assertFalse(script.is_on(self.hass, ENTITY_ID))
self.assertEqual(0, len(calls))
def test_turn_on_service(self):
"""
Verifies that the turn_on service for a script only turns on scripts
that are not currently running.
"""
event = 'test_event'
calls = []
def record_event(event):
calls.append(event)
self.hass.bus.listen(event, record_event)
self.assertTrue(script.setup(self.hass, {
'script': {
'test': {
'sequence': [{
'delay': {
'seconds': 5
}
}, {
'event': event,
}]
}
}
}))
script.turn_on(self.hass, ENTITY_ID)
self.hass.pool.block_till_done()
self.assertTrue(script.is_on(self.hass, ENTITY_ID))
self.assertEqual(0, len(calls))
# calling turn_on a second time should not advance the script
script.turn_on(self.hass, ENTITY_ID)
self.hass.pool.block_till_done()
self.assertEqual(0, len(calls))