Base url: Fix external port different from internal port (#4990)

* Base url: Fix external port different from internal port

* Add base_url example to new config
This commit is contained in:
Paulus Schoutsen 2016-12-18 14:59:45 -08:00
parent 75dd391118
commit 1258c4c680
4 changed files with 23 additions and 9 deletions

View File

@ -161,13 +161,15 @@ def async_setup(hass, config):
host = conf.get(CONF_BASE_URL) host = conf.get(CONF_BASE_URL)
if host: if host:
pass port = None
elif server_host != DEFAULT_SERVER_HOST: elif server_host != DEFAULT_SERVER_HOST:
host = server_host host = server_host
port = server_port
else: else:
host = hass_util.get_local_ip() 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) ssl_certificate is not None)
return True return True

View File

@ -54,6 +54,8 @@ frontend:
http: http:
# Uncomment this to add a password (recommended!) # Uncomment this to add a password (recommended!)
# api_password: PASSWORD # 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 # Checks for available updates
updater: updater:
@ -76,6 +78,11 @@ sun:
# Weather Prediction # Weather Prediction
sensor: sensor:
platform: yr platform: yr
# Text to speech
tts:
platform: google
""" """

View File

@ -55,15 +55,20 @@ class API(object):
"""Object to pass around Home Assistant API location and credentials.""" """Object to pass around Home Assistant API location and credentials."""
def __init__(self, host: str, api_password: Optional[str]=None, 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.""" """Initalize the API."""
self.host = host self.host = host
self.port = port or SERVER_PORT self.port = port
self.api_password = api_password self.api_password = api_password
if use_ssl: if use_ssl:
self.base_url = "https://{}:{}".format(host, self.port) self.base_url = "https://{}".format(host)
else: 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.status = None
self._headers = { self._headers = {
HTTP_HEADER_CONTENT_TYPE: CONTENT_TYPE_JSON, HTTP_HEADER_CONTENT_TYPE: CONTENT_TYPE_JSON,
@ -106,8 +111,8 @@ class API(object):
def __repr__(self) -> str: def __repr__(self) -> str:
"""Return the representation of the API.""" """Return the representation of the API."""
return "API({}, {}, {})".format( return "<API({}, password: {})>".format(
self.host, self.api_password, self.port) self.base_url, 'yes' if self.api_password is not None else 'no')
class HomeAssistant(ha.HomeAssistant): class HomeAssistant(ha.HomeAssistant):

View File

@ -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( assert loop.run_until_complete(
bootstrap.async_setup_component(hass, 'http', { bootstrap.async_setup_component(hass, 'http', {