diff --git a/esphome/components/event/__init__.py b/esphome/components/event/__init__.py index e2b69ba872..8fac7a279c 100644 --- a/esphome/components/event/__init__.py +++ b/esphome/components/event/__init__.py @@ -90,9 +90,7 @@ async def setup_event_core_(var, config, *, event_types: list[str]): for conf in config.get(CONF_ON_EVENT, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) - await automation.build_automation( - trigger, [(cg.std_string, "event_type")], conf - ) + await automation.build_automation(trigger, [(cg.StringRef, "event_type")], conf) cg.add(var.set_event_types(event_types)) diff --git a/esphome/components/event/automation.h b/esphome/components/event/automation.h index 5bdba18687..7730506c10 100644 --- a/esphome/components/event/automation.h +++ b/esphome/components/event/automation.h @@ -14,10 +14,10 @@ template class TriggerEventAction : public Action, public void play(const Ts &...x) override { this->parent_->trigger(this->event_type_.value(x...)); } }; -class EventTrigger : public Trigger { +class EventTrigger : public Trigger { public: EventTrigger(Event *event) { - event->add_on_event_callback([this](const std::string &event_type) { this->trigger(event_type); }); + event->add_on_event_callback([this](StringRef event_type) { this->trigger(event_type); }); } }; diff --git a/esphome/components/event/event.cpp b/esphome/components/event/event.cpp index 8015f2255a..667d4218f3 100644 --- a/esphome/components/event/event.cpp +++ b/esphome/components/event/event.cpp @@ -23,7 +23,7 @@ void Event::trigger(const std::string &event_type) { } this->last_event_type_ = found; ESP_LOGD(TAG, "'%s' >> '%s'", this->get_name().c_str(), this->last_event_type_); - this->event_callback_.call(event_type); + this->event_callback_.call(StringRef(found)); #if defined(USE_EVENT) && defined(USE_CONTROLLER_REGISTRY) ControllerRegistry::notify_event(this); #endif @@ -45,7 +45,7 @@ void Event::set_event_types(const std::vector &event_types) { this->last_event_type_ = nullptr; // Reset when types change } -void Event::add_on_event_callback(std::function &&callback) { +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 f77ad326d9..b5519a0520 100644 --- a/esphome/components/event/event.h +++ b/esphome/components/event/event.h @@ -70,10 +70,10 @@ class Event : public EntityBase, public EntityBase_DeviceClass { /// Check if an event has been triggered. bool has_event() const { return this->last_event_type_ != nullptr; } - void add_on_event_callback(std::function &&callback); + void add_on_event_callback(std::function &&callback); protected: - LazyCallbackManager event_callback_; + LazyCallbackManager event_callback_; FixedVector types_; private: diff --git a/esphome/components/template/select/__init__.py b/esphome/components/template/select/__init__.py index 0e9c240547..574f1f5fb7 100644 --- a/esphome/components/template/select/__init__.py +++ b/esphome/components/template/select/__init__.py @@ -88,5 +88,5 @@ async def to_code(config): if CONF_SET_ACTION in config: await automation.build_automation( - var.get_set_trigger(), [(cg.std_string, "x")], config[CONF_SET_ACTION] + var.get_set_trigger(), [(cg.StringRef, "x")], config[CONF_SET_ACTION] ) diff --git a/esphome/components/template/select/template_select.cpp b/esphome/components/template/select/template_select.cpp index 9d2df0956b..818abfc1d7 100644 --- a/esphome/components/template/select/template_select.cpp +++ b/esphome/components/template/select/template_select.cpp @@ -41,7 +41,7 @@ void TemplateSelect::update() { } void TemplateSelect::control(size_t index) { - this->set_trigger_->trigger(std::string(this->option_at(index))); + this->set_trigger_->trigger(StringRef(this->option_at(index))); if (this->optimistic_) this->publish_state(index); diff --git a/esphome/components/template/select/template_select.h b/esphome/components/template/select/template_select.h index 2757c51405..114d25b9ce 100644 --- a/esphome/components/template/select/template_select.h +++ b/esphome/components/template/select/template_select.h @@ -4,6 +4,7 @@ #include "esphome/core/automation.h" #include "esphome/core/component.h" #include "esphome/core/preferences.h" +#include "esphome/core/string_ref.h" #include "esphome/core/template_lambda.h" namespace esphome::template_ { @@ -17,7 +18,7 @@ class TemplateSelect final : public select::Select, public PollingComponent { void dump_config() override; float get_setup_priority() const override { return setup_priority::HARDWARE; } - Trigger *get_set_trigger() const { return this->set_trigger_; } + Trigger *get_set_trigger() const { return this->set_trigger_; } void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; } void set_initial_option_index(size_t initial_option_index) { this->initial_option_index_ = initial_option_index; } void set_restore_value(bool restore_value) { this->restore_value_ = restore_value; } @@ -27,7 +28,7 @@ class TemplateSelect final : public select::Select, public PollingComponent { bool optimistic_ = false; size_t initial_option_index_{0}; bool restore_value_ = false; - Trigger *set_trigger_ = new Trigger(); + Trigger *set_trigger_ = new Trigger(); TemplateLambda f_; ESPPreferenceObject pref_;