mirror of
https://github.com/home-assistant/core.git
synced 2025-04-27 02:37:50 +00:00
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
31fbfed0a6
commit
272be7cdae
@ -8,6 +8,7 @@ from ipaddress import ip_network
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import ssl
|
import ssl
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
from aiohttp.web_exceptions import HTTPMovedPermanently
|
from aiohttp.web_exceptions import HTTPMovedPermanently
|
||||||
@ -16,7 +17,6 @@ import voluptuous as vol
|
|||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, SERVER_PORT)
|
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, SERVER_PORT)
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
import homeassistant.remote as rem
|
|
||||||
import homeassistant.util as hass_util
|
import homeassistant.util as hass_util
|
||||||
from homeassistant.util.logging import HideSensitiveDataFilter
|
from homeassistant.util.logging import HideSensitiveDataFilter
|
||||||
from homeassistant.util import ssl as ssl_util
|
from homeassistant.util import ssl as ssl_util
|
||||||
@ -82,6 +82,28 @@ CONFIG_SCHEMA = vol.Schema({
|
|||||||
}, extra=vol.ALLOW_EXTRA)
|
}, 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):
|
async def async_setup(hass, config):
|
||||||
"""Set up the HTTP API and debug interface."""
|
"""Set up the HTTP API and debug interface."""
|
||||||
conf = config.get(DOMAIN)
|
conf = config.get(DOMAIN)
|
||||||
@ -146,8 +168,8 @@ async def async_setup(hass, config):
|
|||||||
host = hass_util.get_local_ip()
|
host = hass_util.get_local_ip()
|
||||||
port = server_port
|
port = server_port
|
||||||
|
|
||||||
hass.config.api = rem.API(host, api_password, port,
|
hass.config.api = ApiConfig(host, port, ssl_certificate is not None,
|
||||||
ssl_certificate is not None)
|
api_password)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -1145,8 +1145,8 @@ class Config:
|
|||||||
# List of loaded components
|
# List of loaded components
|
||||||
self.components = set() # type: set
|
self.components = set() # type: set
|
||||||
|
|
||||||
# Remote.API object pointing at local API
|
# API (HTTP) server configuration
|
||||||
self.api = None
|
self.api = None # type: Optional[Any]
|
||||||
|
|
||||||
# Directory that holds the configuration
|
# Directory that holds the configuration
|
||||||
self.config_dir = None # type: Optional[str]
|
self.config_dir = None # type: Optional[str]
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
"""The tests for the Home Assistant HTTP component."""
|
"""The tests for the Home Assistant HTTP component."""
|
||||||
import logging
|
import logging
|
||||||
|
import unittest
|
||||||
|
|
||||||
from homeassistant.setup import async_setup_component
|
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)
|
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):
|
async def test_api_base_url_with_domain(hass):
|
||||||
"""Test setting API URL."""
|
"""Test setting API URL."""
|
||||||
result = await async_setup_component(hass, 'http', {
|
result = await async_setup_component(hass, 'http', {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user