diff --git a/homeassistant/components/python_script/__init__.py b/homeassistant/components/python_script/__init__.py index 89a7ab4ba04..922f5b71a3c 100644 --- a/homeassistant/components/python_script/__init__.py +++ b/homeassistant/components/python_script/__init__.py @@ -195,6 +195,7 @@ def execute(hass, filename, source, data=None): "sum": sum, "any": any, "all": all, + "enumerate": enumerate, } builtins = safe_builtins.copy() builtins.update(utility_builtins) diff --git a/tests/components/python_script/test_init.py b/tests/components/python_script/test_init.py index 142d833698d..1e1f24b6eee 100644 --- a/tests/components/python_script/test_init.py +++ b/tests/components/python_script/test_init.py @@ -179,6 +179,20 @@ for i in [1, 2]: assert hass.states.is_state("hello.2", "world") +async def test_using_enumerate(hass): + """Test that enumerate is accepted and executed.""" + source = """ +for index, value in enumerate(["earth", "mars"]): + hass.states.set('hello.{}'.format(index), value) + """ + + hass.async_add_job(execute, hass, "test.py", source, {}) + await hass.async_block_till_done() + + assert hass.states.is_state("hello.0", "earth") + assert hass.states.is_state("hello.1", "mars") + + async def test_unpacking_sequence(hass, caplog): """Test compile error logs error.""" caplog.set_level(logging.ERROR)