From bb372940476903d157337e245914b6b0197968f0 Mon Sep 17 00:00:00 2001 From: Andrey Date: Thu, 31 Aug 2017 07:21:24 +0300 Subject: [PATCH] Allow panels with external URL (#9214) * Allow panels with external URL * Update comment --- homeassistant/components/frontend/__init__.py | 21 ++++++++++--------- tests/components/test_frontend.py | 19 ++++++++++++++++- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/frontend/__init__.py b/homeassistant/components/frontend/__init__.py index 2f84abc745b..112c93403b0 100644 --- a/homeassistant/components/frontend/__init__.py +++ b/homeassistant/components/frontend/__init__.py @@ -109,14 +109,13 @@ def register_panel(hass, component_name, path, md5=None, sidebar_title=None, component_name: name of the web component path: path to the HTML of the web component + (required unless url is provided) md5: the md5 hash of the web component (for versioning, optional) sidebar_title: title to show in the sidebar (optional) sidebar_icon: icon to show next to title in sidebar (optional) url_path: name to use in the url (defaults to component_name) - url: for the web component (for dev environment, optional) + url: for the web component (optional) config: config to be passed into the web component - - Warning: this API will probably change. Use at own risk. """ panels = hass.data.get(DATA_PANELS) if panels is None: @@ -127,14 +126,16 @@ def register_panel(hass, component_name, path, md5=None, sidebar_title=None, if url_path in panels: _LOGGER.warning("Overwriting component %s", url_path) - if not os.path.isfile(path): - _LOGGER.error( - "Panel %s component does not exist: %s", component_name, path) - return - if md5 is None: - with open(path) as fil: - md5 = hashlib.md5(fil.read().encode('utf-8')).hexdigest() + if url is None: + if not os.path.isfile(path): + _LOGGER.error( + "Panel %s component does not exist: %s", component_name, path) + return + + if md5 is None: + with open(path) as fil: + md5 = hashlib.md5(fil.read().encode('utf-8')).hexdigest() data = { 'url_path': url_path, diff --git a/tests/components/test_frontend.py b/tests/components/test_frontend.py index d99732fefd9..fdd33b99d2b 100644 --- a/tests/components/test_frontend.py +++ b/tests/components/test_frontend.py @@ -7,7 +7,7 @@ import pytest from homeassistant.setup import async_setup_component from homeassistant.components.frontend import ( - DOMAIN, ATTR_THEMES, ATTR_EXTRA_HTML_URL) + DOMAIN, ATTR_THEMES, ATTR_EXTRA_HTML_URL, DATA_PANELS, register_panel) @pytest.fixture @@ -163,3 +163,20 @@ def test_extra_urls(mock_http_client_with_urls): assert resp.status == 200 text = yield from resp.text() assert text.find('href=\'https://domain.com/my_extra_url.html\'') >= 0 + + +@asyncio.coroutine +def test_panel_without_path(hass): + """Test panel registration without file path.""" + register_panel(hass, 'test_component', 'nonexistant_file') + assert hass.data[DATA_PANELS] == {} + + +@asyncio.coroutine +def test_panel_with_url(hass): + """Test panel registration without file path.""" + register_panel(hass, 'test_component', None, url='some_url') + assert hass.data[DATA_PANELS] == { + 'test_component': {'component_name': 'test_component', + 'url': 'some_url', + 'url_path': 'test_component'}}