Custom panel (#14708)

* Add support for custom panels in JS

* Allow specifying JS custom panels

* Add trust external option

* Fix tests

* Do I/O outside event loop

* Change config to avoid breaking change
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2018-06-01 10:06:17 -04:00
1 parent ab3717af76
commit f6eb9e79d5
2 files changed
+99 -33

No files matched your search

+61 -22
View File
@@ -1,23 +1,11 @@
"""The tests for the panel_custom component."""
import asyncio
from unittest.mock import Mock, patch
import pytest
from homeassistant import setup
from homeassistant.components import frontend
from tests.common import mock_component
@pytest.fixture(autouse=True)
def mock_frontend_loaded(hass):
"""Mock frontend is loaded."""
mock_component(hass, 'frontend')
@asyncio.coroutine
def test_webcomponent_custom_path_not_found(hass):
async def test_webcomponent_custom_path_not_found(hass):
"""Test if a web component is found in config panels dir."""
filename = 'mock.file'
@@ -33,45 +21,96 @@ def test_webcomponent_custom_path_not_found(hass):
}
with patch('os.path.isfile', Mock(return_value=False)):
result = yield from setup.async_setup_component(
result = await setup.async_setup_component(
hass, 'panel_custom', config
)
assert not result
assert len(hass.data.get(frontend.DATA_PANELS, {})) == 0
@asyncio.coroutine
def test_webcomponent_custom_path(hass):
async def test_webcomponent_custom_path(hass):
"""Test if a web component is found in config panels dir."""
filename = 'mock.file'
config = {
'panel_custom': {
'name': 'todomvc',
'name': 'todo-mvc',
'webcomponent_path': filename,
'sidebar_title': 'Sidebar Title',
'sidebar_icon': 'mdi:iconicon',
'url_path': 'nice_url',
'config': 5,
'config': {
'hello': 'world',
}
}
}
with patch('os.path.isfile', Mock(return_value=True)):
with patch('os.access', Mock(return_value=True)):
result = yield from setup.async_setup_component(
result = await setup.async_setup_component(
hass, 'panel_custom', config
)
assert result
panels = hass.data.get(frontend.DATA_PANELS, [])
assert len(panels) == 1
assert panels
assert 'nice_url' in panels
panel = panels['nice_url']
assert panel.config == 5
assert panel.config == {
'hello': 'world',
'_panel_custom': {
'html_url': '/api/panel_custom/todo-mvc',
'name': 'todo-mvc',
'embed_iframe': False,
'trust_external': False,
},
}
assert panel.frontend_url_path == 'nice_url'
assert panel.sidebar_icon == 'mdi:iconicon'
assert panel.sidebar_title == 'Sidebar Title'
assert panel.path == filename
async def test_js_webcomponent(hass):
"""Test if a web component is found in config panels dir."""
config = {
'panel_custom': {
'name': 'todo-mvc',
'js_url': '/local/bla.js',
'sidebar_title': 'Sidebar Title',
'sidebar_icon': 'mdi:iconicon',
'url_path': 'nice_url',
'config': {
'hello': 'world',
},
'embed_iframe': True,
'trust_external_script': True,
}
}
result = await setup.async_setup_component(
hass, 'panel_custom', config
)
assert result
panels = hass.data.get(frontend.DATA_PANELS, [])
assert panels
assert 'nice_url' in panels
panel = panels['nice_url']
assert panel.config == {
'hello': 'world',
'_panel_custom': {
'js_url': '/local/bla.js',
'name': 'todo-mvc',
'embed_iframe': True,
'trust_external': True,
}
}
assert panel.frontend_url_path == 'nice_url'
assert panel.sidebar_icon == 'mdi:iconicon'
assert panel.sidebar_title == 'Sidebar Title'