[api] Skip the listen backlog setter when it matches the default (#20066)

This commit is contained in:
J. Nick Koston
2026-10-05 12:13:41 +13:00
committed by GitHub
parent e09cab94d4
commit 31e0a31c65
6 changed files with 33 additions and 6 deletions
+5 -2
View File
@@ -142,6 +142,7 @@ CONF_STATE_SUBSCRIPTION_ONLY = "state_subscription_only"
DEFAULT_PORT = 6053
DEFAULT_REBOOT_TIMEOUT = "15min"
DEFAULT_BATCH_DELAY = "100ms"
DEFAULT_LISTEN_BACKLOG = 4
def _register_provisioning_source(config: ConfigType) -> ConfigType:
@@ -477,8 +478,10 @@ async def to_code(config: ConfigType) -> None:
cg.add(var.set_reboot_timeout(reboot_timeout))
if (batch_delay := config[CONF_BATCH_DELAY]) != cv.time_period(DEFAULT_BATCH_DELAY):
cg.add(var.set_batch_delay(batch_delay))
if CONF_LISTEN_BACKLOG in config:
cg.add(var.set_listen_backlog(config[CONF_LISTEN_BACKLOG]))
if (
listen_backlog := config.get(CONF_LISTEN_BACKLOG)
) is not None and listen_backlog != DEFAULT_LISTEN_BACKLOG:
cg.add(var.set_listen_backlog(listen_backlog))
cg.add_define("MAX_API_CONNECTIONS", config[CONF_MAX_CONNECTIONS])
cg.add_define("API_MAX_SEND_QUEUE", config[CONF_MAX_SEND_QUEUE])
+1 -3
View File
@@ -357,9 +357,7 @@ class APIServer final : public Component
// Group smaller types together
uint16_t port_{6053}; // Keep in sync with DEFAULT_PORT in __init__.py
uint16_t batch_delay_{100}; // Keep in sync with DEFAULT_BATCH_DELAY in __init__.py
// Connection limits - these defaults will be overridden by config values
// from cv.SplitDefault in __init__.py which sets platform-specific defaults.
uint8_t listen_backlog_{4};
uint8_t listen_backlog_{4}; // Keep in sync with DEFAULT_LISTEN_BACKLOG in __init__.py
bool shutting_down_ = false;
uint8_t api_connection_count_{0};
#if defined(USE_PROVISIONING) && defined(USE_API_NOISE)
@@ -13,3 +13,4 @@ api:
port: 6054
reboot_timeout: 0s
batch_delay: 0ms
listen_backlog: 2
@@ -13,3 +13,4 @@ api:
port: 6053
reboot_timeout: 15min
batch_delay: 100ms
listen_backlog: 4
@@ -0,0 +1,12 @@
---
esphome:
name: test
esp8266:
board: d1_mini
wifi:
ssid: test
password: testtest
api:
@@ -12,7 +12,7 @@ def test_default_values_are_not_emitted(
component_config_path: Callable[[str], Path],
config_file: str,
) -> None:
"""Port 6053, a 15 min reboot timeout and 100 ms batch delay are C++ initializers.
"""Port 6053, a 15 min reboot timeout, 100 ms batch delay and backlog 4 are C++ initializers.
Both the schema defaults and the same values written explicitly take the skip path.
"""
@@ -21,6 +21,7 @@ def test_default_values_are_not_emitted(
assert "api_apiserver_id->set_port(" not in main_cpp
assert "api_apiserver_id->set_reboot_timeout(" not in main_cpp
assert "api_apiserver_id->set_batch_delay(" not in main_cpp
assert "api_apiserver_id->set_listen_backlog(" not in main_cpp
def test_custom_values_are_emitted(
@@ -33,3 +34,14 @@ def test_custom_values_are_emitted(
assert "api_apiserver_id->set_port(6054);" in main_cpp
assert "api_apiserver_id->set_reboot_timeout(0);" in main_cpp
assert "api_apiserver_id->set_batch_delay(0);" in main_cpp
assert "api_apiserver_id->set_listen_backlog(2);" in main_cpp
def test_esp8266_listen_backlog_is_emitted(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
"""The ESP8266 schema default of 1 differs from the C++ initializer, so it is set."""
main_cpp = generate_main(component_config_path("esp8266.yaml"))
assert "api_apiserver_id->set_listen_backlog(1);" in main_cpp