mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 04:07:08 +00:00
Add toggle support for scripts.
This commit is contained in:
parent
b961b5037f
commit
7dd529356a
@ -15,7 +15,7 @@ from itertools import islice
|
|||||||
import homeassistant.util.dt as date_util
|
import homeassistant.util.dt as date_util
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ENTITY_ID, EVENT_TIME_CHANGED, SERVICE_TURN_OFF, SERVICE_TURN_ON,
|
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 import ToggleEntity, split_entity_id
|
||||||
from homeassistant.helpers.entity_component import EntityComponent
|
from homeassistant.helpers.entity_component import EntityComponent
|
||||||
from homeassistant.helpers.event import track_point_in_utc_time
|
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})
|
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):
|
def setup(hass, config):
|
||||||
""" Load the scripts from the configuration. """
|
""" Load the scripts from the configuration. """
|
||||||
|
|
||||||
@ -103,8 +108,14 @@ def setup(hass, config):
|
|||||||
for script in component.extract_from_service(service):
|
for script in component.extract_from_service(service):
|
||||||
script.turn_off()
|
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_ON, turn_on_service)
|
||||||
hass.services.register(DOMAIN, SERVICE_TURN_OFF, turn_off_service)
|
hass.services.register(DOMAIN, SERVICE_TURN_OFF, turn_off_service)
|
||||||
|
hass.services.register(DOMAIN, SERVICE_TOGGLE, toggle_service)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -269,3 +269,36 @@ class TestScript(unittest.TestCase):
|
|||||||
script.turn_on(self.hass, ENTITY_ID)
|
script.turn_on(self.hass, ENTITY_ID)
|
||||||
self.hass.pool.block_till_done()
|
self.hass.pool.block_till_done()
|
||||||
self.assertEqual(0, len(calls))
|
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