Remove remote.API from core.Config (#15951)
* Use core.ApiConfig replace remote.API in core.Config * Move ApiConfig to http
This commit is contained in:
parent
f4e84fbf84
commit
1b384c322a
3 changed files with 72 additions and 5 deletions
|
@ -8,6 +8,7 @@ from ipaddress import ip_network
|
|||
import logging
|
||||
import os
|
||||
import ssl
|
||||
from typing import Optional
|
||||
|
||||
from aiohttp import web
|
||||
from aiohttp.web_exceptions import HTTPMovedPermanently
|
||||
|
@ -16,7 +17,6 @@ import voluptuous as vol
|
|||
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
|
||||
|
@ -82,6 +82,28 @@ CONFIG_SCHEMA = vol.Schema({
|
|||
}, 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)
|
||||
|
@ -146,8 +168,8 @@ async def async_setup(hass, config):
|
|||
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
|
||||
|
||||
|
|
|
@ -1145,8 +1145,8 @@ class Config:
|
|||
# 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]
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
"""The tests for the Home Assistant HTTP component."""
|
||||
import logging
|
||||
import unittest
|
||||
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
|
@ -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', {
|
||||
|
|
Loading…
Add table
Reference in a new issue