From cad0bde95b3d4c6b5fca6d64e3b7ec624e7dee51 Mon Sep 17 00:00:00 2001 From: Rene Nulsch <33263735+ReneNulschDE@users.noreply.github.com> Date: Sat, 27 Jan 2018 07:31:40 +0100 Subject: [PATCH] Panel_Iframe - Allow relative urls in config (#11832) * Panel_Iframe - Allow relative urls in config * change regex to check for starting forward slash only * Change error message and const name --- homeassistant/components/panel_iframe.py | 16 +++++++++++----- tests/components/test_panel_iframe.py | 18 ++++++++++++++++-- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/panel_iframe.py b/homeassistant/components/panel_iframe.py index e4be19c53ed..6ddf00cf7d4 100644 --- a/homeassistant/components/panel_iframe.py +++ b/homeassistant/components/panel_iframe.py @@ -8,22 +8,28 @@ import asyncio import voluptuous as vol +from homeassistant.const import (CONF_ICON, CONF_URL) import homeassistant.helpers.config_validation as cv -DOMAIN = 'panel_iframe' DEPENDENCIES = ['frontend'] +DOMAIN = 'panel_iframe' + CONF_TITLE = 'title' -CONF_ICON = 'icon' -CONF_URL = 'url' + +CONF_RELATIVE_URL_ERROR_MSG = "Invalid relative URL. Absolute path required." +CONF_RELATIVE_URL_REGEX = r'\A/' CONFIG_SCHEMA = vol.Schema({ DOMAIN: vol.Schema({ cv.slug: { vol.Optional(CONF_TITLE): cv.string, vol.Optional(CONF_ICON): cv.icon, - # pylint: disable=no-value-for-parameter - vol.Required(CONF_URL): vol.Url(), + vol.Required(CONF_URL): vol.Any( + vol.Match( + CONF_RELATIVE_URL_REGEX, + msg=CONF_RELATIVE_URL_ERROR_MSG), + cv.url), }})}, extra=vol.ALLOW_EXTRA) diff --git a/tests/components/test_panel_iframe.py b/tests/components/test_panel_iframe.py index 805d73e1820..ef702b96f4b 100644 --- a/tests/components/test_panel_iframe.py +++ b/tests/components/test_panel_iframe.py @@ -11,11 +11,11 @@ from tests.common import get_test_home_assistant class TestPanelIframe(unittest.TestCase): """Test the panel_iframe component.""" - def setup_method(self, method): + def setUp(self): """Setup things to be run when tests are started.""" self.hass = get_test_home_assistant() - def teardown_method(self, method): + def tearDown(self): """Stop everything that was started.""" self.hass.stop() @@ -50,6 +50,11 @@ class TestPanelIframe(unittest.TestCase): 'title': 'Weather', 'url': 'https://www.wunderground.com/us/ca/san-diego', }, + 'api': { + 'icon': 'mdi:weather', + 'title': 'Api', + 'url': '/api', + }, }, }) @@ -72,3 +77,12 @@ class TestPanelIframe(unittest.TestCase): 'url': '/frontend_es5/panels/ha-panel-iframe-md5md5.html', 'url_path': 'weather', } + + assert panels.get('api').to_response(self.hass, None) == { + 'component_name': 'iframe', + 'config': {'url': '/api'}, + 'icon': 'mdi:weather', + 'title': 'Api', + 'url': '/frontend_es5/panels/ha-panel-iframe-md5md5.html', + 'url_path': 'api', + }