diff --git a/homeassistant/components/python_script.py b/homeassistant/components/python_script.py index 75b2a1fed71..85f12a18afd 100644 --- a/homeassistant/components/python_script.py +++ b/homeassistant/components/python_script.py @@ -140,6 +140,7 @@ def execute(hass, filename, source, data=None): builtins = safe_builtins.copy() builtins.update(utility_builtins) builtins['datetime'] = datetime + builtins['sorted'] = sorted builtins['time'] = TimeWrapper() builtins['dt_util'] = dt_util restricted_globals = { diff --git a/tests/components/test_python_script.py b/tests/components/test_python_script.py index e5d6b0c4aad..8a7f94d7dcd 100644 --- a/tests/components/test_python_script.py +++ b/tests/components/test_python_script.py @@ -209,6 +209,27 @@ hass.states.set('hello.ab_list', '{}'.format(ab_list)) assert caplog.text == '' +@asyncio.coroutine +def test_execute_sorted(hass, caplog): + """Test sorted() function.""" + caplog.set_level(logging.ERROR) + source = """ +a = sorted([3,1,2]) +assert(a == [1,2,3]) +hass.states.set('hello.a', a[0]) +hass.states.set('hello.b', a[1]) +hass.states.set('hello.c', a[2]) +""" + hass.async_add_job(execute, hass, 'test.py', source, {}) + yield from hass.async_block_till_done() + + assert hass.states.is_state('hello.a', '1') + assert hass.states.is_state('hello.b', '2') + assert hass.states.is_state('hello.c', '3') + # No errors logged = good + assert caplog.text == '' + + @asyncio.coroutine def test_exposed_modules(hass, caplog): """Test datetime and time modules exposed."""