diff --git a/esphome/components/api/api.proto b/esphome/components/api/api.proto index f6eb8ec1c9..e115e4630d 100644 --- a/esphome/components/api/api.proto +++ b/esphome/components/api/api.proto @@ -2147,7 +2147,7 @@ message ListEntitiesEventResponse { EntityCategory entity_category = 7; string device_class = 8; - repeated string event_types = 9 [(container_pointer_no_template) = "std::vector"]; + repeated string event_types = 9 [(container_pointer_no_template) = "FixedVector"]; uint32 device_id = 10 [(field_ifdef) = "USE_DEVICES"]; } message EventResponse { diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 7f8438af25..8c293b41a2 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -1310,8 +1310,7 @@ uint16_t APIConnection::try_send_event_info(EntityBase *entity, APIConnection *c auto *event = static_cast(entity); ListEntitiesEventResponse msg; msg.set_device_class(event->get_device_class_ref()); - for (const char *event_type : event->get_event_types()) - msg.event_types->push_back(event_type); + msg.event_types = &event->get_event_types(); return fill_and_encode_entity_info(event, msg, ListEntitiesEventResponse::MESSAGE_TYPE, conn, remaining_size, is_single); } diff --git a/esphome/components/api/api_pb2.h b/esphome/components/api/api_pb2.h index df793eb262..358049026e 100644 --- a/esphome/components/api/api_pb2.h +++ b/esphome/components/api/api_pb2.h @@ -2788,7 +2788,7 @@ class ListEntitiesEventResponse final : public InfoResponseProtoMessage { #endif StringRef device_class_ref_{}; void set_device_class(const StringRef &ref) { this->device_class_ref_ = ref; } - const std::vector *event_types{}; + const FixedVector *event_types{}; void encode(ProtoWriteBuffer buffer) const override; void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP diff --git a/esphome/components/event/event.cpp b/esphome/components/event/event.cpp index 6b79fa8c11..a14afbd7f5 100644 --- a/esphome/components/event/event.cpp +++ b/esphome/components/event/event.cpp @@ -25,6 +25,22 @@ void Event::trigger(const std::string &event_type) { this->event_callback_.call(event_type); } +void Event::set_event_types(const FixedVector &event_types) { + this->types_.init(event_types.size()); + for (const char *type : event_types) { + this->types_.push_back(type); + } + this->last_event_type_ = nullptr; // Reset when types change +} + +void Event::set_event_types(const std::vector &event_types) { + this->types_.init(event_types.size()); + for (const char *type : event_types) { + this->types_.push_back(type); + } + this->last_event_type_ = nullptr; // Reset when types change +} + void Event::add_on_event_callback(std::function &&callback) { this->event_callback_.add(std::move(callback)); } diff --git a/esphome/components/event/event.h b/esphome/components/event/event.h index efa4c14464..e4b2e0b845 100644 --- a/esphome/components/event/event.h +++ b/esphome/components/event/event.h @@ -31,18 +31,18 @@ class Event : public EntityBase, public EntityBase_DeviceClass { this->types_ = event_types; this->last_event_type_ = nullptr; // Reset when types change } + /// Set the event types supported by this event (from FixedVector). + void set_event_types(const FixedVector &event_types); /// Set the event types supported by this event (from vector). - void set_event_types(const std::vector &event_types) { - this->types_ = event_types; - this->last_event_type_ = nullptr; // Reset when types change - } + void set_event_types(const std::vector &event_types); // Deleted overloads to catch incorrect std::string usage at compile time with clear error messages void set_event_types(std::initializer_list event_types) = delete; + void set_event_types(const FixedVector &event_types) = delete; void set_event_types(const std::vector &event_types) = delete; /// Return the event types supported by this event. - const std::vector &get_event_types() const { return this->types_; } + const FixedVector &get_event_types() const { return this->types_; } /// Return the last triggered event type (pointer to string in types_), or nullptr if no event triggered yet. const char *get_last_event_type() const { return this->last_event_type_; } @@ -51,7 +51,7 @@ class Event : public EntityBase, public EntityBase_DeviceClass { protected: CallbackManager event_callback_; - std::vector types_; + FixedVector types_; private: /// Last triggered event type - must point to entry in types_ to ensure valid lifetime.