From afc5c29569b0793ba8b3c51b53c881612d805399 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 24 Sep 2026 14:06:29 +0100 Subject: [PATCH] [event] Trigger events without building a temporary std::string (#19567) --- esphome/components/event/__init__.py | 9 ++++++++- esphome/components/event/event.cpp | 6 +++--- esphome/components/event/event.h | 4 +++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/esphome/components/event/__init__.py b/esphome/components/event/__init__.py index f6c3094724..d02863b9ee 100644 --- a/esphome/components/event/__init__.py +++ b/esphome/components/event/__init__.py @@ -133,10 +133,17 @@ TRIGGER_EVENT_SCHEMA = cv.Schema( ) +def _event_type_literal(config: ConfigType, value: str) -> str: + """A constant event type is a plain literal; trigger() only compares it, so no copy is needed.""" + return str(cg.safe_exp(value)) + + automation.register_apply_action( "event.trigger", TRIGGER_EVENT_SCHEMA, - automation.ApplyField(CONF_EVENT_TYPE, "trigger", cg.std_string), + automation.ApplyField( + CONF_EVENT_TYPE, "trigger", cg.std_string, _event_type_literal + ), ) diff --git a/esphome/components/event/event.cpp b/esphome/components/event/event.cpp index 673ccc9802..d4ac2644e3 100644 --- a/esphome/components/event/event.cpp +++ b/esphome/components/event/event.cpp @@ -7,17 +7,17 @@ namespace esphome::event { static const char *const TAG = "event"; -void Event::trigger(const std::string &event_type) { +void Event::trigger(const char *event_type) { // Linear search with strcmp - faster than std::set for small datasets (1-5 items typical) const char *found = nullptr; for (const char *type : this->types_) { - if (strcmp(type, event_type.c_str()) == 0) { + if (strcmp(type, event_type) == 0) { found = type; break; } } if (found == nullptr) { - ESP_LOGE(TAG, "'%s': invalid event type for trigger(): %s", this->get_name().c_str(), event_type.c_str()); + ESP_LOGE(TAG, "'%s': invalid event type for trigger(): %s", this->get_name().c_str(), event_type); return; } this->last_event_type_ = found; diff --git a/esphome/components/event/event.h b/esphome/components/event/event.h index e6fc7111c8..374365c6fb 100644 --- a/esphome/components/event/event.h +++ b/esphome/components/event/event.h @@ -21,7 +21,9 @@ namespace esphome::event { class Event : public EntityBase { public: - void trigger(const std::string &event_type); + /// Trigger an event; the type is matched against the configured types by string compare. + void trigger(const char *event_type); + void trigger(const std::string &event_type) { this->trigger(event_type.c_str()); } /// Set the event types supported by this event (from initializer list). void set_event_types(std::initializer_list event_types) {