Add enumerate to builtins in python_script component (#54244)

This commit is contained in:
rjulius23 2021-08-08 07:02:20 +02:00 committed by GitHub
parent 75726a2695
commit 7d29eb282b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 0 deletions

View file

@ -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)

View file

@ -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)