Compare commits

...

2 Commits

Author SHA1 Message Date
Erik
fa704cbb30 Tweak, add tests 2025-10-28 19:54:27 +01:00
Erik
6582744aa2 Ignore http.server_host in HassOS or supervised 2025-10-27 18:57:26 +01:00
4 changed files with 64 additions and 15 deletions

View File

@@ -14,11 +14,7 @@ import aiohttp
from yarl import URL
from homeassistant.auth.models import RefreshToken
from homeassistant.components.http import (
CONF_SERVER_HOST,
CONF_SERVER_PORT,
CONF_SSL_CERTIFICATE,
)
from homeassistant.components.http import CONF_SERVER_PORT, CONF_SSL_CERTIFICATE
from homeassistant.const import SERVER_PORT
from homeassistant.core import HomeAssistant
from homeassistant.helpers.singleton import singleton
@@ -238,13 +234,6 @@ class HassIO:
"refresh_token": refresh_token.token,
}
if http_config.get(CONF_SERVER_HOST) is not None:
options["watchdog"] = False
_LOGGER.warning(
"Found incompatible HTTP option 'server_host'. Watchdog feature"
" disabled"
)
return await self.send_command("/homeassistant/options", payload=options)
@_api_bool

View File

@@ -38,6 +38,7 @@ from homeassistant.const import (
from homeassistant.core import Event, HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv, issue_registry as ir, storage
from homeassistant.helpers.hassio import is_hassio
from homeassistant.helpers.http import (
KEY_ALLOW_CONFIGURED_CORS,
KEY_AUTHENTICATED, # noqa: F401
@@ -207,7 +208,15 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
if conf is None:
conf = cast(ConfData, HTTP_SCHEMA({}))
server_host = conf[CONF_SERVER_HOST]
if is_hassio(hass):
server_host = _DEFAULT_BIND
if conf[CONF_SERVER_HOST] != _DEFAULT_BIND:
_LOGGER.warning(
"The 'http.server_host' option is ignored when running in "
"Home Assistant OS or Supervised"
)
else:
server_host = conf.get(CONF_SERVER_HOST, _DEFAULT_BIND)
server_port = conf[CONF_SERVER_PORT]
ssl_certificate = conf.get(CONF_SSL_CERTIFICATE)
ssl_peer_certificate = conf.get(CONF_SSL_PEER_CERTIFICATE)

View File

@@ -303,7 +303,7 @@ async def test_setup_api_push_api_data_server_host(
assert aioclient_mock.call_count + len(supervisor_client.mock_calls) == 18
assert not aioclient_mock.mock_calls[0][2]["ssl"]
assert aioclient_mock.mock_calls[0][2]["port"] == 9999
assert not aioclient_mock.mock_calls[0][2]["watchdog"]
assert "watchdog" not in aioclient_mock.mock_calls[0][2]
async def test_setup_api_push_api_data_default(

View File

@@ -7,7 +7,7 @@ from http import HTTPStatus
from ipaddress import ip_network
import logging
from pathlib import Path
from unittest.mock import Mock, patch
from unittest.mock import ANY, Mock, patch
import pytest
@@ -667,3 +667,54 @@ async def test_ssl_issue_urls_configured(
"http",
"ssl_configured_without_configured_urls",
) not in issue_registry.issues
@pytest.mark.parametrize(
("hassio", "http_config", "expected_serverhost", "expected_warning_count"),
[
(False, {}, ["0.0.0.0", "::"], 0),
(False, {"server_host": "0.0.0.0"}, ["0.0.0.0"], 0),
(True, {}, ["0.0.0.0", "::"], 0),
(True, {"server_host": "0.0.0.0"}, ["0.0.0.0", "::"], 1),
],
)
async def test_server_host(
hass: HomeAssistant,
hassio: bool,
http_config: dict,
expected_serverhost: list,
expected_warning_count: int,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test server_host default and override behavior."""
mock_server = Mock()
with (
patch("homeassistant.components.http.is_hassio", return_value=hassio),
patch(
"asyncio.BaseEventLoop.create_server", return_value=mock_server
) as mock_create_server,
):
assert await async_setup_component(
hass,
"http",
{"http": http_config},
)
await hass.async_start()
await hass.async_block_till_done()
mock_create_server.assert_called_once_with(
ANY,
expected_serverhost,
8123,
ssl=None,
backlog=128,
reuse_address=None,
reuse_port=None,
)
assert (
caplog.text.count(
"The 'http.server_host' option is ignored when running in Home Assistant OS or Supervised"
)
== expected_warning_count
)