Skip to content

Commit

Permalink
Added sorted() to python_script (#10621)
Browse files Browse the repository at this point in the history
* added sorted() to python_script

* fixed lint errors
  • Loading branch information
etsinko authored and balloob committed Nov 17, 2017
1 parent 24aeea5 commit f052a09
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions homeassistant/components/python_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def protected_getattr(obj, name, default=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 = {
Expand Down
21 changes: 21 additions & 0 deletions tests/components/test_python_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,27 @@ def test_unpacking_sequence(hass, caplog):
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."""
Expand Down

0 comments on commit f052a09

Please sign in to comment.