diff --git a/esphome/components/api/__init__.py b/esphome/components/api/__init__.py index 25ebc93458..764a3d0f1e 100644 --- a/esphome/components/api/__init__.py +++ b/esphome/components/api/__init__.py @@ -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]) diff --git a/esphome/components/api/api_server.h b/esphome/components/api/api_server.h index 865cd17156..491ab38342 100644 --- a/esphome/components/api/api_server.h +++ b/esphome/components/api/api_server.h @@ -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) diff --git a/tests/component_tests/api/config/custom.yaml b/tests/component_tests/api/config/custom.yaml index cdf4038d5d..476a46ebff 100644 --- a/tests/component_tests/api/config/custom.yaml +++ b/tests/component_tests/api/config/custom.yaml @@ -13,3 +13,4 @@ api: port: 6054 reboot_timeout: 0s batch_delay: 0ms + listen_backlog: 2 diff --git a/tests/component_tests/api/config/defaults.yaml b/tests/component_tests/api/config/defaults.yaml index b20fd9b884..49d7b0abcc 100644 --- a/tests/component_tests/api/config/defaults.yaml +++ b/tests/component_tests/api/config/defaults.yaml @@ -13,3 +13,4 @@ api: port: 6053 reboot_timeout: 15min batch_delay: 100ms + listen_backlog: 4 diff --git a/tests/component_tests/api/config/esp8266.yaml b/tests/component_tests/api/config/esp8266.yaml new file mode 100644 index 0000000000..0ba6ccbd33 --- /dev/null +++ b/tests/component_tests/api/config/esp8266.yaml @@ -0,0 +1,12 @@ +--- +esphome: + name: test + +esp8266: + board: d1_mini + +wifi: + ssid: test + password: testtest + +api: diff --git a/tests/component_tests/api/test_default_setters.py b/tests/component_tests/api/test_default_setters.py index 32d35cacb7..2c7f843c68 100644 --- a/tests/component_tests/api/test_default_setters.py +++ b/tests/component_tests/api/test_default_setters.py @@ -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