Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions homeassistant/components/http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import logging
import os
import ssl
from typing import Optional

from aiohttp import web
from aiohttp.web_exceptions import HTTPMovedPermanently
Expand All @@ -16,7 +17,6 @@
from homeassistant.const import (
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, SERVER_PORT)
import homeassistant.helpers.config_validation as cv
import homeassistant.remote as rem
import homeassistant.util as hass_util
from homeassistant.util.logging import HideSensitiveDataFilter
from homeassistant.util import ssl as ssl_util
Expand Down Expand Up @@ -82,6 +82,28 @@
}, extra=vol.ALLOW_EXTRA)


class ApiConfig:
"""Configuration settings for API server."""

def __init__(self, host: str, port: Optional[int] = SERVER_PORT,
use_ssl: bool = False,
api_password: Optional[str] = None) -> None:
"""Initialize a new API config object."""
self.host = host
self.port = port
self.api_password = api_password

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

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


async def async_setup(hass, config):
"""Set up the HTTP API and debug interface."""
conf = config.get(DOMAIN)
Expand Down Expand Up @@ -146,8 +168,8 @@ async def start_server(event):
host = hass_util.get_local_ip()
port = server_port

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

return True

Expand Down
4 changes: 2 additions & 2 deletions homeassistant/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1145,8 +1145,8 @@ def __init__(self) -> None:
# List of loaded components
self.components = set() # type: set

# Remote.API object pointing at local API
self.api = None
# API (HTTP) server configuration
self.api = None # type: Optional[Any]

# Directory that holds the configuration
self.config_dir = None # type: Optional[str]
Expand Down
45 changes: 45 additions & 0 deletions tests/components/http/test_init.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""The tests for the Home Assistant HTTP component."""
import logging
import unittest

from homeassistant.setup import async_setup_component

Expand Down Expand Up @@ -33,6 +34,50 @@ async def test_registering_view_while_running(hass, aiohttp_client,
hass.http.register_view(TestView)


class TestApiConfig(unittest.TestCase):
"""Test API configuration methods."""

def test_api_base_url_with_domain(hass):
"""Test setting API URL with domain."""
api_config = http.ApiConfig('example.com')
assert api_config.base_url == 'http://example.com:8123'

def test_api_base_url_with_ip(hass):
"""Test setting API URL with IP."""
api_config = http.ApiConfig('1.1.1.1')
assert api_config.base_url == 'http://1.1.1.1:8123'

def test_api_base_url_with_ip_and_port(hass):
"""Test setting API URL with IP and port."""
api_config = http.ApiConfig('1.1.1.1', 8124)
assert api_config.base_url == 'http://1.1.1.1:8124'

def test_api_base_url_with_protocol(hass):
"""Test setting API URL with protocol."""
api_config = http.ApiConfig('https://example.com')
assert api_config.base_url == 'https://example.com:8123'

def test_api_base_url_with_protocol_and_port(hass):
"""Test setting API URL with protocol and port."""
api_config = http.ApiConfig('https://example.com', 433)
assert api_config.base_url == 'https://example.com:433'

def test_api_base_url_with_ssl_enable(hass):
"""Test setting API URL with use_ssl enabled."""
api_config = http.ApiConfig('example.com', use_ssl=True)
assert api_config.base_url == 'https://example.com:8123'

def test_api_base_url_with_ssl_enable_and_port(hass):
"""Test setting API URL with use_ssl enabled and port."""
api_config = http.ApiConfig('1.1.1.1', use_ssl=True, port=8888)
assert api_config.base_url == 'https://1.1.1.1:8888'

def test_api_base_url_with_protocol_and_ssl_enable(hass):
"""Test setting API URL with specific protocol and use_ssl enabled."""
api_config = http.ApiConfig('http://example.com', use_ssl=True)
assert api_config.base_url == 'http://example.com:8123'


async def test_api_base_url_with_domain(hass):
"""Test setting API URL."""
result = await async_setup_component(hass, 'http', {
Expand Down