From 304a44df4d71998671fdee0fad45c47e5f97b7d6 Mon Sep 17 00:00:00 2001 From: kbx81 Date: Sun, 14 Sep 2025 01:25:08 -0500 Subject: [PATCH] Consolidate subscribe/unsubscribe messages --- esphome/components/api/api.proto | 17 ++++------ esphome/components/api/api_connection.cpp | 8 ++--- esphome/components/api/api_connection.h | 3 +- esphome/components/api/api_pb2.cpp | 4 +-- esphome/components/api/api_pb2.h | 27 ++++++---------- esphome/components/api/api_pb2_dump.cpp | 18 +++++++++-- esphome/components/api/api_pb2_service.cpp | 30 ++++------------- esphome/components/api/api_pb2_service.h | 15 ++------- .../components/zwave_proxy/zwave_proxy.cpp | 32 +++++++++++-------- esphome/components/zwave_proxy/zwave_proxy.h | 3 +- 10 files changed, 67 insertions(+), 90 deletions(-) diff --git a/esphome/components/api/api.proto b/esphome/components/api/api.proto index 51db6dea9c..27a9cbf44a 100644 --- a/esphome/components/api/api.proto +++ b/esphome/components/api/api.proto @@ -71,8 +71,7 @@ service APIConnection { rpc alarm_control_panel_command (AlarmControlPanelCommandRequest) returns (void) {} rpc zwave_proxy_frame(ZWaveProxyFrame) returns (void) {} - rpc zwave_proxy_subscribe(ZWaveProxySubscribeRequest) returns (void) {} - rpc zwave_proxy_unsubscribe(ZWaveProxyUnsubscribeRequest) returns (void) {} + rpc zwave_proxy_request(ZWaveProxyRequest) returns (void) {} } @@ -2296,16 +2295,14 @@ message ZWaveProxyFrame { bytes data = 1 [(fixed_array_size) = 257]; } -message ZWaveProxySubscribeRequest { +enum ZWaveProxyRequestType { + ZWAVE_PROXY_REQUEST_TYPE_SUBSCRIBE = 0; + ZWAVE_PROXY_REQUEST_TYPE_UNSUBSCRIBE = 1; +} +message ZWaveProxyRequest { option (id) = 129; option (source) = SOURCE_CLIENT; option (ifdef) = "USE_ZWAVE_PROXY"; - uint32 flags = 1; -} - -message ZWaveProxyUnsubscribeRequest { - option (id) = 130; - option (source) = SOURCE_CLIENT; - option (ifdef) = "USE_ZWAVE_PROXY"; + ZWaveProxyRequestType type = 1; } diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 283d18dfb8..04d8116364 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -1217,12 +1217,8 @@ void APIConnection::zwave_proxy_frame(const ZWaveProxyFrame &msg) { zwave_proxy::global_zwave_proxy->send_frame(msg.data, msg.data_len); } -void APIConnection::zwave_proxy_subscribe(const ZWaveProxySubscribeRequest &msg) { - zwave_proxy::global_zwave_proxy->subscribe_api_connection(this, msg.flags); -} - -void APIConnection::zwave_proxy_unsubscribe(const ZWaveProxyUnsubscribeRequest &msg) { - zwave_proxy::global_zwave_proxy->unsubscribe_api_connection(this); +void APIConnection::zwave_proxy_request(const ZWaveProxyRequest &msg) { + zwave_proxy::global_zwave_proxy->zwave_proxy_request(this, msg.type); } #endif diff --git a/esphome/components/api/api_connection.h b/esphome/components/api/api_connection.h index 0a2c5e375a..a4752346f3 100644 --- a/esphome/components/api/api_connection.h +++ b/esphome/components/api/api_connection.h @@ -173,8 +173,7 @@ class APIConnection final : public APIServerConnection { #ifdef USE_ZWAVE_PROXY void zwave_proxy_frame(const ZWaveProxyFrame &msg) override; - void zwave_proxy_subscribe(const ZWaveProxySubscribeRequest &msg) override; - void zwave_proxy_unsubscribe(const ZWaveProxyUnsubscribeRequest &msg) override; + void zwave_proxy_request(const ZWaveProxyRequest &msg) override; #endif #ifdef USE_ALARM_CONTROL_PANEL diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index 86677779f3..5267e08837 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -3044,10 +3044,10 @@ bool ZWaveProxyFrame::decode_length(uint32_t field_id, ProtoLengthDelimited valu } void ZWaveProxyFrame::encode(ProtoWriteBuffer buffer) const { buffer.encode_bytes(1, this->data, this->data_len); } void ZWaveProxyFrame::calculate_size(ProtoSize &size) const { size.add_length(1, this->data_len); } -bool ZWaveProxySubscribeRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { +bool ZWaveProxyRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { case 1: - this->flags = value.as_uint32(); + this->type = static_cast(value.as_uint32()); break; default: return false; diff --git a/esphome/components/api/api_pb2.h b/esphome/components/api/api_pb2.h index 0bce0b5813..ae7e5af314 100644 --- a/esphome/components/api/api_pb2.h +++ b/esphome/components/api/api_pb2.h @@ -276,6 +276,12 @@ enum UpdateCommand : uint32_t { UPDATE_COMMAND_CHECK = 2, }; #endif +#ifdef USE_ZWAVE_PROXY +enum ZWaveProxyRequestType : uint32_t { + ZWAVE_PROXY_REQUEST_TYPE_SUBSCRIBE = 0, + ZWAVE_PROXY_REQUEST_TYPE_UNSUBSCRIBE = 1, +}; +#endif } // namespace enums @@ -2937,14 +2943,14 @@ class ZWaveProxyFrame final : public ProtoDecodableMessage { protected: bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override; }; -class ZWaveProxySubscribeRequest final : public ProtoDecodableMessage { +class ZWaveProxyRequest final : public ProtoDecodableMessage { public: static constexpr uint8_t MESSAGE_TYPE = 129; - static constexpr uint8_t ESTIMATED_SIZE = 4; + static constexpr uint8_t ESTIMATED_SIZE = 2; #ifdef HAS_PROTO_MESSAGE_DUMP - const char *message_name() const override { return "z_wave_proxy_subscribe_request"; } + const char *message_name() const override { return "z_wave_proxy_request"; } #endif - uint32_t flags{0}; + enums::ZWaveProxyRequestType type{}; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2952,19 +2958,6 @@ class ZWaveProxySubscribeRequest final : public ProtoDecodableMessage { protected: bool decode_varint(uint32_t field_id, ProtoVarInt value) override; }; -class ZWaveProxyUnsubscribeRequest final : public ProtoMessage { - public: - static constexpr uint8_t MESSAGE_TYPE = 130; - static constexpr uint8_t ESTIMATED_SIZE = 0; -#ifdef HAS_PROTO_MESSAGE_DUMP - const char *message_name() const override { return "z_wave_proxy_unsubscribe_request"; } -#endif -#ifdef HAS_PROTO_MESSAGE_DUMP - void dump_to(std::string &out) const override; -#endif - - protected: -}; #endif } // namespace esphome::api diff --git a/esphome/components/api/api_pb2_dump.cpp b/esphome/components/api/api_pb2_dump.cpp index 572aed576b..7b9f10bc0d 100644 --- a/esphome/components/api/api_pb2_dump.cpp +++ b/esphome/components/api/api_pb2_dump.cpp @@ -655,6 +655,18 @@ template<> const char *proto_enum_to_string(enums::UpdateC } } #endif +#ifdef USE_ZWAVE_PROXY +template<> const char *proto_enum_to_string(enums::ZWaveProxyRequestType value) { + switch (value) { + case enums::ZWAVE_PROXY_REQUEST_TYPE_SUBSCRIBE: + return "ZWAVE_PROXY_REQUEST_TYPE_SUBSCRIBE"; + case enums::ZWAVE_PROXY_REQUEST_TYPE_UNSUBSCRIBE: + return "ZWAVE_PROXY_REQUEST_TYPE_UNSUBSCRIBE"; + default: + return "UNKNOWN"; + } +} +#endif void HelloRequest::dump_to(std::string &out) const { MessageDumpHelper helper(out, "HelloRequest"); @@ -2118,8 +2130,10 @@ void ZWaveProxyFrame::dump_to(std::string &out) const { out.append(format_hex_pretty(this->data, this->data_len)); out.append("\n"); } -void ZWaveProxySubscribeRequest::dump_to(std::string &out) const { dump_field(out, "flags", this->flags); } -void ZWaveProxyUnsubscribeRequest::dump_to(std::string &out) const { out.append("ZWaveProxyUnsubscribeRequest {}"); } +void ZWaveProxyRequest::dump_to(std::string &out) const { + MessageDumpHelper helper(out, "ZWaveProxyRequest"); + dump_field(out, "type", static_cast(this->type)); +} #endif } // namespace esphome::api diff --git a/esphome/components/api/api_pb2_service.cpp b/esphome/components/api/api_pb2_service.cpp index 20b68d524e..506f9fe805 100644 --- a/esphome/components/api/api_pb2_service.cpp +++ b/esphome/components/api/api_pb2_service.cpp @@ -608,24 +608,13 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type, } #endif #ifdef USE_ZWAVE_PROXY - case ZWaveProxySubscribeRequest::MESSAGE_TYPE: { - ZWaveProxySubscribeRequest msg; + case ZWaveProxyRequest::MESSAGE_TYPE: { + ZWaveProxyRequest msg; msg.decode(msg_data, msg_size); #ifdef HAS_PROTO_MESSAGE_DUMP - ESP_LOGVV(TAG, "on_z_wave_proxy_subscribe_request: %s", msg.dump().c_str()); + ESP_LOGVV(TAG, "on_z_wave_proxy_request: %s", msg.dump().c_str()); #endif - this->on_z_wave_proxy_subscribe_request(msg); - break; - } -#endif -#ifdef USE_ZWAVE_PROXY - case ZWaveProxyUnsubscribeRequest::MESSAGE_TYPE: { - ZWaveProxyUnsubscribeRequest msg; - // Empty message: no decode needed -#ifdef HAS_PROTO_MESSAGE_DUMP - ESP_LOGVV(TAG, "on_z_wave_proxy_unsubscribe_request: %s", msg.dump().c_str()); -#endif - this->on_z_wave_proxy_unsubscribe_request(msg); + this->on_z_wave_proxy_request(msg); break; } #endif @@ -950,16 +939,9 @@ void APIServerConnection::on_z_wave_proxy_frame(const ZWaveProxyFrame &msg) { } #endif #ifdef USE_ZWAVE_PROXY -void APIServerConnection::on_z_wave_proxy_subscribe_request(const ZWaveProxySubscribeRequest &msg) { +void APIServerConnection::on_z_wave_proxy_request(const ZWaveProxyRequest &msg) { if (this->check_authenticated_()) { - this->zwave_proxy_subscribe(msg); - } -} -#endif -#ifdef USE_ZWAVE_PROXY -void APIServerConnection::on_z_wave_proxy_unsubscribe_request(const ZWaveProxyUnsubscribeRequest &msg) { - if (this->check_authenticated_()) { - this->zwave_proxy_unsubscribe(msg); + this->zwave_proxy_request(msg); } } #endif diff --git a/esphome/components/api/api_pb2_service.h b/esphome/components/api/api_pb2_service.h index a2c77103ee..dd65b31c05 100644 --- a/esphome/components/api/api_pb2_service.h +++ b/esphome/components/api/api_pb2_service.h @@ -210,10 +210,7 @@ class APIServerConnectionBase : public ProtoService { virtual void on_z_wave_proxy_frame(const ZWaveProxyFrame &value){}; #endif #ifdef USE_ZWAVE_PROXY - virtual void on_z_wave_proxy_subscribe_request(const ZWaveProxySubscribeRequest &value){}; -#endif -#ifdef USE_ZWAVE_PROXY - virtual void on_z_wave_proxy_unsubscribe_request(const ZWaveProxyUnsubscribeRequest &value){}; + virtual void on_z_wave_proxy_request(const ZWaveProxyRequest &value){}; #endif protected: void read_message(uint32_t msg_size, uint32_t msg_type, uint8_t *msg_data) override; @@ -346,10 +343,7 @@ class APIServerConnection : public APIServerConnectionBase { virtual void zwave_proxy_frame(const ZWaveProxyFrame &msg) = 0; #endif #ifdef USE_ZWAVE_PROXY - virtual void zwave_proxy_subscribe(const ZWaveProxySubscribeRequest &msg) = 0; -#endif -#ifdef USE_ZWAVE_PROXY - virtual void zwave_proxy_unsubscribe(const ZWaveProxyUnsubscribeRequest &msg) = 0; + virtual void zwave_proxy_request(const ZWaveProxyRequest &msg) = 0; #endif protected: void on_hello_request(const HelloRequest &msg) override; @@ -477,10 +471,7 @@ class APIServerConnection : public APIServerConnectionBase { void on_z_wave_proxy_frame(const ZWaveProxyFrame &msg) override; #endif #ifdef USE_ZWAVE_PROXY - void on_z_wave_proxy_subscribe_request(const ZWaveProxySubscribeRequest &msg) override; -#endif -#ifdef USE_ZWAVE_PROXY - void on_z_wave_proxy_unsubscribe_request(const ZWaveProxyUnsubscribeRequest &msg) override; + void on_z_wave_proxy_request(const ZWaveProxyRequest &msg) override; #endif }; diff --git a/esphome/components/zwave_proxy/zwave_proxy.cpp b/esphome/components/zwave_proxy/zwave_proxy.cpp index 1498b827bf..c75b73a7f4 100644 --- a/esphome/components/zwave_proxy/zwave_proxy.cpp +++ b/esphome/components/zwave_proxy/zwave_proxy.cpp @@ -45,20 +45,26 @@ void ZWaveProxy::loop() { void ZWaveProxy::dump_config() { ESP_LOGCONFIG(TAG, "Z-Wave Proxy"); } -void ZWaveProxy::subscribe_api_connection(api::APIConnection *api_connection, uint32_t flags) { - if (this->api_connection_ != nullptr) { - ESP_LOGE(TAG, "Only one API subscription is allowed at a time"); - return; +void ZWaveProxy::zwave_proxy_request(api::APIConnection *api_connection, api::enums::ZWaveProxyRequestType type) { + switch (type) { + case api::enums::ZWAVE_PROXY_REQUEST_TYPE_SUBSCRIBE: + if (this->api_connection_ != nullptr) { + ESP_LOGE(TAG, "Only one API subscription is allowed at a time"); + return; + } + this->api_connection_ = api_connection; + ESP_LOGV(TAG, "API connection is now subscribed"); + break; + case api::enums::ZWAVE_PROXY_REQUEST_TYPE_UNSUBSCRIBE: + if (this->api_connection_ != api_connection) { + ESP_LOGV(TAG, "API connection is not subscribed"); + return; + } + this->api_connection_ = nullptr; + default: + ESP_LOGW(TAG, "Unknown request type: %d", type); + break; } - this->api_connection_ = api_connection; -} - -void ZWaveProxy::unsubscribe_api_connection(api::APIConnection *api_connection) { - if (this->api_connection_ != api_connection) { - ESP_LOGV(TAG, "API connection is not subscribed"); - return; - } - this->api_connection_ = nullptr; } void ZWaveProxy::send_frame(const uint8_t *data, size_t length) { diff --git a/esphome/components/zwave_proxy/zwave_proxy.h b/esphome/components/zwave_proxy/zwave_proxy.h index d6ee6b99bb..a0f25849e4 100644 --- a/esphome/components/zwave_proxy/zwave_proxy.h +++ b/esphome/components/zwave_proxy/zwave_proxy.h @@ -41,8 +41,7 @@ class ZWaveProxy : public uart::UARTDevice, public Component { void loop() override; void dump_config() override; - void subscribe_api_connection(api::APIConnection *api_connection, uint32_t flags); - void unsubscribe_api_connection(api::APIConnection *api_connection); + void zwave_proxy_request(api::APIConnection *api_connection, api::enums::ZWaveProxyRequestType type); api::APIConnection *get_api_connection() { return this->api_connection_; } uint32_t get_feature_flags() const { return ZWaveProxyFeature::FEATURE_ZWAVE_PROXY_ENABLED; }