Skip to content

Commit

Permalink
Base url: Fix external port different from internal port (home-assist…
Browse files Browse the repository at this point in the history
…ant#4990)

* Base url: Fix external port different from internal port

* Add base_url example to new config
  • Loading branch information
balloob committed Dec 18, 2016
1 parent 75dd391 commit 1258c4c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
6 changes: 4 additions & 2 deletions homeassistant/components/http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,15 @@ def start_server(event):
host = conf.get(CONF_BASE_URL)

if host:
pass
port = None
elif server_host != DEFAULT_SERVER_HOST:
host = server_host
port = server_port
else:
host = hass_util.get_local_ip()
port = server_port

hass.config.api = rem.API(host, api_password, server_port,
hass.config.api = rem.API(host, api_password, port,
ssl_certificate is not None)

return True
Expand Down
7 changes: 7 additions & 0 deletions homeassistant/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
http:
# Uncomment this to add a password (recommended!)
# api_password: PASSWORD
# Uncomment this if you are using SSL or running in Docker etc
# base_url: example.duckdns.org:8123
# Checks for available updates
updater:
Expand All @@ -76,6 +78,11 @@
# Weather Prediction
sensor:
platform: yr
# Text to speech
tts:
platform: google
"""


Expand Down
17 changes: 11 additions & 6 deletions homeassistant/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,20 @@ class API(object):
"""Object to pass around Home Assistant API location and credentials."""

def __init__(self, host: str, api_password: Optional[str]=None,
port: Optional[int]=None, use_ssl: bool=False) -> None:
port: Optional[int]=SERVER_PORT, use_ssl: bool=False) -> None:
"""Initalize the API."""
self.host = host
self.port = port or SERVER_PORT
self.port = port
self.api_password = api_password

if use_ssl:
self.base_url = "https://{}:{}".format(host, self.port)
self.base_url = "https://{}".format(host)
else:
self.base_url = "http://{}:{}".format(host, self.port)
self.base_url = "http://{}".format(host)

if port is not None:
self.base_url += ':{}'.format(port)

self.status = None
self._headers = {
HTTP_HEADER_CONTENT_TYPE: CONTENT_TYPE_JSON,
Expand Down Expand Up @@ -106,8 +111,8 @@ def __call__(self, method, path, data=None, timeout=5):

def __repr__(self) -> str:
"""Return the representation of the API."""
return "API({}, {}, {})".format(
self.host, self.api_password, self.port)
return "<API({}, password: {})>".format(
self.base_url, 'yes' if self.api_password is not None else 'no')


class HomeAssistant(ha.HomeAssistant):
Expand Down
2 changes: 1 addition & 1 deletion tests/components/http/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def test_api_base_url(loop):
})
)

assert hass.config.api.base_url == 'http://example.com:8123'
assert hass.config.api.base_url == 'http://example.com'

assert loop.run_until_complete(
bootstrap.async_setup_component(hass, 'http', {
Expand Down

0 comments on commit 1258c4c

Please sign in to comment.