Fix script / automation repeat with count 0 fails (#65448)

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
mk-maddin 2022-02-03 17:18:58 +01:00 committed by GitHub
parent 778cc6106a
commit efb6fd1569
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 1 deletions

View file

@ -742,7 +742,7 @@ class _ScriptRun:
if saved_repeat_vars:
self._variables["repeat"] = saved_repeat_vars
else:
del self._variables["repeat"]
self._variables.pop("repeat", None) # Not set if count = 0
async def _async_choose_step(self) -> None:
"""Choose a sequence."""

View file

@ -1742,6 +1742,44 @@ async def test_repeat_count(hass, caplog, count):
)
async def test_repeat_count_0(hass, caplog):
"""Test repeat action w/ count option."""
event = "test_event"
events = async_capture_events(hass, event)
count = 0
alias = "condition step"
sequence = cv.SCRIPT_SCHEMA(
{
"alias": alias,
"repeat": {
"count": count,
"sequence": {
"event": event,
"event_data_template": {
"first": "{{ repeat.first }}",
"index": "{{ repeat.index }}",
"last": "{{ repeat.last }}",
},
},
},
}
)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
await script_obj.async_run(context=Context())
await hass.async_block_till_done()
assert len(events) == count
assert caplog.text.count(f"Repeating {alias}") == count
assert_action_trace(
{
"0": [{}],
}
)
@pytest.mark.parametrize("condition", ["while", "until"])
async def test_repeat_condition_warning(hass, caplog, condition):
"""Test warning on repeat conditions."""