mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Black
This commit is contained in:
@@ -11,6 +11,7 @@ import pytest
|
||||
|
||||
from homeassistant import exceptions
|
||||
from homeassistant.core import Context, callback
|
||||
|
||||
# Otherwise can't test just this file (import order issue)
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.helpers import script, config_validation as cv
|
||||
@@ -18,12 +19,12 @@ from homeassistant.helpers import script, config_validation as cv
|
||||
from tests.common import async_fire_time_changed
|
||||
|
||||
|
||||
ENTITY_ID = 'script.test'
|
||||
ENTITY_ID = "script.test"
|
||||
|
||||
|
||||
async def test_firing_event(hass):
|
||||
"""Test the firing of events."""
|
||||
event = 'test_event'
|
||||
event = "test_event"
|
||||
context = Context()
|
||||
calls = []
|
||||
|
||||
@@ -34,12 +35,9 @@ async def test_firing_event(hass):
|
||||
|
||||
hass.bus.async_listen(event, record_event)
|
||||
|
||||
script_obj = script.Script(hass, cv.SCRIPT_SCHEMA({
|
||||
'event': event,
|
||||
'event_data': {
|
||||
'hello': 'world'
|
||||
}
|
||||
}))
|
||||
script_obj = script.Script(
|
||||
hass, cv.SCRIPT_SCHEMA({"event": event, "event_data": {"hello": "world"}})
|
||||
)
|
||||
|
||||
await script_obj.async_run(context=context)
|
||||
|
||||
@@ -47,13 +45,13 @@ async def test_firing_event(hass):
|
||||
|
||||
assert len(calls) == 1
|
||||
assert calls[0].context is context
|
||||
assert calls[0].data.get('hello') == 'world'
|
||||
assert calls[0].data.get("hello") == "world"
|
||||
assert not script_obj.can_cancel
|
||||
|
||||
|
||||
async def test_firing_event_template(hass):
|
||||
"""Test the firing of events."""
|
||||
event = 'test_event'
|
||||
event = "test_event"
|
||||
context = Context()
|
||||
calls = []
|
||||
|
||||
@@ -64,33 +62,32 @@ async def test_firing_event_template(hass):
|
||||
|
||||
hass.bus.async_listen(event, record_event)
|
||||
|
||||
script_obj = script.Script(hass, cv.SCRIPT_SCHEMA({
|
||||
'event': event,
|
||||
'event_data_template': {
|
||||
'dict': {
|
||||
1: '{{ is_world }}',
|
||||
2: '{{ is_world }}{{ is_world }}',
|
||||
3: '{{ is_world }}{{ is_world }}{{ is_world }}',
|
||||
},
|
||||
'list': [
|
||||
'{{ is_world }}', '{{ is_world }}{{ is_world }}'
|
||||
]
|
||||
}
|
||||
}))
|
||||
script_obj = script.Script(
|
||||
hass,
|
||||
cv.SCRIPT_SCHEMA(
|
||||
{
|
||||
"event": event,
|
||||
"event_data_template": {
|
||||
"dict": {
|
||||
1: "{{ is_world }}",
|
||||
2: "{{ is_world }}{{ is_world }}",
|
||||
3: "{{ is_world }}{{ is_world }}{{ is_world }}",
|
||||
},
|
||||
"list": ["{{ is_world }}", "{{ is_world }}{{ is_world }}"],
|
||||
},
|
||||
}
|
||||
),
|
||||
)
|
||||
|
||||
await script_obj.async_run({'is_world': 'yes'}, context=context)
|
||||
await script_obj.async_run({"is_world": "yes"}, context=context)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
assert calls[0].context is context
|
||||
assert calls[0].data == {
|
||||
'dict': {
|
||||
1: 'yes',
|
||||
2: 'yesyes',
|
||||
3: 'yesyesyes',
|
||||
},
|
||||
'list': ['yes', 'yesyes']
|
||||
"dict": {1: "yes", 2: "yesyes", 3: "yesyesyes"},
|
||||
"list": ["yes", "yesyes"],
|
||||
}
|
||||
assert not script_obj.can_cancel
|
||||
|
||||
@@ -105,21 +102,22 @@ async def test_calling_service(hass):
|
||||
"""Add recorded event to set."""
|
||||
calls.append(service)
|
||||
|
||||
hass.services.async_register('test', 'script', record_call)
|
||||
hass.services.async_register("test", "script", record_call)
|
||||
|
||||
hass.async_add_job(
|
||||
ft.partial(script.call_from_config, hass, {
|
||||
'service': 'test.script',
|
||||
'data': {
|
||||
'hello': 'world'
|
||||
}
|
||||
}, context=context))
|
||||
ft.partial(
|
||||
script.call_from_config,
|
||||
hass,
|
||||
{"service": "test.script", "data": {"hello": "world"}},
|
||||
context=context,
|
||||
)
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
assert calls[0].context is context
|
||||
assert calls[0].data.get('hello') == 'world'
|
||||
assert calls[0].data.get("hello") == "world"
|
||||
|
||||
|
||||
async def test_calling_service_template(hass):
|
||||
@@ -132,40 +130,47 @@ async def test_calling_service_template(hass):
|
||||
"""Add recorded event to set."""
|
||||
calls.append(service)
|
||||
|
||||
hass.services.async_register('test', 'script', record_call)
|
||||
hass.services.async_register("test", "script", record_call)
|
||||
|
||||
hass.async_add_job(
|
||||
ft.partial(script.call_from_config, hass, {
|
||||
'service_template': """
|
||||
ft.partial(
|
||||
script.call_from_config,
|
||||
hass,
|
||||
{
|
||||
"service_template": """
|
||||
{% if True %}
|
||||
test.script
|
||||
{% else %}
|
||||
test.not_script
|
||||
{% endif %}""",
|
||||
'data_template': {
|
||||
'hello': """
|
||||
"data_template": {
|
||||
"hello": """
|
||||
{% if is_world == 'yes' %}
|
||||
world
|
||||
{% else %}
|
||||
not world
|
||||
{% endif %}
|
||||
"""
|
||||
}
|
||||
}, {'is_world': 'yes'}, context=context))
|
||||
},
|
||||
},
|
||||
{"is_world": "yes"},
|
||||
context=context,
|
||||
)
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
assert calls[0].context is context
|
||||
assert calls[0].data.get('hello') == 'world'
|
||||
assert calls[0].data.get("hello") == "world"
|
||||
|
||||
|
||||
async def test_delay(hass):
|
||||
"""Test the delay."""
|
||||
event = 'test_event'
|
||||
event = "test_event"
|
||||
events = []
|
||||
context = Context()
|
||||
delay_alias = 'delay step'
|
||||
delay_alias = "delay step"
|
||||
|
||||
@callback
|
||||
def record_event(event):
|
||||
@@ -174,10 +179,16 @@ async def test_delay(hass):
|
||||
|
||||
hass.bus.async_listen(event, record_event)
|
||||
|
||||
script_obj = script.Script(hass, cv.SCRIPT_SCHEMA([
|
||||
{'event': event},
|
||||
{'delay': {'seconds': 5}, 'alias': delay_alias},
|
||||
{'event': event}]))
|
||||
script_obj = script.Script(
|
||||
hass,
|
||||
cv.SCRIPT_SCHEMA(
|
||||
[
|
||||
{"event": event},
|
||||
{"delay": {"seconds": 5}, "alias": delay_alias},
|
||||
{"event": event},
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
await script_obj.async_run(context=context)
|
||||
await hass.async_block_till_done()
|
||||
@@ -199,9 +210,9 @@ async def test_delay(hass):
|
||||
|
||||
async def test_delay_template(hass):
|
||||
"""Test the delay as a template."""
|
||||
event = 'test_event'
|
||||
event = "test_event"
|
||||
events = []
|
||||
delay_alias = 'delay step'
|
||||
delay_alias = "delay step"
|
||||
|
||||
@callback
|
||||
def record_event(event):
|
||||
@@ -210,10 +221,16 @@ async def test_delay_template(hass):
|
||||
|
||||
hass.bus.async_listen(event, record_event)
|
||||
|
||||
script_obj = script.Script(hass, cv.SCRIPT_SCHEMA([
|
||||
{'event': event},
|
||||
{'delay': '00:00:{{ 5 }}', 'alias': delay_alias},
|
||||
{'event': event}]))
|
||||
script_obj = script.Script(
|
||||
hass,
|
||||
cv.SCRIPT_SCHEMA(
|
||||
[
|
||||
{"event": event},
|
||||
{"delay": "00:00:{{ 5 }}", "alias": delay_alias},
|
||||
{"event": event},
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
await script_obj.async_run()
|
||||
await hass.async_block_till_done()
|
||||
@@ -233,7 +250,7 @@ async def test_delay_template(hass):
|
||||
|
||||
async def test_delay_invalid_template(hass):
|
||||
"""Test the delay as a template that fails."""
|
||||
event = 'test_event'
|
||||
event = "test_event"
|
||||
events = []
|
||||
|
||||
@callback
|
||||
@@ -243,13 +260,19 @@ async def test_delay_invalid_template(hass):
|
||||
|
||||
hass.bus.async_listen(event, record_event)
|
||||
|
||||
script_obj = script.Script(hass, cv.SCRIPT_SCHEMA([
|
||||
{'event': event},
|
||||
{'delay': '{{ invalid_delay }}'},
|
||||
{'delay': {'seconds': 5}},
|
||||
{'event': event}]))
|
||||
script_obj = script.Script(
|
||||
hass,
|
||||
cv.SCRIPT_SCHEMA(
|
||||
[
|
||||
{"event": event},
|
||||
{"delay": "{{ invalid_delay }}"},
|
||||
{"delay": {"seconds": 5}},
|
||||
{"event": event},
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
with mock.patch.object(script, '_LOGGER') as mock_logger:
|
||||
with mock.patch.object(script, "_LOGGER") as mock_logger:
|
||||
await script_obj.async_run()
|
||||
await hass.async_block_till_done()
|
||||
assert mock_logger.error.called
|
||||
@@ -260,9 +283,9 @@ async def test_delay_invalid_template(hass):
|
||||
|
||||
async def test_delay_complex_template(hass):
|
||||
"""Test the delay with a working complex template."""
|
||||
event = 'test_event'
|
||||
event = "test_event"
|
||||
events = []
|
||||
delay_alias = 'delay step'
|
||||
delay_alias = "delay step"
|
||||
|
||||
@callback
|
||||
def record_event(event):
|
||||
@@ -271,12 +294,16 @@ async def test_delay_complex_template(hass):
|
||||
|
||||
hass.bus.async_listen(event, record_event)
|
||||
|
||||
script_obj = script.Script(hass, cv.SCRIPT_SCHEMA([
|
||||
{'event': event},
|
||||
{'delay': {
|
||||
'seconds': '{{ 5 }}'},
|
||||
'alias': delay_alias},
|
||||
{'event': event}]))
|
||||
script_obj = script.Script(
|
||||
hass,
|
||||
cv.SCRIPT_SCHEMA(
|
||||
[
|
||||
{"event": event},
|
||||
{"delay": {"seconds": "{{ 5 }}"}, "alias": delay_alias},
|
||||
{"event": event},
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
await script_obj.async_run()
|
||||
await hass.async_block_till_done()
|
||||
@@ -296,7 +323,7 @@ async def test_delay_complex_template(hass):
|
||||
|
||||
async def test_delay_complex_invalid_template(hass):
|
||||
"""Test the delay with a complex template that fails."""
|
||||
event = 'test_event'
|
||||
event = "test_event"
|
||||
events = []
|
||||
|
||||
@callback
|
||||
@@ -306,17 +333,19 @@ async def test_delay_complex_invalid_template(hass):
|
||||
|
||||
hass.bus.async_listen(event, record_event)
|
||||
|
||||
script_obj = script.Script(hass, cv.SCRIPT_SCHEMA([
|
||||
{'event': event},
|
||||
{'delay': {
|
||||
'seconds': '{{ invalid_delay }}'
|
||||
}},
|
||||
{'delay': {
|
||||
'seconds': '{{ 5 }}'
|
||||
}},
|
||||
{'event': event}]))
|
||||
script_obj = script.Script(
|
||||
hass,
|
||||
cv.SCRIPT_SCHEMA(
|
||||
[
|
||||
{"event": event},
|
||||
{"delay": {"seconds": "{{ invalid_delay }}"}},
|
||||
{"delay": {"seconds": "{{ 5 }}"}},
|
||||
{"event": event},
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
with mock.patch.object(script, '_LOGGER') as mock_logger:
|
||||
with mock.patch.object(script, "_LOGGER") as mock_logger:
|
||||
await script_obj.async_run()
|
||||
await hass.async_block_till_done()
|
||||
assert mock_logger.error.called
|
||||
@@ -327,7 +356,7 @@ async def test_delay_complex_invalid_template(hass):
|
||||
|
||||
async def test_cancel_while_delay(hass):
|
||||
"""Test the cancelling while the delay is present."""
|
||||
event = 'test_event'
|
||||
event = "test_event"
|
||||
events = []
|
||||
|
||||
@callback
|
||||
@@ -337,9 +366,9 @@ async def test_cancel_while_delay(hass):
|
||||
|
||||
hass.bus.async_listen(event, record_event)
|
||||
|
||||
script_obj = script.Script(hass, cv.SCRIPT_SCHEMA([
|
||||
{'delay': {'seconds': 5}},
|
||||
{'event': event}]))
|
||||
script_obj = script.Script(
|
||||
hass, cv.SCRIPT_SCHEMA([{"delay": {"seconds": 5}}, {"event": event}])
|
||||
)
|
||||
|
||||
await script_obj.async_run()
|
||||
await hass.async_block_till_done()
|
||||
@@ -362,10 +391,10 @@ async def test_cancel_while_delay(hass):
|
||||
|
||||
async def test_wait_template(hass):
|
||||
"""Test the wait template."""
|
||||
event = 'test_event'
|
||||
event = "test_event"
|
||||
events = []
|
||||
context = Context()
|
||||
wait_alias = 'wait step'
|
||||
wait_alias = "wait step"
|
||||
|
||||
@callback
|
||||
def record_event(event):
|
||||
@@ -374,13 +403,21 @@ async def test_wait_template(hass):
|
||||
|
||||
hass.bus.async_listen(event, record_event)
|
||||
|
||||
hass.states.async_set('switch.test', 'on')
|
||||
hass.states.async_set("switch.test", "on")
|
||||
|
||||
script_obj = script.Script(hass, cv.SCRIPT_SCHEMA([
|
||||
{'event': event},
|
||||
{'wait_template': "{{states.switch.test.state == 'off'}}",
|
||||
'alias': wait_alias},
|
||||
{'event': event}]))
|
||||
script_obj = script.Script(
|
||||
hass,
|
||||
cv.SCRIPT_SCHEMA(
|
||||
[
|
||||
{"event": event},
|
||||
{
|
||||
"wait_template": "{{states.switch.test.state == 'off'}}",
|
||||
"alias": wait_alias,
|
||||
},
|
||||
{"event": event},
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
await script_obj.async_run(context=context)
|
||||
await hass.async_block_till_done()
|
||||
@@ -390,7 +427,7 @@ async def test_wait_template(hass):
|
||||
assert script_obj.last_action == wait_alias
|
||||
assert len(events) == 1
|
||||
|
||||
hass.states.async_set('switch.test', 'off')
|
||||
hass.states.async_set("switch.test", "off")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert not script_obj.is_running
|
||||
@@ -401,9 +438,9 @@ async def test_wait_template(hass):
|
||||
|
||||
async def test_wait_template_cancel(hass):
|
||||
"""Test the wait template cancel action."""
|
||||
event = 'test_event'
|
||||
event = "test_event"
|
||||
events = []
|
||||
wait_alias = 'wait step'
|
||||
wait_alias = "wait step"
|
||||
|
||||
@callback
|
||||
def record_event(event):
|
||||
@@ -412,13 +449,21 @@ async def test_wait_template_cancel(hass):
|
||||
|
||||
hass.bus.async_listen(event, record_event)
|
||||
|
||||
hass.states.async_set('switch.test', 'on')
|
||||
hass.states.async_set("switch.test", "on")
|
||||
|
||||
script_obj = script.Script(hass, cv.SCRIPT_SCHEMA([
|
||||
{'event': event},
|
||||
{'wait_template': "{{states.switch.test.state == 'off'}}",
|
||||
'alias': wait_alias},
|
||||
{'event': event}]))
|
||||
script_obj = script.Script(
|
||||
hass,
|
||||
cv.SCRIPT_SCHEMA(
|
||||
[
|
||||
{"event": event},
|
||||
{
|
||||
"wait_template": "{{states.switch.test.state == 'off'}}",
|
||||
"alias": wait_alias,
|
||||
},
|
||||
{"event": event},
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
await script_obj.async_run()
|
||||
await hass.async_block_till_done()
|
||||
@@ -433,7 +478,7 @@ async def test_wait_template_cancel(hass):
|
||||
assert not script_obj.is_running
|
||||
assert len(events) == 1
|
||||
|
||||
hass.states.async_set('switch.test', 'off')
|
||||
hass.states.async_set("switch.test", "off")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert not script_obj.is_running
|
||||
@@ -442,7 +487,7 @@ async def test_wait_template_cancel(hass):
|
||||
|
||||
async def test_wait_template_not_schedule(hass):
|
||||
"""Test the wait template with correct condition."""
|
||||
event = 'test_event'
|
||||
event = "test_event"
|
||||
events = []
|
||||
|
||||
@callback
|
||||
@@ -452,12 +497,18 @@ async def test_wait_template_not_schedule(hass):
|
||||
|
||||
hass.bus.async_listen(event, record_event)
|
||||
|
||||
hass.states.async_set('switch.test', 'on')
|
||||
hass.states.async_set("switch.test", "on")
|
||||
|
||||
script_obj = script.Script(hass, cv.SCRIPT_SCHEMA([
|
||||
{'event': event},
|
||||
{'wait_template': "{{states.switch.test.state == 'on'}}"},
|
||||
{'event': event}]))
|
||||
script_obj = script.Script(
|
||||
hass,
|
||||
cv.SCRIPT_SCHEMA(
|
||||
[
|
||||
{"event": event},
|
||||
{"wait_template": "{{states.switch.test.state == 'on'}}"},
|
||||
{"event": event},
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
await script_obj.async_run()
|
||||
await hass.async_block_till_done()
|
||||
@@ -469,9 +520,9 @@ async def test_wait_template_not_schedule(hass):
|
||||
|
||||
async def test_wait_template_timeout_halt(hass):
|
||||
"""Test the wait template, halt on timeout."""
|
||||
event = 'test_event'
|
||||
event = "test_event"
|
||||
events = []
|
||||
wait_alias = 'wait step'
|
||||
wait_alias = "wait step"
|
||||
|
||||
@callback
|
||||
def record_event(event):
|
||||
@@ -480,17 +531,23 @@ async def test_wait_template_timeout_halt(hass):
|
||||
|
||||
hass.bus.async_listen(event, record_event)
|
||||
|
||||
hass.states.async_set('switch.test', 'on')
|
||||
hass.states.async_set("switch.test", "on")
|
||||
|
||||
script_obj = script.Script(hass, cv.SCRIPT_SCHEMA([
|
||||
{'event': event},
|
||||
{
|
||||
'wait_template': "{{states.switch.test.state == 'off'}}",
|
||||
'continue_on_timeout': False,
|
||||
'timeout': 5,
|
||||
'alias': wait_alias
|
||||
},
|
||||
{'event': event}]))
|
||||
script_obj = script.Script(
|
||||
hass,
|
||||
cv.SCRIPT_SCHEMA(
|
||||
[
|
||||
{"event": event},
|
||||
{
|
||||
"wait_template": "{{states.switch.test.state == 'off'}}",
|
||||
"continue_on_timeout": False,
|
||||
"timeout": 5,
|
||||
"alias": wait_alias,
|
||||
},
|
||||
{"event": event},
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
await script_obj.async_run()
|
||||
await hass.async_block_till_done()
|
||||
@@ -510,9 +567,9 @@ async def test_wait_template_timeout_halt(hass):
|
||||
|
||||
async def test_wait_template_timeout_continue(hass):
|
||||
"""Test the wait template with continuing the script."""
|
||||
event = 'test_event'
|
||||
event = "test_event"
|
||||
events = []
|
||||
wait_alias = 'wait step'
|
||||
wait_alias = "wait step"
|
||||
|
||||
@callback
|
||||
def record_event(event):
|
||||
@@ -521,17 +578,23 @@ async def test_wait_template_timeout_continue(hass):
|
||||
|
||||
hass.bus.async_listen(event, record_event)
|
||||
|
||||
hass.states.async_set('switch.test', 'on')
|
||||
hass.states.async_set("switch.test", "on")
|
||||
|
||||
script_obj = script.Script(hass, cv.SCRIPT_SCHEMA([
|
||||
{'event': event},
|
||||
{
|
||||
'wait_template': "{{states.switch.test.state == 'off'}}",
|
||||
'timeout': 5,
|
||||
'continue_on_timeout': True,
|
||||
'alias': wait_alias
|
||||
},
|
||||
{'event': event}]))
|
||||
script_obj = script.Script(
|
||||
hass,
|
||||
cv.SCRIPT_SCHEMA(
|
||||
[
|
||||
{"event": event},
|
||||
{
|
||||
"wait_template": "{{states.switch.test.state == 'off'}}",
|
||||
"timeout": 5,
|
||||
"continue_on_timeout": True,
|
||||
"alias": wait_alias,
|
||||
},
|
||||
{"event": event},
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
await script_obj.async_run()
|
||||
await hass.async_block_till_done()
|
||||
@@ -551,9 +614,9 @@ async def test_wait_template_timeout_continue(hass):
|
||||
|
||||
async def test_wait_template_timeout_default(hass):
|
||||
"""Test the wait template with default contiune."""
|
||||
event = 'test_event'
|
||||
event = "test_event"
|
||||
events = []
|
||||
wait_alias = 'wait step'
|
||||
wait_alias = "wait step"
|
||||
|
||||
@callback
|
||||
def record_event(event):
|
||||
@@ -562,16 +625,22 @@ async def test_wait_template_timeout_default(hass):
|
||||
|
||||
hass.bus.async_listen(event, record_event)
|
||||
|
||||
hass.states.async_set('switch.test', 'on')
|
||||
hass.states.async_set("switch.test", "on")
|
||||
|
||||
script_obj = script.Script(hass, cv.SCRIPT_SCHEMA([
|
||||
{'event': event},
|
||||
{
|
||||
'wait_template': "{{states.switch.test.state == 'off'}}",
|
||||
'timeout': 5,
|
||||
'alias': wait_alias
|
||||
},
|
||||
{'event': event}]))
|
||||
script_obj = script.Script(
|
||||
hass,
|
||||
cv.SCRIPT_SCHEMA(
|
||||
[
|
||||
{"event": event},
|
||||
{
|
||||
"wait_template": "{{states.switch.test.state == 'off'}}",
|
||||
"timeout": 5,
|
||||
"alias": wait_alias,
|
||||
},
|
||||
{"event": event},
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
await script_obj.async_run()
|
||||
await hass.async_block_till_done()
|
||||
@@ -591,9 +660,9 @@ async def test_wait_template_timeout_default(hass):
|
||||
|
||||
async def test_wait_template_variables(hass):
|
||||
"""Test the wait template with variables."""
|
||||
event = 'test_event'
|
||||
event = "test_event"
|
||||
events = []
|
||||
wait_alias = 'wait step'
|
||||
wait_alias = "wait step"
|
||||
|
||||
@callback
|
||||
def record_event(event):
|
||||
@@ -602,17 +671,20 @@ async def test_wait_template_variables(hass):
|
||||
|
||||
hass.bus.async_listen(event, record_event)
|
||||
|
||||
hass.states.async_set('switch.test', 'on')
|
||||
hass.states.async_set("switch.test", "on")
|
||||
|
||||
script_obj = script.Script(hass, cv.SCRIPT_SCHEMA([
|
||||
{'event': event},
|
||||
{'wait_template': "{{is_state(data, 'off')}}",
|
||||
'alias': wait_alias},
|
||||
{'event': event}]))
|
||||
script_obj = script.Script(
|
||||
hass,
|
||||
cv.SCRIPT_SCHEMA(
|
||||
[
|
||||
{"event": event},
|
||||
{"wait_template": "{{is_state(data, 'off')}}", "alias": wait_alias},
|
||||
{"event": event},
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
await script_obj.async_run({
|
||||
'data': 'switch.test'
|
||||
})
|
||||
await script_obj.async_run({"data": "switch.test"})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert script_obj.is_running
|
||||
@@ -620,7 +692,7 @@ async def test_wait_template_variables(hass):
|
||||
assert script_obj.last_action == wait_alias
|
||||
assert len(events) == 1
|
||||
|
||||
hass.states.async_set('switch.test', 'off')
|
||||
hass.states.async_set("switch.test", "off")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert not script_obj.is_running
|
||||
@@ -636,34 +708,34 @@ async def test_passing_variables_to_script(hass):
|
||||
"""Add recorded event to set."""
|
||||
calls.append(service)
|
||||
|
||||
hass.services.async_register('test', 'script', record_call)
|
||||
hass.services.async_register("test", "script", record_call)
|
||||
|
||||
script_obj = script.Script(hass, cv.SCRIPT_SCHEMA([
|
||||
{
|
||||
'service': 'test.script',
|
||||
'data_template': {
|
||||
'hello': '{{ greeting }}',
|
||||
},
|
||||
},
|
||||
{'delay': '{{ delay_period }}'},
|
||||
{
|
||||
'service': 'test.script',
|
||||
'data_template': {
|
||||
'hello': '{{ greeting2 }}',
|
||||
},
|
||||
}]))
|
||||
script_obj = script.Script(
|
||||
hass,
|
||||
cv.SCRIPT_SCHEMA(
|
||||
[
|
||||
{
|
||||
"service": "test.script",
|
||||
"data_template": {"hello": "{{ greeting }}"},
|
||||
},
|
||||
{"delay": "{{ delay_period }}"},
|
||||
{
|
||||
"service": "test.script",
|
||||
"data_template": {"hello": "{{ greeting2 }}"},
|
||||
},
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
await script_obj.async_run({
|
||||
'greeting': 'world',
|
||||
'greeting2': 'universe',
|
||||
'delay_period': '00:00:05'
|
||||
})
|
||||
await script_obj.async_run(
|
||||
{"greeting": "world", "greeting2": "universe", "delay_period": "00:00:05"}
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert script_obj.is_running
|
||||
assert len(calls) == 1
|
||||
assert calls[-1].data['hello'] == 'world'
|
||||
assert calls[-1].data["hello"] == "world"
|
||||
|
||||
future = dt_util.utcnow() + timedelta(seconds=5)
|
||||
async_fire_time_changed(hass, future)
|
||||
@@ -671,12 +743,12 @@ async def test_passing_variables_to_script(hass):
|
||||
|
||||
assert not script_obj.is_running
|
||||
assert len(calls) == 2
|
||||
assert calls[-1].data['hello'] == 'universe'
|
||||
assert calls[-1].data["hello"] == "universe"
|
||||
|
||||
|
||||
async def test_condition(hass):
|
||||
"""Test if we can use conditions in a script."""
|
||||
event = 'test_event'
|
||||
event = "test_event"
|
||||
events = []
|
||||
|
||||
@callback
|
||||
@@ -686,32 +758,37 @@ async def test_condition(hass):
|
||||
|
||||
hass.bus.async_listen(event, record_event)
|
||||
|
||||
hass.states.async_set('test.entity', 'hello')
|
||||
hass.states.async_set("test.entity", "hello")
|
||||
|
||||
script_obj = script.Script(hass, cv.SCRIPT_SCHEMA([
|
||||
{'event': event},
|
||||
{
|
||||
'condition': 'template',
|
||||
'value_template': '{{ states.test.entity.state == "hello" }}',
|
||||
},
|
||||
{'event': event},
|
||||
]))
|
||||
script_obj = script.Script(
|
||||
hass,
|
||||
cv.SCRIPT_SCHEMA(
|
||||
[
|
||||
{"event": event},
|
||||
{
|
||||
"condition": "template",
|
||||
"value_template": '{{ states.test.entity.state == "hello" }}',
|
||||
},
|
||||
{"event": event},
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
await script_obj.async_run()
|
||||
await hass.async_block_till_done()
|
||||
assert len(events) == 2
|
||||
|
||||
hass.states.async_set('test.entity', 'goodbye')
|
||||
hass.states.async_set("test.entity", "goodbye")
|
||||
|
||||
await script_obj.async_run()
|
||||
await hass.async_block_till_done()
|
||||
assert len(events) == 3
|
||||
|
||||
|
||||
@asynctest.patch('homeassistant.helpers.script.condition.async_from_config')
|
||||
@asynctest.patch("homeassistant.helpers.script.condition.async_from_config")
|
||||
async def test_condition_created_once(async_from_config, hass):
|
||||
"""Test that the conditions do not get created multiple times."""
|
||||
event = 'test_event'
|
||||
event = "test_event"
|
||||
events = []
|
||||
|
||||
@callback
|
||||
@@ -721,16 +798,21 @@ async def test_condition_created_once(async_from_config, hass):
|
||||
|
||||
hass.bus.async_listen(event, record_event)
|
||||
|
||||
hass.states.async_set('test.entity', 'hello')
|
||||
hass.states.async_set("test.entity", "hello")
|
||||
|
||||
script_obj = script.Script(hass, cv.SCRIPT_SCHEMA([
|
||||
{'event': event},
|
||||
{
|
||||
'condition': 'template',
|
||||
'value_template': '{{ states.test.entity.state == "hello" }}',
|
||||
},
|
||||
{'event': event},
|
||||
]))
|
||||
script_obj = script.Script(
|
||||
hass,
|
||||
cv.SCRIPT_SCHEMA(
|
||||
[
|
||||
{"event": event},
|
||||
{
|
||||
"condition": "template",
|
||||
"value_template": '{{ states.test.entity.state == "hello" }}',
|
||||
},
|
||||
{"event": event},
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
await script_obj.async_run()
|
||||
await script_obj.async_run()
|
||||
@@ -741,7 +823,7 @@ async def test_condition_created_once(async_from_config, hass):
|
||||
|
||||
async def test_all_conditions_cached(hass):
|
||||
"""Test that multiple conditions get cached."""
|
||||
event = 'test_event'
|
||||
event = "test_event"
|
||||
events = []
|
||||
|
||||
@callback
|
||||
@@ -751,20 +833,25 @@ async def test_all_conditions_cached(hass):
|
||||
|
||||
hass.bus.async_listen(event, record_event)
|
||||
|
||||
hass.states.async_set('test.entity', 'hello')
|
||||
hass.states.async_set("test.entity", "hello")
|
||||
|
||||
script_obj = script.Script(hass, cv.SCRIPT_SCHEMA([
|
||||
{'event': event},
|
||||
{
|
||||
'condition': 'template',
|
||||
'value_template': '{{ states.test.entity.state == "hello" }}',
|
||||
},
|
||||
{
|
||||
'condition': 'template',
|
||||
'value_template': '{{ states.test.entity.state != "hello" }}',
|
||||
},
|
||||
{'event': event},
|
||||
]))
|
||||
script_obj = script.Script(
|
||||
hass,
|
||||
cv.SCRIPT_SCHEMA(
|
||||
[
|
||||
{"event": event},
|
||||
{
|
||||
"condition": "template",
|
||||
"value_template": '{{ states.test.entity.state == "hello" }}',
|
||||
},
|
||||
{
|
||||
"condition": "template",
|
||||
"value_template": '{{ states.test.entity.state != "hello" }}',
|
||||
},
|
||||
{"event": event},
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
await script_obj.async_run()
|
||||
await hass.async_block_till_done()
|
||||
@@ -773,18 +860,19 @@ async def test_all_conditions_cached(hass):
|
||||
|
||||
async def test_last_triggered(hass):
|
||||
"""Test the last_triggered."""
|
||||
event = 'test_event'
|
||||
event = "test_event"
|
||||
|
||||
script_obj = script.Script(hass, cv.SCRIPT_SCHEMA([
|
||||
{'event': event},
|
||||
{'delay': {'seconds': 5}},
|
||||
{'event': event}]))
|
||||
script_obj = script.Script(
|
||||
hass,
|
||||
cv.SCRIPT_SCHEMA(
|
||||
[{"event": event}, {"delay": {"seconds": 5}}, {"event": event}]
|
||||
),
|
||||
)
|
||||
|
||||
assert script_obj.last_triggered is None
|
||||
|
||||
time = dt_util.utcnow()
|
||||
with mock.patch('homeassistant.helpers.script.date_util.utcnow',
|
||||
return_value=time):
|
||||
with mock.patch("homeassistant.helpers.script.date_util.utcnow", return_value=time):
|
||||
await script_obj.async_run()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@@ -799,11 +887,11 @@ async def test_propagate_error_service_not_found(hass):
|
||||
def record_event(event):
|
||||
events.append(event)
|
||||
|
||||
hass.bus.async_listen('test_event', record_event)
|
||||
hass.bus.async_listen("test_event", record_event)
|
||||
|
||||
script_obj = script.Script(hass, cv.SCRIPT_SCHEMA([
|
||||
{'service': 'test.script'},
|
||||
{'event': 'test_event'}]))
|
||||
script_obj = script.Script(
|
||||
hass, cv.SCRIPT_SCHEMA([{"service": "test.script"}, {"event": "test_event"}])
|
||||
)
|
||||
|
||||
with pytest.raises(exceptions.ServiceNotFound):
|
||||
await script_obj.async_run()
|
||||
@@ -820,7 +908,7 @@ async def test_propagate_error_invalid_service_data(hass):
|
||||
def record_event(event):
|
||||
events.append(event)
|
||||
|
||||
hass.bus.async_listen('test_event', record_event)
|
||||
hass.bus.async_listen("test_event", record_event)
|
||||
|
||||
calls = []
|
||||
|
||||
@@ -829,12 +917,16 @@ async def test_propagate_error_invalid_service_data(hass):
|
||||
"""Add recorded event to set."""
|
||||
calls.append(service)
|
||||
|
||||
hass.services.async_register('test', 'script', record_call,
|
||||
schema=vol.Schema({'text': str}))
|
||||
hass.services.async_register(
|
||||
"test", "script", record_call, schema=vol.Schema({"text": str})
|
||||
)
|
||||
|
||||
script_obj = script.Script(hass, cv.SCRIPT_SCHEMA([
|
||||
{'service': 'test.script', 'data': {'text': 1}},
|
||||
{'event': 'test_event'}]))
|
||||
script_obj = script.Script(
|
||||
hass,
|
||||
cv.SCRIPT_SCHEMA(
|
||||
[{"service": "test.script", "data": {"text": 1}}, {"event": "test_event"}]
|
||||
),
|
||||
)
|
||||
|
||||
with pytest.raises(vol.Invalid):
|
||||
await script_obj.async_run()
|
||||
@@ -852,7 +944,7 @@ async def test_propagate_error_service_exception(hass):
|
||||
def record_event(event):
|
||||
events.append(event)
|
||||
|
||||
hass.bus.async_listen('test_event', record_event)
|
||||
hass.bus.async_listen("test_event", record_event)
|
||||
|
||||
calls = []
|
||||
|
||||
@@ -861,11 +953,11 @@ async def test_propagate_error_service_exception(hass):
|
||||
"""Add recorded event to set."""
|
||||
raise ValueError("BROKEN")
|
||||
|
||||
hass.services.async_register('test', 'script', record_call)
|
||||
hass.services.async_register("test", "script", record_call)
|
||||
|
||||
script_obj = script.Script(hass, cv.SCRIPT_SCHEMA([
|
||||
{'service': 'test.script'},
|
||||
{'event': 'test_event'}]))
|
||||
script_obj = script.Script(
|
||||
hass, cv.SCRIPT_SCHEMA([{"service": "test.script"}, {"event": "test_event"}])
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
await script_obj.async_run()
|
||||
@@ -877,27 +969,26 @@ async def test_propagate_error_service_exception(hass):
|
||||
|
||||
def test_log_exception():
|
||||
"""Test logged output."""
|
||||
script_obj = script.Script(None, cv.SCRIPT_SCHEMA([
|
||||
{'service': 'test.script'},
|
||||
{'event': 'test_event'}]))
|
||||
script_obj = script.Script(
|
||||
None, cv.SCRIPT_SCHEMA([{"service": "test.script"}, {"event": "test_event"}])
|
||||
)
|
||||
script_obj._exception_step = 1
|
||||
|
||||
for exc, msg in (
|
||||
(vol.Invalid("Invalid number"), 'Invalid data'),
|
||||
(exceptions.TemplateError(
|
||||
jinja2.TemplateError('Unclosed bracket')),
|
||||
'Error rendering template'),
|
||||
(exceptions.Unauthorized(), 'Unauthorized'),
|
||||
(exceptions.ServiceNotFound('light', 'turn_on'),
|
||||
'Service not found'),
|
||||
(ValueError("Cannot parse JSON"), 'Unknown error'),
|
||||
(vol.Invalid("Invalid number"), "Invalid data"),
|
||||
(
|
||||
exceptions.TemplateError(jinja2.TemplateError("Unclosed bracket")),
|
||||
"Error rendering template",
|
||||
),
|
||||
(exceptions.Unauthorized(), "Unauthorized"),
|
||||
(exceptions.ServiceNotFound("light", "turn_on"), "Service not found"),
|
||||
(ValueError("Cannot parse JSON"), "Unknown error"),
|
||||
):
|
||||
logger = mock.Mock()
|
||||
script_obj.async_log_exception(logger, 'Test error', exc)
|
||||
script_obj.async_log_exception(logger, "Test error", exc)
|
||||
|
||||
assert len(logger.mock_calls) == 1
|
||||
_, _, p_error_desc, p_action_type, p_step, p_error = \
|
||||
logger.mock_calls[0][1]
|
||||
_, _, p_error_desc, p_action_type, p_step, p_error = logger.mock_calls[0][1]
|
||||
|
||||
assert p_error_desc == msg
|
||||
assert p_action_type == script.ACTION_FIRE_EVENT
|
||||
|
||||
Reference in New Issue
Block a user