Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions homeassistant/components/http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

from homeassistant.const import (
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, SERVER_PORT)
from homeassistant.core import ApiConfig
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 @@ -146,8 +146,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
28 changes: 25 additions & 3 deletions homeassistant/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP,
EVENT_SERVICE_EXECUTED, EVENT_SERVICE_REGISTERED, EVENT_STATE_CHANGED,
EVENT_TIME_CHANGED, MATCH_ALL, EVENT_HOMEASSISTANT_CLOSE,
EVENT_SERVICE_REMOVED, __version__)
EVENT_SERVICE_REMOVED, SERVER_PORT, __version__)
from homeassistant import loader
from homeassistant.exceptions import (
HomeAssistantError, InvalidEntityFormatError, InvalidStateError)
Expand Down Expand Up @@ -1127,6 +1127,28 @@ def execute_service() -> None:
_LOGGER.exception('Error executing service %s', service_call)


class ApiConfig:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it need to be in core? I think that it can just live in the http component

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved. Not sure how to set typing for config.api, use Any for now.

"""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)


class Config:
"""Configuration settings for Home Assistant."""

Expand All @@ -1145,8 +1167,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[ApiConfig]

# Directory that holds the configuration
self.config_dir = None # type: Optional[str]
Expand Down
44 changes: 44 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,50 @@ def test_is_allowed_path(self):
self.config.is_allowed_path(None)


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

def test_api_base_url_with_domain(hass):
"""Test setting API URL with domain."""
api_config = ha.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 = ha.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 = ha.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 = ha.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 = ha.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 = ha.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 = ha.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 = ha.ApiConfig('http://example.com', use_ssl=True)
assert api_config.base_url == 'http://example.com:8123'


@patch('homeassistant.core.monotonic')
def test_create_timer(mock_monotonic, loop):
"""Test create timer."""
Expand Down