From 5b55e205efb9ddf25dc69a28ca3f56a73ed9fe31 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 2 Jul 2025 16:42:08 -0500 Subject: [PATCH] Save flash and RAM by conditionally compiling unused API password code (#9297) --- esphome/components/api/__init__.py | 4 +++- esphome/components/api/api_connection.cpp | 9 ++++++++- esphome/components/api/api_server.cpp | 4 ++++ esphome/components/api/api_server.h | 6 +++++- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/esphome/components/api/__init__.py b/esphome/components/api/__init__.py index b02a875d72..2f1be28293 100644 --- a/esphome/components/api/__init__.py +++ b/esphome/components/api/__init__.py @@ -132,7 +132,9 @@ async def to_code(config): await cg.register_component(var, config) cg.add(var.set_port(config[CONF_PORT])) - cg.add(var.set_password(config[CONF_PASSWORD])) + if config[CONF_PASSWORD]: + cg.add_define("USE_API_PASSWORD") + cg.add(var.set_password(config[CONF_PASSWORD])) cg.add(var.set_reboot_timeout(config[CONF_REBOOT_TIMEOUT])) cg.add(var.set_batch_delay(config[CONF_BATCH_DELAY])) diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index e83d508c50..49ad9706bc 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -1503,7 +1503,10 @@ HelloResponse APIConnection::hello(const HelloRequest &msg) { return resp; } ConnectResponse APIConnection::connect(const ConnectRequest &msg) { - bool correct = this->parent_->check_password(msg.password); + bool correct = true; +#ifdef USE_API_PASSWORD + correct = this->parent_->check_password(msg.password); +#endif ConnectResponse resp; // bool invalid_password = 1; @@ -1524,7 +1527,11 @@ ConnectResponse APIConnection::connect(const ConnectRequest &msg) { } DeviceInfoResponse APIConnection::device_info(const DeviceInfoRequest &msg) { DeviceInfoResponse resp{}; +#ifdef USE_API_PASSWORD resp.uses_password = this->parent_->uses_password(); +#else + resp.uses_password = false; +#endif resp.name = App.get_name(); resp.friendly_name = App.get_friendly_name(); resp.suggested_area = App.get_area(); diff --git a/esphome/components/api/api_server.cpp b/esphome/components/api/api_server.cpp index ebe80604dc..0fd9c1a228 100644 --- a/esphome/components/api/api_server.cpp +++ b/esphome/components/api/api_server.cpp @@ -218,6 +218,7 @@ void APIServer::dump_config() { #endif } +#ifdef USE_API_PASSWORD bool APIServer::uses_password() const { return !this->password_.empty(); } bool APIServer::check_password(const std::string &password) const { @@ -248,6 +249,7 @@ bool APIServer::check_password(const std::string &password) const { return result == 0; } +#endif void APIServer::handle_disconnect(APIConnection *conn) {} @@ -431,7 +433,9 @@ float APIServer::get_setup_priority() const { return setup_priority::AFTER_WIFI; void APIServer::set_port(uint16_t port) { this->port_ = port; } +#ifdef USE_API_PASSWORD void APIServer::set_password(const std::string &password) { this->password_ = password; } +#endif void APIServer::set_batch_delay(uint16_t batch_delay) { this->batch_delay_ = batch_delay; } diff --git a/esphome/components/api/api_server.h b/esphome/components/api/api_server.h index 5a9b0677bc..9dc2b4b7d6 100644 --- a/esphome/components/api/api_server.h +++ b/esphome/components/api/api_server.h @@ -35,10 +35,12 @@ class APIServer : public Component, public Controller { void dump_config() override; void on_shutdown() override; bool teardown() override; +#ifdef USE_API_PASSWORD bool check_password(const std::string &password) const; bool uses_password() const; - void set_port(uint16_t port); void set_password(const std::string &password); +#endif + void set_port(uint16_t port); void set_reboot_timeout(uint32_t reboot_timeout); void set_batch_delay(uint16_t batch_delay); uint16_t get_batch_delay() const { return batch_delay_; } @@ -179,7 +181,9 @@ class APIServer : public Component, public Controller { // Vectors and strings (12 bytes each on 32-bit) std::vector> clients_; +#ifdef USE_API_PASSWORD std::string password_; +#endif std::vector shared_write_buffer_; // Shared proto write buffer for all connections std::vector state_subs_; #ifdef USE_API_YAML_SERVICES