2016-04-22 01:29:20 +00:00
|
|
|
"""The tests for the Script component."""
|
2016-10-30 21:18:53 +00:00
|
|
|
# pylint: disable=protected-access
|
2016-04-22 01:29:20 +00:00
|
|
|
from datetime import timedelta
|
2019-04-30 16:20:38 +00:00
|
|
|
import functools as ft
|
2016-10-11 06:36:38 +00:00
|
|
|
from unittest import mock
|
2016-04-22 01:29:20 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
import asynctest
|
2018-12-13 11:21:16 +00:00
|
|
|
import jinja2
|
2018-12-03 14:46:25 +00:00
|
|
|
import pytest
|
2019-12-09 15:52:24 +00:00
|
|
|
import voluptuous as vol
|
2018-12-03 14:46:25 +00:00
|
|
|
|
2019-12-09 15:52:24 +00:00
|
|
|
# Otherwise can't test just this file (import order issue)
|
2018-12-03 14:46:25 +00:00
|
|
|
from homeassistant import exceptions
|
2019-12-09 15:52:24 +00:00
|
|
|
import homeassistant.components.scene as scene
|
2019-10-05 20:30:43 +00:00
|
|
|
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_ON
|
2018-09-04 19:16:24 +00:00
|
|
|
from homeassistant.core import Context, callback
|
2019-12-09 15:52:24 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv, script
|
2016-04-22 01:29:20 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
from tests.common import async_fire_time_changed
|
2016-04-22 01:29:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ENTITY_ID = "script.test"
|
2016-04-22 01:29:20 +00:00
|
|
|
|
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
async def test_firing_event(hass):
|
|
|
|
"""Test the firing of events."""
|
2019-07-31 19:25:30 +00:00
|
|
|
event = "test_event"
|
2019-04-30 16:20:38 +00:00
|
|
|
context = Context()
|
|
|
|
calls = []
|
2016-04-22 01:29:20 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
@callback
|
|
|
|
def record_event(event):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
calls.append(event)
|
2016-04-22 01:29:20 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
hass.bus.async_listen(event, record_event)
|
2016-04-22 01:29:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
script_obj = script.Script(
|
|
|
|
hass, cv.SCRIPT_SCHEMA({"event": event, "event_data": {"hello": "world"}})
|
|
|
|
)
|
2016-04-22 01:29:20 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
await script_obj.async_run(context=context)
|
2016-04-22 01:29:20 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
await hass.async_block_till_done()
|
2016-04-22 01:29:20 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
assert len(calls) == 1
|
|
|
|
assert calls[0].context is context
|
2019-07-31 19:25:30 +00:00
|
|
|
assert calls[0].data.get("hello") == "world"
|
2019-04-30 16:20:38 +00:00
|
|
|
assert not script_obj.can_cancel
|
|
|
|
|
|
|
|
|
|
|
|
async def test_firing_event_template(hass):
|
|
|
|
"""Test the firing of events."""
|
2019-07-31 19:25:30 +00:00
|
|
|
event = "test_event"
|
2019-04-30 16:20:38 +00:00
|
|
|
context = Context()
|
|
|
|
calls = []
|
2018-01-19 06:13:14 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
@callback
|
|
|
|
def record_event(event):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
calls.append(event)
|
2018-01-19 06:13:14 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
hass.bus.async_listen(event, record_event)
|
2018-01-19 06:13:14 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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 }}"],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await script_obj.async_run({"is_world": "yes"}, context=context)
|
2018-01-19 06:13:14 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
await hass.async_block_till_done()
|
2016-04-22 01:29:20 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
assert len(calls) == 1
|
|
|
|
assert calls[0].context is context
|
|
|
|
assert calls[0].data == {
|
2019-07-31 19:25:30 +00:00
|
|
|
"dict": {1: "yes", 2: "yesyes", 3: "yesyesyes"},
|
|
|
|
"list": ["yes", "yesyes"],
|
2019-04-30 16:20:38 +00:00
|
|
|
}
|
|
|
|
assert not script_obj.can_cancel
|
2016-04-22 01:29:20 +00:00
|
|
|
|
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
async def test_calling_service(hass):
|
|
|
|
"""Test the calling of a service."""
|
|
|
|
calls = []
|
|
|
|
context = Context()
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def record_call(service):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
calls.append(service)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.services.async_register("test", "script", record_call)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
hass.async_add_job(
|
2019-07-31 19:25:30 +00:00
|
|
|
ft.partial(
|
|
|
|
script.call_from_config,
|
|
|
|
hass,
|
|
|
|
{"service": "test.script", "data": {"hello": "world"}},
|
|
|
|
context=context,
|
|
|
|
)
|
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
2016-04-22 01:29:20 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
assert len(calls) == 1
|
|
|
|
assert calls[0].context is context
|
2019-07-31 19:25:30 +00:00
|
|
|
assert calls[0].data.get("hello") == "world"
|
2016-04-22 01:29:20 +00:00
|
|
|
|
|
|
|
|
2019-10-05 20:30:43 +00:00
|
|
|
async def test_activating_scene(hass):
|
|
|
|
"""Test the activation of a scene."""
|
|
|
|
calls = []
|
|
|
|
context = Context()
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def record_call(service):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
calls.append(service)
|
|
|
|
|
|
|
|
hass.services.async_register(scene.DOMAIN, SERVICE_TURN_ON, record_call)
|
|
|
|
|
|
|
|
hass.async_add_job(
|
|
|
|
ft.partial(
|
|
|
|
script.call_from_config, hass, {"scene": "scene.hello"}, context=context
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(calls) == 1
|
|
|
|
assert calls[0].context is context
|
|
|
|
assert calls[0].data.get(ATTR_ENTITY_ID) == "scene.hello"
|
|
|
|
|
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
async def test_calling_service_template(hass):
|
|
|
|
"""Test the calling of a service."""
|
|
|
|
calls = []
|
|
|
|
context = Context()
|
2016-04-22 01:29:20 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
@callback
|
|
|
|
def record_call(service):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
calls.append(service)
|
2016-04-22 01:29:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.services.async_register("test", "script", record_call)
|
2016-04-22 01:29:20 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
hass.async_add_job(
|
2019-07-31 19:25:30 +00:00
|
|
|
ft.partial(
|
|
|
|
script.call_from_config,
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"service_template": """
|
2016-04-22 01:29:20 +00:00
|
|
|
{% if True %}
|
|
|
|
test.script
|
|
|
|
{% else %}
|
|
|
|
test.not_script
|
|
|
|
{% endif %}""",
|
2019-07-31 19:25:30 +00:00
|
|
|
"data_template": {
|
|
|
|
"hello": """
|
2018-01-19 06:13:14 +00:00
|
|
|
{% if is_world == 'yes' %}
|
2016-04-22 01:29:20 +00:00
|
|
|
world
|
|
|
|
{% else %}
|
2018-01-19 06:13:14 +00:00
|
|
|
not world
|
2016-04-22 01:29:20 +00:00
|
|
|
{% endif %}
|
|
|
|
"""
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{"is_world": "yes"},
|
|
|
|
context=context,
|
|
|
|
)
|
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(calls) == 1
|
|
|
|
assert calls[0].context is context
|
2019-07-31 19:25:30 +00:00
|
|
|
assert calls[0].data.get("hello") == "world"
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_delay(hass):
|
|
|
|
"""Test the delay."""
|
2019-07-31 19:25:30 +00:00
|
|
|
event = "test_event"
|
2019-04-30 16:20:38 +00:00
|
|
|
events = []
|
|
|
|
context = Context()
|
2019-07-31 19:25:30 +00:00
|
|
|
delay_alias = "delay step"
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def record_event(event):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
events.append(event)
|
2016-04-22 01:29:20 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
hass.bus.async_listen(event, record_event)
|
2016-04-22 01:29:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
script_obj = script.Script(
|
|
|
|
hass,
|
|
|
|
cv.SCRIPT_SCHEMA(
|
|
|
|
[
|
|
|
|
{"event": event},
|
|
|
|
{"delay": {"seconds": 5}, "alias": delay_alias},
|
|
|
|
{"event": event},
|
|
|
|
]
|
|
|
|
),
|
|
|
|
)
|
2016-04-22 01:29:20 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
await script_obj.async_run(context=context)
|
|
|
|
await hass.async_block_till_done()
|
2016-04-22 01:29:20 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
assert script_obj.is_running
|
|
|
|
assert script_obj.can_cancel
|
|
|
|
assert script_obj.last_action == delay_alias
|
|
|
|
assert len(events) == 1
|
2016-04-22 01:29:20 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
future = dt_util.utcnow() + timedelta(seconds=5)
|
|
|
|
async_fire_time_changed(hass, future)
|
|
|
|
await hass.async_block_till_done()
|
2016-04-22 01:42:20 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
assert not script_obj.is_running
|
|
|
|
assert len(events) == 2
|
|
|
|
assert events[0].context is context
|
|
|
|
assert events[1].context is context
|
2017-02-12 21:27:53 +00:00
|
|
|
|
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
async def test_delay_template(hass):
|
|
|
|
"""Test the delay as a template."""
|
2019-07-31 19:25:30 +00:00
|
|
|
event = "test_event"
|
2019-04-30 16:20:38 +00:00
|
|
|
events = []
|
2019-07-31 19:25:30 +00:00
|
|
|
delay_alias = "delay step"
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
@callback
|
|
|
|
def record_event(event):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
events.append(event)
|
|
|
|
|
|
|
|
hass.bus.async_listen(event, record_event)
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
script_obj = script.Script(
|
|
|
|
hass,
|
|
|
|
cv.SCRIPT_SCHEMA(
|
|
|
|
[
|
|
|
|
{"event": event},
|
|
|
|
{"delay": "00:00:{{ 5 }}", "alias": delay_alias},
|
|
|
|
{"event": event},
|
|
|
|
]
|
|
|
|
),
|
|
|
|
)
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
await script_obj.async_run()
|
|
|
|
await hass.async_block_till_done()
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
assert script_obj.is_running
|
|
|
|
assert script_obj.can_cancel
|
|
|
|
assert script_obj.last_action == delay_alias
|
|
|
|
assert len(events) == 1
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
future = dt_util.utcnow() + timedelta(seconds=5)
|
|
|
|
async_fire_time_changed(hass, future)
|
|
|
|
await hass.async_block_till_done()
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
assert not script_obj.is_running
|
|
|
|
assert len(events) == 2
|
2017-02-12 21:27:53 +00:00
|
|
|
|
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
async def test_delay_invalid_template(hass):
|
|
|
|
"""Test the delay as a template that fails."""
|
2019-07-31 19:25:30 +00:00
|
|
|
event = "test_event"
|
2019-04-30 16:20:38 +00:00
|
|
|
events = []
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
@callback
|
|
|
|
def record_event(event):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
events.append(event)
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
hass.bus.async_listen(event, record_event)
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
script_obj = script.Script(
|
|
|
|
hass,
|
|
|
|
cv.SCRIPT_SCHEMA(
|
|
|
|
[
|
|
|
|
{"event": event},
|
|
|
|
{"delay": "{{ invalid_delay }}"},
|
|
|
|
{"delay": {"seconds": 5}},
|
|
|
|
{"event": event},
|
|
|
|
]
|
|
|
|
),
|
|
|
|
)
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with mock.patch.object(script, "_LOGGER") as mock_logger:
|
2019-04-30 16:20:38 +00:00
|
|
|
await script_obj.async_run()
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert mock_logger.error.called
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
assert not script_obj.is_running
|
|
|
|
assert len(events) == 1
|
2017-02-12 21:27:53 +00:00
|
|
|
|
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
async def test_delay_complex_template(hass):
|
|
|
|
"""Test the delay with a working complex template."""
|
2019-07-31 19:25:30 +00:00
|
|
|
event = "test_event"
|
2019-04-30 16:20:38 +00:00
|
|
|
events = []
|
2019-07-31 19:25:30 +00:00
|
|
|
delay_alias = "delay step"
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
@callback
|
|
|
|
def record_event(event):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
events.append(event)
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
hass.bus.async_listen(event, record_event)
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
script_obj = script.Script(
|
|
|
|
hass,
|
|
|
|
cv.SCRIPT_SCHEMA(
|
|
|
|
[
|
|
|
|
{"event": event},
|
|
|
|
{"delay": {"seconds": "{{ 5 }}"}, "alias": delay_alias},
|
|
|
|
{"event": event},
|
|
|
|
]
|
|
|
|
),
|
|
|
|
)
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
await script_obj.async_run()
|
|
|
|
await hass.async_block_till_done()
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
assert script_obj.is_running
|
|
|
|
assert script_obj.can_cancel
|
|
|
|
assert script_obj.last_action == delay_alias
|
|
|
|
assert len(events) == 1
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
future = dt_util.utcnow() + timedelta(seconds=5)
|
|
|
|
async_fire_time_changed(hass, future)
|
|
|
|
await hass.async_block_till_done()
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
assert not script_obj.is_running
|
|
|
|
assert len(events) == 2
|
2017-02-12 21:27:53 +00:00
|
|
|
|
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
async def test_delay_complex_invalid_template(hass):
|
|
|
|
"""Test the delay with a complex template that fails."""
|
2019-07-31 19:25:30 +00:00
|
|
|
event = "test_event"
|
2019-04-30 16:20:38 +00:00
|
|
|
events = []
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
@callback
|
|
|
|
def record_event(event):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
events.append(event)
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
hass.bus.async_listen(event, record_event)
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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:
|
2019-04-30 16:20:38 +00:00
|
|
|
await script_obj.async_run()
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert mock_logger.error.called
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
assert not script_obj.is_running
|
|
|
|
assert len(events) == 1
|
2017-02-12 21:27:53 +00:00
|
|
|
|
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
async def test_cancel_while_delay(hass):
|
|
|
|
"""Test the cancelling while the delay is present."""
|
2019-07-31 19:25:30 +00:00
|
|
|
event = "test_event"
|
2019-04-30 16:20:38 +00:00
|
|
|
events = []
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
@callback
|
|
|
|
def record_event(event):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
events.append(event)
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
hass.bus.async_listen(event, record_event)
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
script_obj = script.Script(
|
|
|
|
hass, cv.SCRIPT_SCHEMA([{"delay": {"seconds": 5}}, {"event": event}])
|
|
|
|
)
|
2017-02-12 21:27:53 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
await script_obj.async_run()
|
|
|
|
await hass.async_block_till_done()
|
2018-08-13 09:23:27 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
assert script_obj.is_running
|
|
|
|
assert len(events) == 0
|
2018-08-13 09:23:27 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
script_obj.async_stop()
|
2018-08-13 09:23:27 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
assert not script_obj.is_running
|
2018-08-13 09:23:27 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
# Make sure the script is really stopped.
|
|
|
|
future = dt_util.utcnow() + timedelta(seconds=5)
|
|
|
|
async_fire_time_changed(hass, future)
|
|
|
|
await hass.async_block_till_done()
|
2018-08-13 09:23:27 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
assert not script_obj.is_running
|
|
|
|
assert len(events) == 0
|
2018-08-13 09:23:27 +00:00
|
|
|
|
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
async def test_wait_template(hass):
|
|
|
|
"""Test the wait template."""
|
2019-07-31 19:25:30 +00:00
|
|
|
event = "test_event"
|
2019-04-30 16:20:38 +00:00
|
|
|
events = []
|
|
|
|
context = Context()
|
2019-07-31 19:25:30 +00:00
|
|
|
wait_alias = "wait step"
|
2018-08-13 09:23:27 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
@callback
|
|
|
|
def record_event(event):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
events.append(event)
|
2018-08-13 09:23:27 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
hass.bus.async_listen(event, record_event)
|
2018-08-13 09:23:27 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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},
|
|
|
|
]
|
|
|
|
),
|
|
|
|
)
|
2018-08-13 09:23:27 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
await script_obj.async_run(context=context)
|
|
|
|
await hass.async_block_till_done()
|
2018-08-13 09:23:27 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
assert script_obj.is_running
|
|
|
|
assert script_obj.can_cancel
|
|
|
|
assert script_obj.last_action == wait_alias
|
|
|
|
assert len(events) == 1
|
2018-08-13 09:23:27 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("switch.test", "off")
|
2019-04-30 16:20:38 +00:00
|
|
|
await hass.async_block_till_done()
|
2018-08-13 09:23:27 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
assert not script_obj.is_running
|
|
|
|
assert len(events) == 2
|
|
|
|
assert events[0].context is context
|
|
|
|
assert events[1].context is context
|
2018-08-13 09:23:27 +00:00
|
|
|
|
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
async def test_wait_template_cancel(hass):
|
|
|
|
"""Test the wait template cancel action."""
|
2019-07-31 19:25:30 +00:00
|
|
|
event = "test_event"
|
2019-04-30 16:20:38 +00:00
|
|
|
events = []
|
2019-07-31 19:25:30 +00:00
|
|
|
wait_alias = "wait step"
|
2018-08-13 09:23:27 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
@callback
|
|
|
|
def record_event(event):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
events.append(event)
|
2017-10-12 14:57:18 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
hass.bus.async_listen(event, record_event)
|
2017-10-12 14:57:18 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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},
|
|
|
|
]
|
|
|
|
),
|
|
|
|
)
|
2017-10-12 14:57:18 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
await script_obj.async_run()
|
|
|
|
await hass.async_block_till_done()
|
2017-10-12 14:57:18 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
assert script_obj.is_running
|
|
|
|
assert script_obj.can_cancel
|
|
|
|
assert script_obj.last_action == wait_alias
|
|
|
|
assert len(events) == 1
|
2017-10-12 14:57:18 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
script_obj.async_stop()
|
2017-10-12 14:57:18 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
assert not script_obj.is_running
|
|
|
|
assert len(events) == 1
|
2017-10-12 14:57:18 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("switch.test", "off")
|
2019-04-30 16:20:38 +00:00
|
|
|
await hass.async_block_till_done()
|
2017-10-12 14:57:18 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
assert not script_obj.is_running
|
|
|
|
assert len(events) == 1
|
2016-04-22 01:42:20 +00:00
|
|
|
|
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
async def test_wait_template_not_schedule(hass):
|
|
|
|
"""Test the wait template with correct condition."""
|
2019-07-31 19:25:30 +00:00
|
|
|
event = "test_event"
|
2019-04-30 16:20:38 +00:00
|
|
|
events = []
|
2016-04-22 01:42:20 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
@callback
|
|
|
|
def record_event(event):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
events.append(event)
|
2016-04-28 10:03:57 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
hass.bus.async_listen(event, record_event)
|
2016-04-28 10:03:57 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("switch.test", "on")
|
2016-04-28 10:03:57 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
script_obj = script.Script(
|
|
|
|
hass,
|
|
|
|
cv.SCRIPT_SCHEMA(
|
|
|
|
[
|
|
|
|
{"event": event},
|
|
|
|
{"wait_template": "{{states.switch.test.state == 'on'}}"},
|
|
|
|
{"event": event},
|
|
|
|
]
|
|
|
|
),
|
|
|
|
)
|
2016-10-11 06:36:38 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
await script_obj.async_run()
|
|
|
|
await hass.async_block_till_done()
|
2016-10-11 06:36:38 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
assert not script_obj.is_running
|
|
|
|
assert script_obj.can_cancel
|
|
|
|
assert len(events) == 2
|
2016-10-11 06:36:38 +00:00
|
|
|
|
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
async def test_wait_template_timeout_halt(hass):
|
|
|
|
"""Test the wait template, halt on timeout."""
|
2019-07-31 19:25:30 +00:00
|
|
|
event = "test_event"
|
2019-04-30 16:20:38 +00:00
|
|
|
events = []
|
2019-07-31 19:25:30 +00:00
|
|
|
wait_alias = "wait step"
|
2016-10-11 06:36:38 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
@callback
|
|
|
|
def record_event(event):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
events.append(event)
|
|
|
|
|
|
|
|
hass.bus.async_listen(event, record_event)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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},
|
|
|
|
]
|
|
|
|
),
|
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
await script_obj.async_run()
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert script_obj.is_running
|
|
|
|
assert script_obj.can_cancel
|
|
|
|
assert script_obj.last_action == wait_alias
|
|
|
|
assert len(events) == 1
|
|
|
|
|
|
|
|
future = dt_util.utcnow() + timedelta(seconds=5)
|
|
|
|
async_fire_time_changed(hass, future)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert not script_obj.is_running
|
|
|
|
assert len(events) == 1
|
|
|
|
|
|
|
|
|
|
|
|
async def test_wait_template_timeout_continue(hass):
|
|
|
|
"""Test the wait template with continuing the script."""
|
2019-07-31 19:25:30 +00:00
|
|
|
event = "test_event"
|
2019-04-30 16:20:38 +00:00
|
|
|
events = []
|
2019-07-31 19:25:30 +00:00
|
|
|
wait_alias = "wait step"
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def record_event(event):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
events.append(event)
|
|
|
|
|
|
|
|
hass.bus.async_listen(event, record_event)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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},
|
|
|
|
]
|
|
|
|
),
|
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
await script_obj.async_run()
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert script_obj.is_running
|
|
|
|
assert script_obj.can_cancel
|
|
|
|
assert script_obj.last_action == wait_alias
|
|
|
|
assert len(events) == 1
|
|
|
|
|
|
|
|
future = dt_util.utcnow() + timedelta(seconds=5)
|
|
|
|
async_fire_time_changed(hass, future)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert not script_obj.is_running
|
|
|
|
assert len(events) == 2
|
|
|
|
|
|
|
|
|
|
|
|
async def test_wait_template_timeout_default(hass):
|
|
|
|
"""Test the wait template with default contiune."""
|
2019-07-31 19:25:30 +00:00
|
|
|
event = "test_event"
|
2019-04-30 16:20:38 +00:00
|
|
|
events = []
|
2019-07-31 19:25:30 +00:00
|
|
|
wait_alias = "wait step"
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def record_event(event):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
events.append(event)
|
|
|
|
|
|
|
|
hass.bus.async_listen(event, record_event)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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},
|
|
|
|
]
|
|
|
|
),
|
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
await script_obj.async_run()
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert script_obj.is_running
|
|
|
|
assert script_obj.can_cancel
|
|
|
|
assert script_obj.last_action == wait_alias
|
|
|
|
assert len(events) == 1
|
|
|
|
|
|
|
|
future = dt_util.utcnow() + timedelta(seconds=5)
|
|
|
|
async_fire_time_changed(hass, future)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert not script_obj.is_running
|
|
|
|
assert len(events) == 2
|
|
|
|
|
|
|
|
|
|
|
|
async def test_wait_template_variables(hass):
|
|
|
|
"""Test the wait template with variables."""
|
2019-07-31 19:25:30 +00:00
|
|
|
event = "test_event"
|
2019-04-30 16:20:38 +00:00
|
|
|
events = []
|
2019-07-31 19:25:30 +00:00
|
|
|
wait_alias = "wait step"
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def record_event(event):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
events.append(event)
|
|
|
|
|
|
|
|
hass.bus.async_listen(event, record_event)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("switch.test", "on")
|
2019-04-30 16:20:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
script_obj = script.Script(
|
|
|
|
hass,
|
|
|
|
cv.SCRIPT_SCHEMA(
|
|
|
|
[
|
|
|
|
{"event": event},
|
|
|
|
{"wait_template": "{{is_state(data, 'off')}}", "alias": wait_alias},
|
|
|
|
{"event": event},
|
|
|
|
]
|
|
|
|
),
|
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await script_obj.async_run({"data": "switch.test"})
|
2019-04-30 16:20:38 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert script_obj.is_running
|
|
|
|
assert script_obj.can_cancel
|
|
|
|
assert script_obj.last_action == wait_alias
|
|
|
|
assert len(events) == 1
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("switch.test", "off")
|
2019-04-30 16:20:38 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert not script_obj.is_running
|
|
|
|
assert len(events) == 2
|
|
|
|
|
|
|
|
|
|
|
|
async def test_passing_variables_to_script(hass):
|
|
|
|
"""Test if we can pass variables to script."""
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def record_call(service):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
calls.append(service)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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 }}"},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
),
|
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await script_obj.async_run(
|
|
|
|
{"greeting": "world", "greeting2": "universe", "delay_period": "00:00:05"}
|
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert script_obj.is_running
|
|
|
|
assert len(calls) == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert calls[-1].data["hello"] == "world"
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
future = dt_util.utcnow() + timedelta(seconds=5)
|
|
|
|
async_fire_time_changed(hass, future)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert not script_obj.is_running
|
|
|
|
assert len(calls) == 2
|
2019-07-31 19:25:30 +00:00
|
|
|
assert calls[-1].data["hello"] == "universe"
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_condition(hass):
|
|
|
|
"""Test if we can use conditions in a script."""
|
2019-07-31 19:25:30 +00:00
|
|
|
event = "test_event"
|
2019-04-30 16:20:38 +00:00
|
|
|
events = []
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def record_event(event):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
events.append(event)
|
|
|
|
|
|
|
|
hass.bus.async_listen(event, record_event)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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},
|
|
|
|
]
|
|
|
|
),
|
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
await script_obj.async_run()
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(events) == 2
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("test.entity", "goodbye")
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
await script_obj.async_run()
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(events) == 3
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@asynctest.patch("homeassistant.helpers.script.condition.async_from_config")
|
2019-04-30 16:20:38 +00:00
|
|
|
async def test_condition_created_once(async_from_config, hass):
|
|
|
|
"""Test that the conditions do not get created multiple times."""
|
2019-07-31 19:25:30 +00:00
|
|
|
event = "test_event"
|
2019-04-30 16:20:38 +00:00
|
|
|
events = []
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def record_event(event):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
events.append(event)
|
|
|
|
|
|
|
|
hass.bus.async_listen(event, record_event)
|
2016-10-11 06:36:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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},
|
|
|
|
]
|
|
|
|
),
|
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
await script_obj.async_run()
|
|
|
|
await script_obj.async_run()
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert async_from_config.call_count == 1
|
|
|
|
assert len(script_obj._config_cache) == 1
|
|
|
|
|
|
|
|
|
|
|
|
async def test_all_conditions_cached(hass):
|
|
|
|
"""Test that multiple conditions get cached."""
|
2019-07-31 19:25:30 +00:00
|
|
|
event = "test_event"
|
2019-04-30 16:20:38 +00:00
|
|
|
events = []
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def record_event(event):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
events.append(event)
|
2017-01-11 15:23:05 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
hass.bus.async_listen(event, record_event)
|
2017-01-11 15:23:05 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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},
|
|
|
|
]
|
|
|
|
),
|
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
await script_obj.async_run()
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(script_obj._config_cache) == 2
|
|
|
|
|
|
|
|
|
|
|
|
async def test_last_triggered(hass):
|
|
|
|
"""Test the last_triggered."""
|
2019-07-31 19:25:30 +00:00
|
|
|
event = "test_event"
|
2019-04-30 16:20:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
script_obj = script.Script(
|
|
|
|
hass,
|
|
|
|
cv.SCRIPT_SCHEMA(
|
|
|
|
[{"event": event}, {"delay": {"seconds": 5}}, {"event": event}]
|
|
|
|
),
|
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
assert script_obj.last_triggered is None
|
|
|
|
|
|
|
|
time = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
with mock.patch("homeassistant.helpers.script.date_util.utcnow", return_value=time):
|
2019-04-30 16:20:38 +00:00
|
|
|
await script_obj.async_run()
|
|
|
|
await hass.async_block_till_done()
|
2017-01-11 15:23:05 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
assert script_obj.last_triggered == time
|
2018-12-03 14:46:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_propagate_error_service_not_found(hass):
|
|
|
|
"""Test that a script aborts when a service is not found."""
|
|
|
|
events = []
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def record_event(event):
|
|
|
|
events.append(event)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.bus.async_listen("test_event", record_event)
|
2018-12-03 14:46:25 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
script_obj = script.Script(
|
|
|
|
hass, cv.SCRIPT_SCHEMA([{"service": "test.script"}, {"event": "test_event"}])
|
|
|
|
)
|
2018-12-03 14:46:25 +00:00
|
|
|
|
|
|
|
with pytest.raises(exceptions.ServiceNotFound):
|
|
|
|
await script_obj.async_run()
|
|
|
|
|
|
|
|
assert len(events) == 0
|
2018-12-13 11:21:16 +00:00
|
|
|
assert script_obj._cur == -1
|
2018-12-03 14:46:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_propagate_error_invalid_service_data(hass):
|
|
|
|
"""Test that a script aborts when we send invalid service data."""
|
|
|
|
events = []
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def record_event(event):
|
|
|
|
events.append(event)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.bus.async_listen("test_event", record_event)
|
2018-12-03 14:46:25 +00:00
|
|
|
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def record_call(service):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
calls.append(service)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.services.async_register(
|
|
|
|
"test", "script", record_call, schema=vol.Schema({"text": str})
|
|
|
|
)
|
2018-12-03 14:46:25 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
script_obj = script.Script(
|
|
|
|
hass,
|
|
|
|
cv.SCRIPT_SCHEMA(
|
|
|
|
[{"service": "test.script", "data": {"text": 1}}, {"event": "test_event"}]
|
|
|
|
),
|
|
|
|
)
|
2018-12-03 14:46:25 +00:00
|
|
|
|
|
|
|
with pytest.raises(vol.Invalid):
|
|
|
|
await script_obj.async_run()
|
|
|
|
|
|
|
|
assert len(events) == 0
|
|
|
|
assert len(calls) == 0
|
2018-12-13 11:21:16 +00:00
|
|
|
assert script_obj._cur == -1
|
2018-12-03 14:46:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_propagate_error_service_exception(hass):
|
|
|
|
"""Test that a script aborts when a service throws an exception."""
|
|
|
|
events = []
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def record_event(event):
|
|
|
|
events.append(event)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.bus.async_listen("test_event", record_event)
|
2018-12-03 14:46:25 +00:00
|
|
|
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def record_call(service):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
raise ValueError("BROKEN")
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.services.async_register("test", "script", record_call)
|
2018-12-03 14:46:25 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
script_obj = script.Script(
|
|
|
|
hass, cv.SCRIPT_SCHEMA([{"service": "test.script"}, {"event": "test_event"}])
|
|
|
|
)
|
2018-12-03 14:46:25 +00:00
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
await script_obj.async_run()
|
|
|
|
|
|
|
|
assert len(events) == 0
|
|
|
|
assert len(calls) == 0
|
2018-12-13 11:21:16 +00:00
|
|
|
assert script_obj._cur == -1
|
|
|
|
|
|
|
|
|
|
|
|
def test_log_exception():
|
|
|
|
"""Test logged output."""
|
2019-07-31 19:25:30 +00:00
|
|
|
script_obj = script.Script(
|
|
|
|
None, cv.SCRIPT_SCHEMA([{"service": "test.script"}, {"event": "test_event"}])
|
|
|
|
)
|
2018-12-13 11:21:16 +00:00
|
|
|
script_obj._exception_step = 1
|
|
|
|
|
|
|
|
for exc, msg in (
|
2019-07-31 19:25:30 +00:00
|
|
|
(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"),
|
2018-12-13 11:21:16 +00:00
|
|
|
):
|
|
|
|
logger = mock.Mock()
|
2019-07-31 19:25:30 +00:00
|
|
|
script_obj.async_log_exception(logger, "Test error", exc)
|
2018-12-13 11:21:16 +00:00
|
|
|
|
|
|
|
assert len(logger.mock_calls) == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
_, _, p_error_desc, p_action_type, p_step, p_error = logger.mock_calls[0][1]
|
2018-12-13 11:21:16 +00:00
|
|
|
|
|
|
|
assert p_error_desc == msg
|
|
|
|
assert p_action_type == script.ACTION_FIRE_EVENT
|
|
|
|
assert p_step == 2
|
|
|
|
if isinstance(exc, ValueError):
|
|
|
|
assert p_error == ""
|
|
|
|
else:
|
|
|
|
assert p_error == str(exc)
|
2020-01-30 00:19:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_referenced_entities():
|
|
|
|
"""Test referenced entities."""
|
|
|
|
script_obj = script.Script(
|
|
|
|
None,
|
|
|
|
cv.SCRIPT_SCHEMA(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"service": "test.script",
|
|
|
|
"data": {"entity_id": "light.service_not_list"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"service": "test.script",
|
|
|
|
"data": {"entity_id": ["light.service_list"]},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"condition": "state",
|
|
|
|
"entity_id": "sensor.condition",
|
|
|
|
"state": "100",
|
|
|
|
},
|
2020-01-30 17:28:06 +00:00
|
|
|
{"service": "test.script", "data": {"without": "entity_id"}},
|
2020-01-30 00:19:13 +00:00
|
|
|
{"scene": "scene.hello"},
|
|
|
|
{"event": "test_event"},
|
|
|
|
{"delay": "{{ delay_period }}"},
|
|
|
|
]
|
|
|
|
),
|
|
|
|
)
|
|
|
|
assert script_obj.referenced_entities == {
|
|
|
|
"light.service_not_list",
|
|
|
|
"light.service_list",
|
|
|
|
"sensor.condition",
|
|
|
|
"scene.hello",
|
|
|
|
}
|
|
|
|
# Test we cache results.
|
|
|
|
assert script_obj.referenced_entities is script_obj.referenced_entities
|
|
|
|
|
|
|
|
|
|
|
|
async def test_referenced_devices():
|
|
|
|
"""Test referenced entities."""
|
|
|
|
script_obj = script.Script(
|
|
|
|
None,
|
|
|
|
cv.SCRIPT_SCHEMA(
|
|
|
|
[
|
|
|
|
{"domain": "light", "device_id": "script-dev-id"},
|
|
|
|
{
|
|
|
|
"condition": "device",
|
|
|
|
"device_id": "condition-dev-id",
|
|
|
|
"domain": "switch",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
),
|
|
|
|
)
|
|
|
|
assert script_obj.referenced_devices == {"script-dev-id", "condition-dev-id"}
|
|
|
|
# Test we cache results.
|
|
|
|
assert script_obj.referenced_devices is script_obj.referenced_devices
|