Add context to scripts and automations (#16415)

* Add context to script helper

* Update script component

* Add context to automations

* Lint
This commit is contained in:
Paulus Schoutsen 2018-09-04 21:16:24 +02:00 committed by Pascal Vizeli
parent e1501c83f8
commit 746f4ac158
17 changed files with 164 additions and 144 deletions

View file

@ -4,7 +4,7 @@ from datetime import timedelta
from unittest import mock
import unittest
from homeassistant.core import callback
from homeassistant.core import Context, callback
# Otherwise can't test just this file (import order issue)
import homeassistant.components # noqa
import homeassistant.util.dt as dt_util
@ -32,6 +32,7 @@ class TestScriptHelper(unittest.TestCase):
def test_firing_event(self):
"""Test the firing of events."""
event = 'test_event'
context = Context()
calls = []
@callback
@ -48,17 +49,19 @@ class TestScriptHelper(unittest.TestCase):
}
}))
script_obj.run()
script_obj.run(context=context)
self.hass.block_till_done()
assert len(calls) == 1
assert calls[0].context is context
assert calls[0].data.get('hello') == 'world'
assert not script_obj.can_cancel
def test_firing_event_template(self):
"""Test the firing of events."""
event = 'test_event'
context = Context()
calls = []
@callback
@ -82,11 +85,12 @@ class TestScriptHelper(unittest.TestCase):
}
}))
script_obj.run({'is_world': 'yes'})
script_obj.run({'is_world': 'yes'}, context=context)
self.hass.block_till_done()
assert len(calls) == 1
assert calls[0].context is context
assert calls[0].data == {
'dict': {
1: 'yes',
@ -100,6 +104,7 @@ class TestScriptHelper(unittest.TestCase):
def test_calling_service(self):
"""Test the calling of a service."""
calls = []
context = Context()
@callback
def record_call(service):
@ -113,16 +118,18 @@ class TestScriptHelper(unittest.TestCase):
'data': {
'hello': 'world'
}
})
}, context=context)
self.hass.block_till_done()
assert len(calls) == 1
assert calls[0].context is context
assert calls[0].data.get('hello') == 'world'
def test_calling_service_template(self):
"""Test the calling of a service."""
calls = []
context = Context()
@callback
def record_call(service):
@ -147,17 +154,19 @@ class TestScriptHelper(unittest.TestCase):
{% endif %}
"""
}
}, {'is_world': 'yes'})
}, {'is_world': 'yes'}, context=context)
self.hass.block_till_done()
assert len(calls) == 1
assert calls[0].context is context
assert calls[0].data.get('hello') == 'world'
def test_delay(self):
"""Test the delay."""
event = 'test_event'
events = []
context = Context()
@callback
def record_event(event):
@ -171,7 +180,7 @@ class TestScriptHelper(unittest.TestCase):
{'delay': {'seconds': 5}},
{'event': event}]))
script_obj.run()
script_obj.run(context=context)
self.hass.block_till_done()
assert script_obj.is_running
@ -185,6 +194,8 @@ class TestScriptHelper(unittest.TestCase):
assert not script_obj.is_running
assert len(events) == 2
assert events[0].context is context
assert events[1].context is context
def test_delay_template(self):
"""Test the delay as a template."""
@ -282,6 +293,7 @@ class TestScriptHelper(unittest.TestCase):
"""Test the wait template."""
event = 'test_event'
events = []
context = Context()
@callback
def record_event(event):
@ -297,7 +309,7 @@ class TestScriptHelper(unittest.TestCase):
{'wait_template': "{{states.switch.test.state == 'off'}}"},
{'event': event}]))
script_obj.run()
script_obj.run(context=context)
self.hass.block_till_done()
assert script_obj.is_running
@ -310,6 +322,8 @@ class TestScriptHelper(unittest.TestCase):
assert not script_obj.is_running
assert len(events) == 2
assert events[0].context is context
assert events[1].context is context
def test_wait_template_cancel(self):
"""Test the wait template cancel action."""