mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Script helper: support variables
This commit is contained in:
parent
09a771a026
commit
26863284b6
@ -47,7 +47,7 @@ class Script():
|
|||||||
"""Return true if script is on."""
|
"""Return true if script is on."""
|
||||||
return self._cur != -1
|
return self._cur != -1
|
||||||
|
|
||||||
def run(self):
|
def run(self, variables=None):
|
||||||
"""Run script."""
|
"""Run script."""
|
||||||
with self._lock:
|
with self._lock:
|
||||||
if self._cur == -1:
|
if self._cur == -1:
|
||||||
@ -66,7 +66,7 @@ class Script():
|
|||||||
def script_delay(now):
|
def script_delay(now):
|
||||||
"""Called after delay is done."""
|
"""Called after delay is done."""
|
||||||
self._delay_listener = None
|
self._delay_listener = None
|
||||||
self.run()
|
self.run(variables)
|
||||||
|
|
||||||
self._delay_listener = track_point_in_utc_time(
|
self._delay_listener = track_point_in_utc_time(
|
||||||
self.hass, script_delay,
|
self.hass, script_delay,
|
||||||
@ -77,7 +77,7 @@ class Script():
|
|||||||
return
|
return
|
||||||
|
|
||||||
elif service.validate_service_call(action) is None:
|
elif service.validate_service_call(action) is None:
|
||||||
self._call_service(action)
|
self._call_service(action, variables)
|
||||||
|
|
||||||
elif CONF_EVENT in action:
|
elif CONF_EVENT in action:
|
||||||
self._fire_event(action)
|
self._fire_event(action)
|
||||||
@ -98,11 +98,11 @@ class Script():
|
|||||||
if self._change_listener:
|
if self._change_listener:
|
||||||
self._change_listener()
|
self._change_listener()
|
||||||
|
|
||||||
def _call_service(self, action):
|
def _call_service(self, action, variables):
|
||||||
"""Call the service specified in the action."""
|
"""Call the service specified in the action."""
|
||||||
self.last_action = action.get(CONF_ALIAS, 'call service')
|
self.last_action = action.get(CONF_ALIAS, 'call service')
|
||||||
self._log("Executing step %s", self.last_action)
|
self._log("Executing step %s", self.last_action)
|
||||||
service.call_from_config(self.hass, action, True)
|
service.call_from_config(self.hass, action, True, variables)
|
||||||
|
|
||||||
def _fire_event(self, action):
|
def _fire_event(self, action):
|
||||||
"""Fire an event."""
|
"""Fire an event."""
|
||||||
|
@ -173,3 +173,47 @@ class TestScriptHelper(unittest.TestCase):
|
|||||||
|
|
||||||
assert not script_obj.is_running
|
assert not script_obj.is_running
|
||||||
assert len(events) == 0
|
assert len(events) == 0
|
||||||
|
|
||||||
|
def test_passing_variables_to_script(self):
|
||||||
|
"""Test if we can pass variables to script."""
|
||||||
|
calls = []
|
||||||
|
|
||||||
|
def record_call(service):
|
||||||
|
"""Add recorded event to set."""
|
||||||
|
calls.append(service)
|
||||||
|
|
||||||
|
self.hass.services.register('test', 'script', record_call)
|
||||||
|
|
||||||
|
script_obj = script.Script(self.hass, [
|
||||||
|
{
|
||||||
|
'service': 'test.script',
|
||||||
|
'data_template': {
|
||||||
|
'hello': '{{ greeting }}',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{'delay': {'seconds': 5}},
|
||||||
|
{
|
||||||
|
'service': 'test.script',
|
||||||
|
'data_template': {
|
||||||
|
'hello': '{{ greeting2 }}',
|
||||||
|
},
|
||||||
|
}])
|
||||||
|
|
||||||
|
script_obj.run({
|
||||||
|
'greeting': 'world',
|
||||||
|
'greeting2': 'universe',
|
||||||
|
})
|
||||||
|
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
|
assert script_obj.is_running
|
||||||
|
assert len(calls) == 1
|
||||||
|
assert calls[-1].data['hello'] == 'world'
|
||||||
|
|
||||||
|
future = dt_util.utcnow() + timedelta(seconds=5)
|
||||||
|
fire_time_changed(self.hass, future)
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
|
assert not script_obj.is_running
|
||||||
|
assert len(calls) == 2
|
||||||
|
assert calls[-1].data['hello'] == 'universe'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user