mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 23:21:54 +00:00 
			
		
		
		
	Save flash and RAM by conditionally compiling unused API password code
This commit is contained in:
		| @@ -132,7 +132,9 @@ async def to_code(config): | |||||||
|     await cg.register_component(var, config) |     await cg.register_component(var, config) | ||||||
|  |  | ||||||
|     cg.add(var.set_port(config[CONF_PORT])) |     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_reboot_timeout(config[CONF_REBOOT_TIMEOUT])) | ||||||
|     cg.add(var.set_batch_delay(config[CONF_BATCH_DELAY])) |     cg.add(var.set_batch_delay(config[CONF_BATCH_DELAY])) | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1503,7 +1503,10 @@ HelloResponse APIConnection::hello(const HelloRequest &msg) { | |||||||
|   return resp; |   return resp; | ||||||
| } | } | ||||||
| ConnectResponse APIConnection::connect(const ConnectRequest &msg) { | 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; |   ConnectResponse resp; | ||||||
|   // bool invalid_password = 1; |   // bool invalid_password = 1; | ||||||
| @@ -1524,7 +1527,11 @@ ConnectResponse APIConnection::connect(const ConnectRequest &msg) { | |||||||
| } | } | ||||||
| DeviceInfoResponse APIConnection::device_info(const DeviceInfoRequest &msg) { | DeviceInfoResponse APIConnection::device_info(const DeviceInfoRequest &msg) { | ||||||
|   DeviceInfoResponse resp{}; |   DeviceInfoResponse resp{}; | ||||||
|  | #ifdef USE_API_PASSWORD | ||||||
|   resp.uses_password = this->parent_->uses_password(); |   resp.uses_password = this->parent_->uses_password(); | ||||||
|  | #else | ||||||
|  |   resp.uses_password = false; | ||||||
|  | #endif | ||||||
|   resp.name = App.get_name(); |   resp.name = App.get_name(); | ||||||
|   resp.friendly_name = App.get_friendly_name(); |   resp.friendly_name = App.get_friendly_name(); | ||||||
|   resp.suggested_area = App.get_area(); |   resp.suggested_area = App.get_area(); | ||||||
|   | |||||||
| @@ -218,6 +218,7 @@ void APIServer::dump_config() { | |||||||
| #endif | #endif | ||||||
| } | } | ||||||
|  |  | ||||||
|  | #ifdef USE_API_PASSWORD | ||||||
| bool APIServer::uses_password() const { return !this->password_.empty(); } | bool APIServer::uses_password() const { return !this->password_.empty(); } | ||||||
|  |  | ||||||
| bool APIServer::check_password(const std::string &password) const { | 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; |   return result == 0; | ||||||
| } | } | ||||||
|  | #endif | ||||||
|  |  | ||||||
| void APIServer::handle_disconnect(APIConnection *conn) {} | 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; } | 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; } | 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; } | void APIServer::set_batch_delay(uint16_t batch_delay) { this->batch_delay_ = batch_delay; } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -35,10 +35,12 @@ class APIServer : public Component, public Controller { | |||||||
|   void dump_config() override; |   void dump_config() override; | ||||||
|   void on_shutdown() override; |   void on_shutdown() override; | ||||||
|   bool teardown() override; |   bool teardown() override; | ||||||
|  | #ifdef USE_API_PASSWORD | ||||||
|   bool check_password(const std::string &password) const; |   bool check_password(const std::string &password) const; | ||||||
|   bool uses_password() const; |   bool uses_password() const; | ||||||
|   void set_port(uint16_t port); |  | ||||||
|   void set_password(const std::string &password); |   void set_password(const std::string &password); | ||||||
|  | #endif | ||||||
|  |   void set_port(uint16_t port); | ||||||
|   void set_reboot_timeout(uint32_t reboot_timeout); |   void set_reboot_timeout(uint32_t reboot_timeout); | ||||||
|   void set_batch_delay(uint16_t batch_delay); |   void set_batch_delay(uint16_t batch_delay); | ||||||
|   uint16_t get_batch_delay() const { return 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) |   // Vectors and strings (12 bytes each on 32-bit) | ||||||
|   std::vector<std::unique_ptr<APIConnection>> clients_; |   std::vector<std::unique_ptr<APIConnection>> clients_; | ||||||
|  | #ifdef USE_API_PASSWORD | ||||||
|   std::string password_; |   std::string password_; | ||||||
|  | #endif | ||||||
|   std::vector<uint8_t> shared_write_buffer_;  // Shared proto write buffer for all connections |   std::vector<uint8_t> shared_write_buffer_;  // Shared proto write buffer for all connections | ||||||
|   std::vector<HomeAssistantStateSubscription> state_subs_; |   std::vector<HomeAssistantStateSubscription> state_subs_; | ||||||
| #ifdef USE_API_YAML_SERVICES | #ifdef USE_API_YAML_SERVICES | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user