Allow iteration in python_script (#8134)

* Allow iteration in python_script

* Add tests
This commit is contained in:
Paulus Schoutsen 2017-06-21 04:32:50 -07:00 committed by Pascal Vizeli
parent 4d2b79156d
commit 6398e92836
2 changed files with 16 additions and 0 deletions

View File

@ -92,6 +92,7 @@ def execute(hass, filename, source, data=None):
'_print_': StubPrinter, '_print_': StubPrinter,
'_getattr_': protected_getattr, '_getattr_': protected_getattr,
'_write_': full_write_guard, '_write_': full_write_guard,
'_getiter_': iter,
} }
logger = logging.getLogger('{}.{}'.format(__name__, filename)) logger = logging.getLogger('{}.{}'.format(__name__, filename))
local = { local = {

View File

@ -149,3 +149,18 @@ hass.stop()
yield from hass.async_block_till_done() yield from hass.async_block_till_done()
assert "Not allowed to access HomeAssistant.stop" in caplog.text assert "Not allowed to access HomeAssistant.stop" in caplog.text
@asyncio.coroutine
def test_iterating(hass):
"""Test compile error logs error."""
source = """
for i in [1, 2]:
hass.states.set('hello.{}'.format(i), 'world')
"""
hass.async_add_job(execute, hass, 'test.py', source, {})
yield from hass.async_block_till_done()
assert hass.states.is_state('hello.1', 'world')
assert hass.states.is_state('hello.2', 'world')