Add view commands to Lovelace (#18063)

* Add get and update view command

* Add add view command

* Add move view command

* Add delete command

* lint
This commit is contained in:
Bram Kragten authored and Paulus Schoutsen committed 2018-11-01 09:44:38 +01:00
1 parent 329d128e03
commit 4163889c6b
2 files changed
+369 -2

No files matched your search

+181
View File
@@ -565,3 +565,184 @@ async def test_lovelace_delete_card(hass, hass_ws_client):
assert msg['id'] == 5
assert msg['type'] == TYPE_RESULT
assert msg['success']
async def test_lovelace_get_view(hass, hass_ws_client):
"""Test get_view command."""
await async_setup_component(hass, 'lovelace')
client = await hass_ws_client(hass)
yaml = YAML(typ='rt')
with patch('homeassistant.util.ruamel_yaml.load_yaml',
return_value=yaml.load(TEST_YAML_A)):
await client.send_json({
'id': 5,
'type': 'lovelace/config/view/get',
'view_id': 'example',
})
msg = await client.receive_json()
assert msg['id'] == 5
assert msg['type'] == TYPE_RESULT
assert msg['success']
assert "".join(msg['result'].split()) == "".join('title: Example\n # \
Optional unique id for direct\
access /lovelace/${id}\nid: example\n # Optional\
background (overwrites the global background).\n\
background: radial-gradient(crimson, skyblue)\n\
# Each view can have a different theme applied.\n\
theme: dark-mode\n'.split())
async def test_lovelace_get_view_not_found(hass, hass_ws_client):
"""Test get_card command cannot find card."""
await async_setup_component(hass, 'lovelace')
client = await hass_ws_client(hass)
yaml = YAML(typ='rt')
with patch('homeassistant.util.ruamel_yaml.load_yaml',
return_value=yaml.load(TEST_YAML_A)):
await client.send_json({
'id': 5,
'type': 'lovelace/config/view/get',
'view_id': 'not_found',
})
msg = await client.receive_json()
assert msg['id'] == 5
assert msg['type'] == TYPE_RESULT
assert msg['success'] is False
assert msg['error']['code'] == 'view_not_found'
async def test_lovelace_update_view(hass, hass_ws_client):
"""Test update_view command."""
await async_setup_component(hass, 'lovelace')
client = await hass_ws_client(hass)
yaml = YAML(typ='rt')
origyaml = yaml.load(TEST_YAML_A)
with patch('homeassistant.util.ruamel_yaml.load_yaml',
return_value=origyaml), \
patch('homeassistant.util.ruamel_yaml.save_yaml') \
as save_yaml_mock:
await client.send_json({
'id': 5,
'type': 'lovelace/config/view/update',
'view_id': 'example',
'view_config': 'id: example2\ntitle: New title\n',
})
msg = await client.receive_json()
result = save_yaml_mock.call_args_list[0][0][1]
orig_view = origyaml.mlget(['views', 0], list_ok=True)
new_view = result.mlget(['views', 0], list_ok=True)
assert new_view['title'] == 'New title'
assert new_view['cards'] == orig_view['cards']
assert 'theme' not in new_view
assert msg['id'] == 5
assert msg['type'] == TYPE_RESULT
assert msg['success']
async def test_lovelace_add_view(hass, hass_ws_client):
"""Test add_view command."""
await async_setup_component(hass, 'lovelace')
client = await hass_ws_client(hass)
yaml = YAML(typ='rt')
with patch('homeassistant.util.ruamel_yaml.load_yaml',
return_value=yaml.load(TEST_YAML_A)), \
patch('homeassistant.util.ruamel_yaml.save_yaml') \
as save_yaml_mock:
await client.send_json({
'id': 5,
'type': 'lovelace/config/view/add',
'view_config': 'id: test\ntitle: added\n',
})
msg = await client.receive_json()
result = save_yaml_mock.call_args_list[0][0][1]
assert result.mlget(['views', 2, 'title'],
list_ok=True) == 'added'
assert msg['id'] == 5
assert msg['type'] == TYPE_RESULT
assert msg['success']
async def test_lovelace_add_view_position(hass, hass_ws_client):
"""Test add_view command with position."""
await async_setup_component(hass, 'lovelace')
client = await hass_ws_client(hass)
yaml = YAML(typ='rt')
with patch('homeassistant.util.ruamel_yaml.load_yaml',
return_value=yaml.load(TEST_YAML_A)), \
patch('homeassistant.util.ruamel_yaml.save_yaml') \
as save_yaml_mock:
await client.send_json({
'id': 5,
'type': 'lovelace/config/view/add',
'position': 0,
'view_config': 'id: test\ntitle: added\n',
})
msg = await client.receive_json()
result = save_yaml_mock.call_args_list[0][0][1]
assert result.mlget(['views', 0, 'title'],
list_ok=True) == 'added'
assert msg['id'] == 5
assert msg['type'] == TYPE_RESULT
assert msg['success']
async def test_lovelace_move_view_position(hass, hass_ws_client):
"""Test move_view command."""
await async_setup_component(hass, 'lovelace')
client = await hass_ws_client(hass)
yaml = YAML(typ='rt')
with patch('homeassistant.util.ruamel_yaml.load_yaml',
return_value=yaml.load(TEST_YAML_A)), \
patch('homeassistant.util.ruamel_yaml.save_yaml') \
as save_yaml_mock:
await client.send_json({
'id': 5,
'type': 'lovelace/config/view/move',
'view_id': 'example',
'new_position': 1,
})
msg = await client.receive_json()
result = save_yaml_mock.call_args_list[0][0][1]
assert result.mlget(['views', 1, 'title'],
list_ok=True) == 'Example'
assert msg['id'] == 5
assert msg['type'] == TYPE_RESULT
assert msg['success']
async def test_lovelace_delete_view(hass, hass_ws_client):
"""Test delete_card command."""
await async_setup_component(hass, 'lovelace')
client = await hass_ws_client(hass)
yaml = YAML(typ='rt')
with patch('homeassistant.util.ruamel_yaml.load_yaml',
return_value=yaml.load(TEST_YAML_A)), \
patch('homeassistant.util.ruamel_yaml.save_yaml') \
as save_yaml_mock:
await client.send_json({
'id': 5,
'type': 'lovelace/config/view/delete',
'view_id': 'example',
})
msg = await client.receive_json()
result = save_yaml_mock.call_args_list[0][0][1]
views = result.get('views', [])
assert len(views) == 1
assert views[0]['title'] == 'Second view'
assert msg['id'] == 5
assert msg['type'] == TYPE_RESULT
assert msg['success']