From fca80d81c8ed534d0cf03b5cccb37ec325e300a0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 7 Nov 2025 11:30:34 -0600 Subject: [PATCH] [event] Store event types in flash memory --- esphome/components/event/event.cpp | 4 ++-- esphome/components/event/event.h | 21 +++++++++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/esphome/components/event/event.cpp b/esphome/components/event/event.cpp index ccb221d441..6b79fa8c11 100644 --- a/esphome/components/event/event.cpp +++ b/esphome/components/event/event.cpp @@ -20,8 +20,8 @@ void Event::trigger(const std::string &event_type) { ESP_LOGE(TAG, "'%s': invalid event type for trigger(): %s", this->get_name().c_str(), event_type.c_str()); return; } - this->last_event_type = found; - ESP_LOGD(TAG, "'%s' Triggered event '%s'", this->get_name().c_str(), this->last_event_type); + this->last_event_type_ = found; + ESP_LOGD(TAG, "'%s' Triggered event '%s'", this->get_name().c_str(), this->last_event_type_); this->event_callback_.call(event_type); } diff --git a/esphome/components/event/event.h b/esphome/components/event/event.h index 5219288d2a..74709dcac1 100644 --- a/esphome/components/event/event.h +++ b/esphome/components/event/event.h @@ -23,17 +23,22 @@ namespace event { class Event : public EntityBase, public EntityBase_DeviceClass { public: - const char *last_event_type{nullptr}; - void trigger(const std::string &event_type); /// Set the event types supported by this event (from initializer list). - void set_event_types(std::initializer_list event_types) { this->types_ = event_types; } + void set_event_types(std::initializer_list event_types) { + 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) { this->types_ = event_types; } + void set_event_types(const FixedVector &event_types) { + this->types_ = event_types; + this->last_event_type_ = nullptr; // Reset when types change + } /// Set the event types supported by this event (from C array). template void set_event_types(const char *const (&event_types)[N]) { this->types_.assign(event_types, event_types + N); + this->last_event_type_ = nullptr; // Reset when types change } // Deleted overloads to catch incorrect std::string usage at compile time with clear error messages @@ -43,11 +48,19 @@ class Event : public EntityBase, public EntityBase_DeviceClass { /// Return the event types supported by this event. 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_; } + void add_on_event_callback(std::function &&callback); protected: CallbackManager event_callback_; FixedVector types_; + + private: + /// Last triggered event type - must point to entry in types_ to ensure valid lifetime. + /// Set by trigger() after validation, reset to nullptr when types_ changes. + const char *last_event_type_{nullptr}; }; } // namespace event