diff --git a/homeassistant/core.py b/homeassistant/core.py index 27906e0401f3..b8d159893fe2 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -89,7 +89,7 @@ from .util.unit_system import IMPERIAL_SYSTEM, METRIC_SYSTEM, UnitSystem # Typing imports that create a circular dependency if TYPE_CHECKING: from .auth import AuthManager - from .components.http import HomeAssistantHTTP + from .components.http import ApiConfig, HomeAssistantHTTP from .config_entries import ConfigEntries @@ -1701,8 +1701,8 @@ class Config: # List of loaded components self.components: set[str] = set() - # API (HTTP) server configuration, see components.http.ApiConfig - self.api: Any | None = None + # API (HTTP) server configuration + self.api: ApiConfig | None = None # Directory that holds the configuration self.config_dir: str | None = None diff --git a/homeassistant/helpers/network.py b/homeassistant/helpers/network.py index 0b2804f821df..0c52012e43cb 100644 --- a/homeassistant/helpers/network.py +++ b/homeassistant/helpers/network.py @@ -41,14 +41,20 @@ def get_url( allow_internal: bool = True, allow_external: bool = True, allow_cloud: bool = True, - allow_ip: bool = True, - prefer_external: bool = False, + allow_ip: bool | None = None, + prefer_external: bool | None = None, prefer_cloud: bool = False, ) -> str: """Get a URL to this instance.""" if require_current_request and http.current_request.get() is None: raise NoURLAvailableError + if prefer_external is None: + prefer_external = hass.config.api is not None and hass.config.api.use_ssl + + if allow_ip is None: + allow_ip = hass.config.api is None or not hass.config.api.use_ssl + order = [TYPE_URL_INTERNAL, TYPE_URL_EXTERNAL] if prefer_external: order.reverse() diff --git a/tests/helpers/test_network.py b/tests/helpers/test_network.py index 05c72f10db56..7e9086f44677 100644 --- a/tests/helpers/test_network.py +++ b/tests/helpers/test_network.py @@ -480,6 +480,12 @@ async def test_get_url(hass: HomeAssistant): get_url(hass, prefer_external=True, allow_external=False) == "http://example.local" ) + # Prefer external defaults to True if use_ssl=True + hass.config.api = Mock(use_ssl=True) + assert get_url(hass) == "https://example.com" + hass.config.api = Mock(use_ssl=False) + assert get_url(hass) == "http://example.local" + hass.config.api = None with pytest.raises(NoURLAvailableError): get_url(hass, allow_external=False, require_ssl=True) @@ -519,6 +525,19 @@ async def test_get_url(hass: HomeAssistant): ), pytest.raises(NoURLAvailableError): _get_internal_url(hass, require_current_request=True) + # Test allow_ip defaults when SSL specified + await async_process_ha_core_config( + hass, + {"external_url": "https://1.1.1.1"}, + ) + assert hass.config.external_url == "https://1.1.1.1" + assert get_url(hass, allow_internal=False) == "https://1.1.1.1" + hass.config.api = Mock(use_ssl=False) + assert get_url(hass, allow_internal=False) == "https://1.1.1.1" + hass.config.api = Mock(use_ssl=True) + with pytest.raises(NoURLAvailableError): + assert get_url(hass, allow_internal=False) + async def test_get_request_host(hass: HomeAssistant): """Test getting the host of the current web request from the request context."""