mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 16:27:08 +00:00
Merge pull request #1358 from andythigpen/script-improvements
Script improvements
This commit is contained in:
commit
aa748e3e48
@ -15,7 +15,7 @@ from itertools import islice
|
||||
import homeassistant.util.dt as date_util
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID, EVENT_TIME_CHANGED, SERVICE_TURN_OFF, SERVICE_TURN_ON,
|
||||
STATE_ON)
|
||||
SERVICE_TOGGLE, STATE_ON)
|
||||
from homeassistant.helpers.entity import ToggleEntity, split_entity_id
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
from homeassistant.helpers.event import track_point_in_utc_time
|
||||
@ -61,6 +61,11 @@ def turn_off(hass, entity_id):
|
||||
hass.services.call(DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: entity_id})
|
||||
|
||||
|
||||
def toggle(hass, entity_id):
|
||||
""" Toggles script. """
|
||||
hass.services.call(DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: entity_id})
|
||||
|
||||
|
||||
def setup(hass, config):
|
||||
""" Load the scripts from the configuration. """
|
||||
|
||||
@ -70,8 +75,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):
|
||||
@ -99,8 +108,14 @@ def setup(hass, config):
|
||||
for script in component.extract_from_service(service):
|
||||
script.turn_off()
|
||||
|
||||
def toggle_service(service):
|
||||
""" Toggles a script. """
|
||||
for script in component.extract_from_service(service):
|
||||
script.toggle()
|
||||
|
||||
hass.services.register(DOMAIN, SERVICE_TURN_ON, turn_on_service)
|
||||
hass.services.register(DOMAIN, SERVICE_TURN_OFF, turn_off_service)
|
||||
hass.services.register(DOMAIN, SERVICE_TOGGLE, toggle_service)
|
||||
|
||||
return True
|
||||
|
||||
|
@ -232,3 +232,73 @@ 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))
|
||||
|
||||
def test_toggle_service(self):
|
||||
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.toggle(self.hass, ENTITY_ID)
|
||||
self.hass.pool.block_till_done()
|
||||
self.assertTrue(script.is_on(self.hass, ENTITY_ID))
|
||||
self.assertEqual(0, len(calls))
|
||||
|
||||
script.toggle(self.hass, ENTITY_ID)
|
||||
self.hass.pool.block_till_done()
|
||||
self.assertFalse(script.is_on(self.hass, ENTITY_ID))
|
||||
self.assertEqual(0, len(calls))
|
||||
|
Loading…
x
Reference in New Issue
Block a user