From fd5aad1ee75edfa2b6c4f0d9ac6c2c5e545cb63e Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 16 Jul 2016 22:32:36 -0700 Subject: [PATCH] Add panel_iframe component --- homeassistant/components/panel_iframe.py | 29 +++++++++ tests/components/test_panel_iframe.py | 77 ++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 homeassistant/components/panel_iframe.py create mode 100644 tests/components/test_panel_iframe.py diff --git a/homeassistant/components/panel_iframe.py b/homeassistant/components/panel_iframe.py new file mode 100644 index 00000000000..8df0644ea37 --- /dev/null +++ b/homeassistant/components/panel_iframe.py @@ -0,0 +1,29 @@ +import voluptuous as vol + +import homeassistant.helpers.config_validation as cv +from homeassistant.components.frontend import register_built_in_panel + +DOMAIN = 'panel_iframe' +DEPENDENCIES = ['frontend'] + +CONF_TITLE = 'title' +CONF_ICON = 'icon' +CONF_URL = 'url' + +CONFIG_SCHEMA = vol.Schema({ + DOMAIN: vol.Schema({ + cv.slug: { + vol.Optional(CONF_TITLE): cv.string, + vol.Optional(CONF_ICON): cv.icon, + vol.Required(CONF_URL): vol.Url(), + }})}, extra=vol.ALLOW_EXTRA) + + +def setup(hass, config): + """Setup iframe frontend panels.""" + for url_name, info in config[DOMAIN].items(): + register_built_in_panel( + hass, 'iframe', info.get(CONF_TITLE), info.get(CONF_ICON), + url_name, {'url': info[CONF_URL]}) + + return True diff --git a/tests/components/test_panel_iframe.py b/tests/components/test_panel_iframe.py new file mode 100644 index 00000000000..dc7fa7f0199 --- /dev/null +++ b/tests/components/test_panel_iframe.py @@ -0,0 +1,77 @@ +"""The tests for the panel_iframe component.""" +from collections import defaultdict +import unittest +from unittest.mock import patch + +from homeassistant import bootstrap +from homeassistant.components import frontend + +from tests.common import get_test_home_assistant + + +class TestPanelIframe(unittest.TestCase): + """Test the panel_iframe component.""" + + def setup_method(self, method): + """Setup things to be run when tests are started.""" + self.hass = get_test_home_assistant() + + def teardown_method(self, method): + """Stop everything that was started.""" + self.hass.stop() + frontend.PANELS = {} + + def test_wrong_config(self): + """Test setup with wrong configuration.""" + + to_try = [ + {'invalid space': { + 'url': 'https://home-assistant.io'}}, + {'router': { + 'url': 'not-a-url'}}] + + for conf in to_try: + assert not bootstrap.setup_component( + self.hass, 'panel_iframe', { + 'panel_iframe': conf + }) + + @patch.dict('homeassistant.components.frontend.FINGERPRINTS', { + 'panels/ha-panel-iframe.html': 'md5md5'}) + def test_correct_config(self): + """Test correct config.""" + assert bootstrap.setup_component( + self.hass, 'panel_iframe', { + 'panel_iframe': { + 'router': { + 'icon': 'mdi:network-wireless', + 'title': 'Router', + 'url': 'http://192.168.1.1', + }, + 'weather': { + 'icon': 'mdi:weather', + 'title': 'Weather', + 'url': 'https://www.wunderground.com/us/ca/san-diego', + }, + }, + }) + + # 5 dev tools + map are automatically loaded + assert len(frontend.PANELS) == 8 + assert frontend.PANELS['router'] == { + 'component_name': 'iframe', + 'config': {'url': 'http://192.168.1.1'}, + 'icon': 'mdi:network-wireless', + 'title': 'Router', + 'url': '/frontend/panels/iframe-md5md5.html', + 'url_name': 'router' + } + + assert frontend.PANELS['weather'] == { + 'component_name': 'iframe', + 'config': {'url': 'https://www.wunderground.com/us/ca/san-diego'}, + 'icon': 'mdi:weather', + 'title': 'Weather', + 'url': '/frontend/panels/iframe-md5md5.html', + 'url_name': 'weather', + }