diff --git a/esphome/components/api/api.proto b/esphome/components/api/api.proto index dca722dca5..9d43e22497 100644 --- a/esphome/components/api/api.proto +++ b/esphome/components/api/api.proto @@ -95,6 +95,9 @@ message HelloResponse { // and only exists for debugging/logging purposes. // For example "ESPHome v1.10.0 on ESP8266" string server_info = 3; + + // The name of the server (App.get_name()) + string name = 4; } // Message sent at the beginning of each connection to authenticate the client diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 1f629c2c85..20011c0954 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -766,6 +766,8 @@ HelloResponse APIConnection::hello(const HelloRequest &msg) { resp.api_version_major = 1; resp.api_version_minor = 6; resp.server_info = App.get_name() + " (esphome v" ESPHOME_VERSION ")"; + resp.name = App.get_name(); + this->connection_state_ = ConnectionState::CONNECTED; return resp; } diff --git a/esphome/components/api/api_frame_helper.cpp b/esphome/components/api/api_frame_helper.cpp index d9eadb2aaa..9f0ab82a52 100644 --- a/esphome/components/api/api_frame_helper.cpp +++ b/esphome/components/api/api_frame_helper.cpp @@ -3,6 +3,7 @@ #include "esphome/core/log.h" #include "esphome/core/hal.h" #include "esphome/core/helpers.h" +#include "esphome/core/application.h" #include "proto.h" #include @@ -302,9 +303,16 @@ APIError APINoiseFrameHelper::state_action_() { } if (state_ == State::SERVER_HELLO) { // send server hello - uint8_t msg[1]; - msg[0] = 0x01; // chosen proto - aerr = write_frame_(msg, 1); + std::vector msg; + // chosen proto + msg.push_back(0x01); + + // node name, terminated by null byte + const std::string &name = App.get_name(); + const uint8_t *name_ptr = reinterpret_cast(name.c_str()); + msg.insert(msg.end(), name_ptr, name_ptr + name.size() + 1); + + aerr = write_frame_(msg.data(), msg.size()); if (aerr != APIError::OK) return aerr; diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index 5b6853c276..4ecd727f29 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -319,6 +319,10 @@ bool HelloResponse::decode_length(uint32_t field_id, ProtoLengthDelimited value) this->server_info = value.as_string(); return true; } + case 4: { + this->name = value.as_string(); + return true; + } default: return false; } @@ -327,6 +331,7 @@ void HelloResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(1, this->api_version_major); buffer.encode_uint32(2, this->api_version_minor); buffer.encode_string(3, this->server_info); + buffer.encode_string(4, this->name); } #ifdef HAS_PROTO_MESSAGE_DUMP void HelloResponse::dump_to(std::string &out) const { @@ -345,6 +350,10 @@ void HelloResponse::dump_to(std::string &out) const { out.append(" server_info: "); out.append("'").append(this->server_info).append("'"); out.append("\n"); + + out.append(" name: "); + out.append("'").append(this->name).append("'"); + out.append("\n"); out.append("}"); } #endif diff --git a/esphome/components/api/api_pb2.h b/esphome/components/api/api_pb2.h index e92b2fa4b6..48ecb5f682 100644 --- a/esphome/components/api/api_pb2.h +++ b/esphome/components/api/api_pb2.h @@ -147,6 +147,7 @@ class HelloResponse : public ProtoMessage { uint32_t api_version_major{0}; uint32_t api_version_minor{0}; std::string server_info{}; + std::string name{}; void encode(ProtoWriteBuffer buffer) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override;