2018-09-25 06:39:35 +00:00
|
|
|
"""Test the Lovelace initialization."""
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
from homeassistant.setup import async_setup_component
|
2018-12-10 07:57:17 +00:00
|
|
|
from homeassistant.components import frontend, lovelace
|
|
|
|
|
|
|
|
|
|
|
|
async def test_lovelace_from_storage(hass, hass_ws_client, hass_storage):
|
|
|
|
"""Test we load lovelace config from storage."""
|
|
|
|
assert await async_setup_component(hass, 'lovelace', {})
|
|
|
|
assert hass.data[frontend.DATA_PANELS]['lovelace'].config == {
|
|
|
|
'mode': 'storage'
|
|
|
|
}
|
|
|
|
|
|
|
|
client = await hass_ws_client(hass)
|
|
|
|
|
|
|
|
# Fetch data
|
|
|
|
await client.send_json({
|
|
|
|
'id': 5,
|
|
|
|
'type': 'lovelace/config'
|
|
|
|
})
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert not response['success']
|
|
|
|
assert response['error']['code'] == 'config_not_found'
|
|
|
|
|
|
|
|
# Store new config
|
|
|
|
await client.send_json({
|
|
|
|
'id': 6,
|
|
|
|
'type': 'lovelace/config/save',
|
|
|
|
'config': {
|
|
|
|
'yo': 'hello'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response['success']
|
|
|
|
assert hass_storage[lovelace.STORAGE_KEY]['data'] == {
|
2018-12-10 11:25:08 +00:00
|
|
|
'config': {'yo': 'hello'}
|
2018-12-10 07:57:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Load new config
|
|
|
|
await client.send_json({
|
|
|
|
'id': 7,
|
|
|
|
'type': 'lovelace/config'
|
|
|
|
})
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response['success']
|
|
|
|
|
|
|
|
assert response['result'] == {
|
|
|
|
'yo': 'hello'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-24 05:13:55 +00:00
|
|
|
async def test_lovelace_from_storage_save_before_load(hass, hass_ws_client,
|
|
|
|
hass_storage):
|
|
|
|
"""Test we can load lovelace config from storage."""
|
|
|
|
assert await async_setup_component(hass, 'lovelace', {})
|
|
|
|
client = await hass_ws_client(hass)
|
|
|
|
|
|
|
|
# Store new config
|
|
|
|
await client.send_json({
|
|
|
|
'id': 6,
|
|
|
|
'type': 'lovelace/config/save',
|
|
|
|
'config': {
|
|
|
|
'yo': 'hello'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response['success']
|
|
|
|
assert hass_storage[lovelace.STORAGE_KEY]['data'] == {
|
|
|
|
'config': {'yo': 'hello'}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-10 07:57:17 +00:00
|
|
|
async def test_lovelace_from_yaml(hass, hass_ws_client):
|
|
|
|
"""Test we load lovelace config from yaml."""
|
|
|
|
assert await async_setup_component(hass, 'lovelace', {
|
|
|
|
'lovelace': {
|
|
|
|
'mode': 'YAML'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
assert hass.data[frontend.DATA_PANELS]['lovelace'].config == {
|
|
|
|
'mode': 'yaml'
|
|
|
|
}
|
|
|
|
|
|
|
|
client = await hass_ws_client(hass)
|
|
|
|
|
|
|
|
# Fetch data
|
|
|
|
await client.send_json({
|
|
|
|
'id': 5,
|
|
|
|
'type': 'lovelace/config'
|
|
|
|
})
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert not response['success']
|
|
|
|
|
|
|
|
assert response['error']['code'] == 'config_not_found'
|
|
|
|
|
|
|
|
# Store new config not allowed
|
|
|
|
await client.send_json({
|
|
|
|
'id': 6,
|
|
|
|
'type': 'lovelace/config/save',
|
|
|
|
'config': {
|
|
|
|
'yo': 'hello'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert not response['success']
|
|
|
|
|
|
|
|
# Patch data
|
|
|
|
with patch('homeassistant.components.lovelace.load_yaml', return_value={
|
|
|
|
'hello': 'yo'
|
|
|
|
}):
|
|
|
|
await client.send_json({
|
|
|
|
'id': 7,
|
|
|
|
'type': 'lovelace/config'
|
|
|
|
})
|
|
|
|
response = await client.receive_json()
|
|
|
|
|
|
|
|
assert response['success']
|
|
|
|
assert response['result'] == {'hello': 'yo'}
|