Update script helper constructor parameters (#38763)

Add domain and make it and name required.

Add optional running_description.
This commit is contained in:
Phil Bruckner
2020-08-12 11:39:05 -05:00
committed by GitHub
parent fbf44b37a9
commit 716fa63e73
20 changed files with 194 additions and 100 deletions

View File

@@ -85,14 +85,16 @@ def async_watch_for_action(script_obj, message):
return flag
async def test_firing_event_basic(hass):
async def test_firing_event_basic(hass, caplog):
"""Test the firing of events."""
event = "test_event"
context = Context()
events = async_capture_events(hass, event)
sequence = cv.SCRIPT_SCHEMA({"event": event, "event_data": {"hello": "world"}})
script_obj = script.Script(hass, sequence)
script_obj = script.Script(
hass, sequence, "Test Name", "test_domain", running_description="test script"
)
await script_obj.async_run(context=context)
await hass.async_block_till_done()
@@ -100,6 +102,8 @@ async def test_firing_event_basic(hass):
assert len(events) == 1
assert events[0].context is context
assert events[0].data.get("hello") == "world"
assert ".test_name:" in caplog.text
assert "Test Name: Running test script" in caplog.text
async def test_firing_event_template(hass):
@@ -121,7 +125,7 @@ async def test_firing_event_template(hass):
},
}
)
script_obj = script.Script(hass, sequence)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
await script_obj.async_run(MappingProxyType({"is_world": "yes"}), context=context)
await hass.async_block_till_done()
@@ -140,7 +144,7 @@ async def test_calling_service_basic(hass):
calls = async_mock_service(hass, "test", "script")
sequence = cv.SCRIPT_SCHEMA({"service": "test.script", "data": {"hello": "world"}})
script_obj = script.Script(hass, sequence)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
await script_obj.async_run(context=context)
await hass.async_block_till_done()
@@ -174,7 +178,7 @@ async def test_calling_service_template(hass):
},
}
)
script_obj = script.Script(hass, sequence)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
await script_obj.async_run(MappingProxyType({"is_world": "yes"}), context=context)
await hass.async_block_till_done()
@@ -227,7 +231,9 @@ async def test_multiple_runs_no_wait(hass):
},
]
)
script_obj = script.Script(hass, sequence, script_mode="parallel", max_runs=2)
script_obj = script.Script(
hass, sequence, "Test Name", "test_domain", script_mode="parallel", max_runs=2
)
# Start script twice in such a way that second run will be started while first run
# is in the middle of the first service call.
@@ -259,7 +265,7 @@ async def test_activating_scene(hass):
calls = async_mock_service(hass, scene.DOMAIN, SERVICE_TURN_ON)
sequence = cv.SCRIPT_SCHEMA({"scene": "scene.hello"})
script_obj = script.Script(hass, sequence)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
await script_obj.async_run(context=context)
await hass.async_block_till_done()
@@ -285,7 +291,14 @@ async def test_stop_no_wait(hass, count):
hass.services.async_register("test", "script", async_simulate_long_service)
sequence = cv.SCRIPT_SCHEMA([{"service": "test.script"}, {"event": event}])
script_obj = script.Script(hass, sequence, script_mode="parallel", max_runs=count)
script_obj = script.Script(
hass,
sequence,
"Test Name",
"test_domain",
script_mode="parallel",
max_runs=count,
)
# Get script started specified number of times and wait until the test.script
# service has started for each run.
@@ -317,7 +330,7 @@ async def test_delay_basic(hass, mock_timeout):
"""Test the delay."""
delay_alias = "delay step"
sequence = cv.SCRIPT_SCHEMA({"delay": {"seconds": 5}, "alias": delay_alias})
script_obj = script.Script(hass, sequence)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
delay_started_flag = async_watch_for_action(script_obj, delay_alias)
try:
@@ -349,7 +362,9 @@ async def test_multiple_runs_delay(hass, mock_timeout):
{"event": event, "event_data": {"value": 2}},
]
)
script_obj = script.Script(hass, sequence, script_mode="parallel", max_runs=2)
script_obj = script.Script(
hass, sequence, "Test Name", "test_domain", script_mode="parallel", max_runs=2
)
delay_started_flag = async_watch_for_action(script_obj, "delay")
try:
@@ -381,7 +396,7 @@ async def test_multiple_runs_delay(hass, mock_timeout):
async def test_delay_template_ok(hass, mock_timeout):
"""Test the delay as a template."""
sequence = cv.SCRIPT_SCHEMA({"delay": "00:00:{{ 5 }}"})
script_obj = script.Script(hass, sequence)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
delay_started_flag = async_watch_for_action(script_obj, "delay")
try:
@@ -411,7 +426,7 @@ async def test_delay_template_invalid(hass, caplog):
{"event": event},
]
)
script_obj = script.Script(hass, sequence)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
start_idx = len(caplog.records)
await script_obj.async_run()
@@ -429,7 +444,7 @@ async def test_delay_template_invalid(hass, caplog):
async def test_delay_template_complex_ok(hass, mock_timeout):
"""Test the delay with a working complex template."""
sequence = cv.SCRIPT_SCHEMA({"delay": {"seconds": "{{ 5 }}"}})
script_obj = script.Script(hass, sequence)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
delay_started_flag = async_watch_for_action(script_obj, "delay")
try:
@@ -458,7 +473,7 @@ async def test_delay_template_complex_invalid(hass, caplog):
{"event": event},
]
)
script_obj = script.Script(hass, sequence)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
start_idx = len(caplog.records)
await script_obj.async_run()
@@ -478,7 +493,7 @@ async def test_cancel_delay(hass):
event = "test_event"
events = async_capture_events(hass, event)
sequence = cv.SCRIPT_SCHEMA([{"delay": {"seconds": 5}}, {"event": event}])
script_obj = script.Script(hass, sequence)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
delay_started_flag = async_watch_for_action(script_obj, "delay")
try:
@@ -513,7 +528,7 @@ async def test_wait_template_basic(hass):
"alias": wait_alias,
}
)
script_obj = script.Script(hass, sequence)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
wait_started_flag = async_watch_for_action(script_obj, wait_alias)
try:
@@ -545,7 +560,9 @@ async def test_multiple_runs_wait_template(hass):
{"event": event, "event_data": {"value": 2}},
]
)
script_obj = script.Script(hass, sequence, script_mode="parallel", max_runs=2)
script_obj = script.Script(
hass, sequence, "Test Name", "test_domain", script_mode="parallel", max_runs=2
)
wait_started_flag = async_watch_for_action(script_obj, "wait")
try:
@@ -582,7 +599,7 @@ async def test_cancel_wait_template(hass):
{"event": event},
]
)
script_obj = script.Script(hass, sequence)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
wait_started_flag = async_watch_for_action(script_obj, "wait")
try:
@@ -620,7 +637,7 @@ async def test_wait_template_not_schedule(hass):
{"event": event},
]
)
script_obj = script.Script(hass, sequence)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
hass.states.async_set("switch.test", "on")
await script_obj.async_run()
@@ -644,7 +661,7 @@ async def test_wait_template_timeout(hass, mock_timeout, continue_on_timeout, n_
if continue_on_timeout is not None:
sequence[0]["continue_on_timeout"] = continue_on_timeout
sequence = cv.SCRIPT_SCHEMA(sequence)
script_obj = script.Script(hass, sequence)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
wait_started_flag = async_watch_for_action(script_obj, "wait")
try:
@@ -668,7 +685,7 @@ async def test_wait_template_timeout(hass, mock_timeout, continue_on_timeout, n_
async def test_wait_template_variables(hass):
"""Test the wait template with variables."""
sequence = cv.SCRIPT_SCHEMA({"wait_template": "{{ is_state(data, 'off') }}"})
script_obj = script.Script(hass, sequence)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
wait_started_flag = async_watch_for_action(script_obj, "wait")
try:
@@ -703,7 +720,7 @@ async def test_condition_basic(hass):
{"event": event},
]
)
script_obj = script.Script(hass, sequence)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
hass.states.async_set("test.entity", "hello")
await script_obj.async_run()
@@ -728,7 +745,9 @@ async def test_condition_created_once(async_from_config, hass):
"value_template": '{{ states.test.entity.state == "hello" }}',
}
)
script_obj = script.Script(hass, sequence, script_mode="parallel", max_runs=2)
script_obj = script.Script(
hass, sequence, "Test Name", "test_domain", script_mode="parallel", max_runs=2
)
async_from_config.reset_mock()
@@ -755,7 +774,7 @@ async def test_condition_all_cached(hass):
},
]
)
script_obj = script.Script(hass, sequence)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
hass.states.async_set("test.entity", "hello")
await script_obj.async_run()
@@ -785,7 +804,7 @@ async def test_repeat_count(hass):
}
}
)
script_obj = script.Script(hass, sequence)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
await script_obj.async_run()
await hass.async_block_till_done()
@@ -829,7 +848,9 @@ async def test_repeat_conditional(hass, condition):
"condition": "template",
"value_template": "{{ is_state('sensor.test', 'done') }}",
}
script_obj = script.Script(hass, cv.SCRIPT_SCHEMA(sequence))
script_obj = script.Script(
hass, cv.SCRIPT_SCHEMA(sequence), "Test Name", "test_domain"
)
wait_started = async_watch_for_action(script_obj, "wait")
hass.states.async_set("sensor.test", "1")
@@ -876,7 +897,9 @@ async def test_repeat_var_in_condition(hass, condition):
"condition": "template",
"value_template": "{{ repeat.index == 2 }}",
}
script_obj = script.Script(hass, cv.SCRIPT_SCHEMA(sequence))
script_obj = script.Script(
hass, cv.SCRIPT_SCHEMA(sequence), "Test Name", "test_domain"
)
with mock.patch(
"homeassistant.helpers.condition._LOGGER.error",
@@ -956,7 +979,7 @@ async def test_repeat_nested(hass, variables, first_last, inside_x):
},
]
)
script_obj = script.Script(hass, sequence, "test script")
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
with mock.patch(
"homeassistant.helpers.condition._LOGGER.error",
@@ -1014,7 +1037,7 @@ async def test_choose(hass, var, result):
"default": {"event": event, "event_data": {"choice": "default"}},
}
)
script_obj = script.Script(hass, sequence)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
await script_obj.async_run(MappingProxyType({"var": var}))
await hass.async_block_till_done()
@@ -1035,7 +1058,12 @@ async def test_multiple_runs_repeat_choose(hass, caplog, action):
"""Test parallel runs with repeat & choose actions & max_runs > default."""
max_runs = script.DEFAULT_MAX + 1
script_obj = script.Script(
hass, cv.SCRIPT_SCHEMA(action), script_mode="parallel", max_runs=max_runs
hass,
cv.SCRIPT_SCHEMA(action),
"Test Name",
"test_domain",
script_mode="parallel",
max_runs=max_runs,
)
events = async_capture_events(hass, "abc")
@@ -1052,7 +1080,7 @@ async def test_last_triggered(hass):
"""Test the last_triggered."""
event = "test_event"
sequence = cv.SCRIPT_SCHEMA({"event": event})
script_obj = script.Script(hass, sequence)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
assert script_obj.last_triggered is None
@@ -1069,7 +1097,7 @@ async def test_propagate_error_service_not_found(hass):
event = "test_event"
events = async_capture_events(hass, event)
sequence = cv.SCRIPT_SCHEMA([{"service": "test.script"}, {"event": event}])
script_obj = script.Script(hass, sequence)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
with pytest.raises(exceptions.ServiceNotFound):
await script_obj.async_run()
@@ -1086,7 +1114,7 @@ async def test_propagate_error_invalid_service_data(hass):
sequence = cv.SCRIPT_SCHEMA(
[{"service": "test.script", "data": {"text": 1}}, {"event": event}]
)
script_obj = script.Script(hass, sequence)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
with pytest.raises(vol.Invalid):
await script_obj.async_run()
@@ -1109,7 +1137,7 @@ async def test_propagate_error_service_exception(hass):
hass.services.async_register("test", "script", record_call)
sequence = cv.SCRIPT_SCHEMA([{"service": "test.script"}, {"event": event}])
script_obj = script.Script(hass, sequence)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
with pytest.raises(ValueError):
await script_obj.async_run()
@@ -1143,6 +1171,8 @@ async def test_referenced_entities(hass):
{"delay": "{{ delay_period }}"},
]
),
"Test Name",
"test_domain",
)
assert script_obj.referenced_entities == {
"light.service_not_list",
@@ -1168,6 +1198,8 @@ async def test_referenced_devices(hass):
},
]
),
"Test Name",
"test_domain",
)
assert script_obj.referenced_devices == {"script-dev-id", "condition-dev-id"}
# Test we cache results.
@@ -1191,7 +1223,7 @@ async def test_script_mode_single(hass, caplog):
{"event": event, "event_data": {"value": 2}},
]
)
script_obj = script.Script(hass, sequence)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
wait_started_flag = async_watch_for_action(script_obj, "wait")
try:
@@ -1239,7 +1271,13 @@ async def test_script_mode_2(hass, caplog, script_mode, messages, last_events):
logger = logging.getLogger("TEST")
max_runs = 1 if script_mode == "restart" else 2
script_obj = script.Script(
hass, sequence, script_mode=script_mode, max_runs=max_runs, logger=logger
hass,
sequence,
"Test Name",
"test_domain",
script_mode=script_mode,
max_runs=max_runs,
logger=logger,
)
wait_started_flag = async_watch_for_action(script_obj, "wait")
@@ -1303,7 +1341,13 @@ async def test_script_mode_queued(hass):
)
logger = logging.getLogger("TEST")
script_obj = script.Script(
hass, sequence, script_mode="queued", max_runs=2, logger=logger
hass,
sequence,
"Test Name",
"test_domain",
script_mode="queued",
max_runs=2,
logger=logger,
)
watch_messages = []
@@ -1379,7 +1423,8 @@ async def test_script_mode_queued_cancel(hass):
script_obj = script.Script(
hass,
cv.SCRIPT_SCHEMA({"wait_template": "{{ false }}"}),
"test",
"Test Name",
"test_domain",
script_mode="queued",
max_runs=2,
)
@@ -1417,21 +1462,17 @@ async def test_script_mode_queued_cancel(hass):
async def test_script_logging(hass, caplog):
"""Test script logging."""
script_obj = script.Script(hass, [], "Script with % Name")
script_obj = script.Script(hass, [], "Script with % Name", "test_domain")
script_obj._log("Test message with name %s", 1)
assert "Script with % Name: Test message with name 1" in caplog.text
script_obj = script.Script(hass, [])
script_obj._log("Test message without name %s", 2)
assert "Test message without name 2" in caplog.text
async def test_shutdown_at(hass, caplog):
"""Test stopping scripts at shutdown."""
delay_alias = "delay step"
sequence = cv.SCRIPT_SCHEMA({"delay": {"seconds": 120}, "alias": delay_alias})
script_obj = script.Script(hass, sequence, "test script")
script_obj = script.Script(hass, sequence, "test script", "test_domain")
delay_started_flag = async_watch_for_action(script_obj, delay_alias)
try:
@@ -1455,7 +1496,7 @@ async def test_shutdown_after(hass, caplog):
"""Test stopping scripts at shutdown."""
delay_alias = "delay step"
sequence = cv.SCRIPT_SCHEMA({"delay": {"seconds": 120}, "alias": delay_alias})
script_obj = script.Script(hass, sequence, "test script")
script_obj = script.Script(hass, sequence, "test script", "test_domain")
delay_started_flag = async_watch_for_action(script_obj, delay_alias)
hass.state = CoreState.stopping
@@ -1485,7 +1526,7 @@ async def test_shutdown_after(hass, caplog):
async def test_update_logger(hass, caplog):
"""Test updating logger."""
sequence = cv.SCRIPT_SCHEMA({"event": "test_event"})
script_obj = script.Script(hass, sequence)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
await script_obj.async_run()
await hass.async_block_till_done()