From f8af6e786338afafefcf99809d41f0e6f2deca61 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 18 Dec 2016 12:56:07 -0800 Subject: [PATCH] Allow setting base url (#4985) --- homeassistant/components/http/__init__.py | 18 +++++++-- tests/components/http/test_init.py | 47 +++++++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/http/__init__.py b/homeassistant/components/http/__init__.py index 446b1f5f28bcf4..1bbb3576eed0c9 100644 --- a/homeassistant/components/http/__init__.py +++ b/homeassistant/components/http/__init__.py @@ -18,7 +18,7 @@ import homeassistant.helpers.config_validation as cv import homeassistant.remote as rem -from homeassistant.util import get_local_ip +import homeassistant.util as hass_util from homeassistant.components import persistent_notification from homeassistant.const import ( SERVER_PORT, CONTENT_TYPE_JSON, ALLOWED_CORS_HEADERS, @@ -41,6 +41,7 @@ CONF_API_PASSWORD = 'api_password' CONF_SERVER_HOST = 'server_host' CONF_SERVER_PORT = 'server_port' +CONF_BASE_URL = 'base_url' CONF_DEVELOPMENT = 'development' CONF_SSL_CERTIFICATE = 'ssl_certificate' CONF_SSL_KEY = 'ssl_key' @@ -84,6 +85,7 @@ vol.Optional(CONF_SERVER_HOST, default=DEFAULT_SERVER_HOST): cv.string, vol.Optional(CONF_SERVER_PORT, default=SERVER_PORT): vol.All(vol.Coerce(int), vol.Range(min=1, max=65535)), + vol.Optional(CONF_BASE_URL): cv.string, vol.Optional(CONF_DEVELOPMENT, default=DEFAULT_DEVELOPMENT): cv.string, vol.Optional(CONF_SSL_CERTIFICATE, default=None): cv.isfile, vol.Optional(CONF_SSL_KEY, default=None): cv.isfile, @@ -155,9 +157,17 @@ def start_server(event): hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, start_server) hass.http = server - hass.config.api = rem.API(server_host if server_host != '0.0.0.0' - else get_local_ip(), - api_password, server_port, + + host = conf.get(CONF_BASE_URL) + + if host: + pass + elif server_host != DEFAULT_SERVER_HOST: + host = server_host + else: + host = hass_util.get_local_ip() + + hass.config.api = rem.API(host, api_password, server_port, ssl_certificate is not None) return True diff --git a/tests/components/http/test_init.py b/tests/components/http/test_init.py index f50e1fb9dbf92b..10793406ea7b34 100644 --- a/tests/components/http/test_init.py +++ b/tests/components/http/test_init.py @@ -1,6 +1,7 @@ """The tests for the Home Assistant HTTP component.""" import asyncio import requests +from unittest.mock import MagicMock from homeassistant import bootstrap, const import homeassistant.components.http as http @@ -154,3 +155,49 @@ def test_registering_view_while_running(hass, test_client): text = yield from resp.text() assert text == 'hello' + + +def test_api_base_url(loop): + """Test setting api url.""" + + hass = MagicMock() + hass.loop = loop + + assert loop.run_until_complete( + bootstrap.async_setup_component(hass, 'http', { + 'http': { + 'base_url': 'example.com' + } + }) + ) + + assert hass.config.api.base_url == 'http://example.com:8123' + + assert loop.run_until_complete( + bootstrap.async_setup_component(hass, 'http', { + 'http': { + 'server_host': '1.1.1.1' + } + }) + ) + + assert hass.config.api.base_url == 'http://1.1.1.1:8123' + + assert loop.run_until_complete( + bootstrap.async_setup_component(hass, 'http', { + 'http': { + 'server_host': '1.1.1.1' + } + }) + ) + + assert hass.config.api.base_url == 'http://1.1.1.1:8123' + + assert loop.run_until_complete( + bootstrap.async_setup_component(hass, 'http', { + 'http': { + } + }) + ) + + assert hass.config.api.base_url == 'http://127.0.0.1:8123'