diff --git a/esphome/components/api/api.proto b/esphome/components/api/api.proto index c3795bb796..8865812146 100644 --- a/esphome/components/api/api.proto +++ b/esphome/components/api/api.proto @@ -136,6 +136,7 @@ message ConnectRequest { option (id) = 3; option (source) = SOURCE_CLIENT; option (no_delay) = true; + option (ifdef) = "USE_API_PASSWORD"; // The password to log in with string password = 1; @@ -147,6 +148,7 @@ message ConnectResponse { option (id) = 4; option (source) = SOURCE_SERVER; option (no_delay) = true; + option (ifdef) = "USE_API_PASSWORD"; bool invalid_password = 1; } diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 537d75467f..c4e515a102 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -1436,11 +1436,10 @@ HelloResponse APIConnection::hello(const HelloRequest &msg) { this->flags_.connection_state = static_cast(ConnectionState::CONNECTED); return resp; } +#ifdef USE_API_PASSWORD ConnectResponse APIConnection::connect(const ConnectRequest &msg) { bool correct = true; -#ifdef USE_API_PASSWORD correct = this->parent_->check_password(msg.password); -#endif ConnectResponse resp; // bool invalid_password = 1; @@ -1459,6 +1458,7 @@ ConnectResponse APIConnection::connect(const ConnectRequest &msg) { } return resp; } +#endif DeviceInfoResponse APIConnection::device_info(const DeviceInfoRequest &msg) { DeviceInfoResponse resp{}; #ifdef USE_API_PASSWORD diff --git a/esphome/components/api/api_connection.h b/esphome/components/api/api_connection.h index b70b037999..91bbef742c 100644 --- a/esphome/components/api/api_connection.h +++ b/esphome/components/api/api_connection.h @@ -173,7 +173,9 @@ class APIConnection : public APIServerConnection { void on_get_time_response(const GetTimeResponse &value) override; #endif HelloResponse hello(const HelloRequest &msg) override; +#ifdef USE_API_PASSWORD ConnectResponse connect(const ConnectRequest &msg) override; +#endif DisconnectResponse disconnect(const DisconnectRequest &msg) override; PingResponse ping(const PingRequest &msg) override { return {}; } DeviceInfoResponse device_info(const DeviceInfoRequest &msg) override; diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index 3505ec758d..8968edeab9 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -82,6 +82,7 @@ void HelloResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->server_info, false); ProtoSize::add_string_field(total_size, 1, this->name, false); } +#ifdef USE_API_PASSWORD bool ConnectRequest::decode_length(uint32_t field_id, ProtoLengthDelimited value) { switch (field_id) { case 1: { @@ -110,6 +111,7 @@ void ConnectResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool void ConnectResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_bool_field(total_size, 1, this->invalid_password, false); } +#endif bool AreaInfo::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { case 1: { diff --git a/esphome/components/api/api_pb2.h b/esphome/components/api/api_pb2.h index 3bfc5f1cf4..540c0331ee 100644 --- a/esphome/components/api/api_pb2.h +++ b/esphome/components/api/api_pb2.h @@ -348,6 +348,7 @@ class HelloResponse : public ProtoMessage { bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override; bool decode_varint(uint32_t field_id, ProtoVarInt value) override; }; +#ifdef USE_API_PASSWORD class ConnectRequest : public ProtoMessage { public: static constexpr uint16_t MESSAGE_TYPE = 3; @@ -382,6 +383,7 @@ class ConnectResponse : public ProtoMessage { protected: bool decode_varint(uint32_t field_id, ProtoVarInt value) override; }; +#endif class DisconnectRequest : public ProtoMessage { public: static constexpr uint16_t MESSAGE_TYPE = 5; diff --git a/esphome/components/api/api_pb2_dump.cpp b/esphome/components/api/api_pb2_dump.cpp index 48ddd42d61..9d190928df 100644 --- a/esphome/components/api/api_pb2_dump.cpp +++ b/esphome/components/api/api_pb2_dump.cpp @@ -632,6 +632,7 @@ void HelloResponse::dump_to(std::string &out) const { out.append("\n"); out.append("}"); } +#ifdef USE_API_PASSWORD void ConnectRequest::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ConnectRequest {\n"); @@ -648,6 +649,7 @@ void ConnectResponse::dump_to(std::string &out) const { out.append("\n"); out.append("}"); } +#endif void DisconnectRequest::dump_to(std::string &out) const { out.append("DisconnectRequest {}"); } void DisconnectResponse::dump_to(std::string &out) const { out.append("DisconnectResponse {}"); } void PingRequest::dump_to(std::string &out) const { out.append("PingRequest {}"); } diff --git a/esphome/components/api/api_pb2_service.cpp b/esphome/components/api/api_pb2_service.cpp index 92dd90053b..23b442d31b 100644 --- a/esphome/components/api/api_pb2_service.cpp +++ b/esphome/components/api/api_pb2_service.cpp @@ -25,6 +25,7 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type, this->on_hello_request(msg); break; } +#ifdef USE_API_PASSWORD case 3: { ConnectRequest msg; msg.decode(msg_data, msg_size); @@ -34,6 +35,7 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type, this->on_connect_request(msg); break; } +#endif case 5: { DisconnectRequest msg; msg.decode(msg_data, msg_size); @@ -600,12 +602,14 @@ void APIServerConnection::on_hello_request(const HelloRequest &msg) { this->on_fatal_error(); } } +#ifdef USE_API_PASSWORD void APIServerConnection::on_connect_request(const ConnectRequest &msg) { ConnectResponse ret = this->connect(msg); if (!this->send_message(ret)) { this->on_fatal_error(); } } +#endif void APIServerConnection::on_disconnect_request(const DisconnectRequest &msg) { DisconnectResponse ret = this->disconnect(msg); if (!this->send_message(ret)) { diff --git a/esphome/components/api/api_pb2_service.h b/esphome/components/api/api_pb2_service.h index 458f8ec81b..66921caac3 100644 --- a/esphome/components/api/api_pb2_service.h +++ b/esphome/components/api/api_pb2_service.h @@ -27,7 +27,9 @@ class APIServerConnectionBase : public ProtoService { virtual void on_hello_request(const HelloRequest &value){}; +#ifdef USE_API_PASSWORD virtual void on_connect_request(const ConnectRequest &value){}; +#endif virtual void on_disconnect_request(const DisconnectRequest &value){}; virtual void on_disconnect_response(const DisconnectResponse &value){}; @@ -206,7 +208,9 @@ class APIServerConnectionBase : public ProtoService { class APIServerConnection : public APIServerConnectionBase { public: virtual HelloResponse hello(const HelloRequest &msg) = 0; +#ifdef USE_API_PASSWORD virtual ConnectResponse connect(const ConnectRequest &msg) = 0; +#endif virtual DisconnectResponse disconnect(const DisconnectRequest &msg) = 0; virtual PingResponse ping(const PingRequest &msg) = 0; virtual DeviceInfoResponse device_info(const DeviceInfoRequest &msg) = 0; @@ -323,7 +327,9 @@ class APIServerConnection : public APIServerConnectionBase { #endif protected: void on_hello_request(const HelloRequest &msg) override; +#ifdef USE_API_PASSWORD void on_connect_request(const ConnectRequest &msg) override; +#endif void on_disconnect_request(const DisconnectRequest &msg) override; void on_ping_request(const PingRequest &msg) override; void on_device_info_request(const DeviceInfoRequest &msg) override;