From efb6fd1569558d3813c201b886b1a72f61a1f953 Mon Sep 17 00:00:00 2001 From: mk-maddin <46523240+mk-maddin@users.noreply.github.com> Date: Thu, 3 Feb 2022 17:18:58 +0100 Subject: [PATCH] Fix script / automation repeat with count 0 fails (#65448) Co-authored-by: Paulus Schoutsen Co-authored-by: Erik Montnemery --- homeassistant/helpers/script.py | 2 +- tests/helpers/test_script.py | 38 +++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/homeassistant/helpers/script.py b/homeassistant/helpers/script.py index 8e54e294f4b..5a80691fa46 100644 --- a/homeassistant/helpers/script.py +++ b/homeassistant/helpers/script.py @@ -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.""" diff --git a/tests/helpers/test_script.py b/tests/helpers/test_script.py index 2ee4213a688..5bb4833a796 100644 --- a/tests/helpers/test_script.py +++ b/tests/helpers/test_script.py @@ -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."""