Add enumerate to builtins in python_script component (#54244)
This commit is contained in:
parent
75726a2695
commit
7d29eb282b
2 changed files with 15 additions and 0 deletions
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue