Skip to content

Commit

Permalink
Allow setting base url (home-assistant#4985)
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob authored Dec 18, 2016
1 parent fec3334 commit f8af6e7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
18 changes: 14 additions & 4 deletions homeassistant/components/http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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'
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
47 changes: 47 additions & 0 deletions tests/components/http/test_init.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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'

0 comments on commit f8af6e7

Please sign in to comment.