1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Template: Expand method to expand groups, and closest as filter (#23691)

* Implement expand method

* Allow expand and closest to be used as filters

* Correct patch

* Addresses review comments
This commit is contained in:
Penny Wood
2019-06-22 15:32:32 +08:00
committed by Paulus Schoutsen
parent a6eef22fbc
commit 22d9bee41a
3 changed files with 346 additions and 185 deletions

View File

@@ -620,7 +620,7 @@ def test_states_function(hass):
def test_now(mock_is_safe, hass):
"""Test now method."""
now = dt_util.now()
with patch.dict(template.ENV.globals, {'now': lambda: now}):
with patch('homeassistant.util.dt.now', return_value=now):
assert now.isoformat() == \
template.Template('{{ now().isoformat() }}',
hass).async_render()
@@ -631,7 +631,7 @@ def test_now(mock_is_safe, hass):
def test_utcnow(mock_is_safe, hass):
"""Test utcnow method."""
now = dt_util.utcnow()
with patch.dict(template.ENV.globals, {'utcnow': lambda: now}):
with patch('homeassistant.util.dt.utcnow', return_value=now):
assert now.isoformat() == \
template.Template('{{ utcnow().isoformat() }}',
hass).async_render()
@@ -882,6 +882,9 @@ def test_closest_function_home_vs_domain(hass):
assert template.Template('{{ closest(states.test_domain).entity_id }}',
hass).async_render() == 'test_domain.object'
assert template.Template('{{ (states.test_domain | closest).entity_id }}',
hass).async_render() == 'test_domain.object'
def test_closest_function_home_vs_all_states(hass):
"""Test closest function home vs all states."""
@@ -898,6 +901,9 @@ def test_closest_function_home_vs_all_states(hass):
assert template.Template('{{ closest(states).entity_id }}',
hass).async_render() == 'test_domain_2.and_closer'
assert template.Template('{{ (states | closest).entity_id }}',
hass).async_render() == 'test_domain_2.and_closer'
async def test_closest_function_home_vs_group_entity_id(hass):
"""Test closest function home vs group entity id."""
@@ -948,6 +954,74 @@ async def test_closest_function_home_vs_group_state(hass):
['test_domain.object', 'group.location_group'])
async def test_expand(hass):
"""Test expand function."""
info = render_to_info(
hass, "{{ expand('test.object') }}")
assert_result_info(
info, '[]',
['test.object'])
info = render_to_info(
hass, "{{ expand(56) }}")
assert_result_info(
info, '[]')
hass.states.async_set('test.object', 'happy')
info = render_to_info(
hass, "{{ expand('test.object') | map(attribute='entity_id')"
" | join(', ') }}")
assert_result_info(
info, 'test.object',
[])
info = render_to_info(
hass, "{{ expand('group.new_group') | map(attribute='entity_id')"
" | join(', ') }}")
assert_result_info(
info, '',
['group.new_group'])
info = render_to_info(
hass, "{{ expand(states.group) | map(attribute='entity_id')"
" | join(', ') }}")
assert_result_info(
info, '',
[], ['group'])
await group.Group.async_create_group(
hass, 'new group', ['test.object'])
info = render_to_info(
hass, "{{ expand('group.new_group') | map(attribute='entity_id')"
" | join(', ') }}")
assert_result_info(
info, 'test.object',
['group.new_group'])
info = render_to_info(
hass, "{{ expand(states.group) | map(attribute='entity_id')"
" | join(', ') }}")
assert_result_info(
info, 'test.object',
['group.new_group'], ['group'])
info = render_to_info(
hass, "{{ expand('group.new_group', 'test.object')"
" | map(attribute='entity_id') | join(', ') }}")
assert_result_info(
info, 'test.object',
['group.new_group'])
info = render_to_info(
hass, "{{ ['group.new_group', 'test.object'] | expand"
" | map(attribute='entity_id') | join(', ') }}")
assert_result_info(
info, 'test.object',
['group.new_group'])
def test_closest_function_to_coord(hass):
"""Test closest function to coord."""
hass.states.async_set('test_domain.closest_home', 'happy', {
@@ -972,6 +1046,13 @@ def test_closest_function_to_coord(hass):
assert tpl.async_render() == 'test_domain.closest_zone'
tpl = template.Template(
'{{ (states.test_domain | closest("%s", %s)).entity_id }}'
% (hass.config.latitude + 0.3,
hass.config.longitude + 0.3), hass)
assert tpl.async_render() == 'test_domain.closest_zone'
def test_closest_function_to_entity_id(hass):
"""Test closest function to entity id."""
@@ -1003,6 +1084,20 @@ def test_closest_function_to_entity_id(hass):
'zone.far_away'],
["test_domain"])
info = render_to_info(
hass,
"{{ ([states.test_domain, 'test_domain.closest_zone'] "
"| closest(zone)).entity_id }}",
{
'zone': 'zone.far_away'
})
assert_result_info(
info, 'test_domain.closest_zone',
['test_domain.closest_home', 'test_domain.closest_zone',
'zone.far_away'],
["test_domain"])
def test_closest_function_to_state(hass):
"""Test closest function to state."""
@@ -1060,6 +1155,8 @@ def test_closest_function_invalid_coordinates(hass):
assert template.Template('{{ closest("invalid", "coord", states) }}',
hass).async_render() == 'None'
assert template.Template('{{ states | closest("invalid", "coord") }}',
hass).async_render() == 'None'
def test_closest_function_no_location_states(hass):