diff --git a/esphome/core/component.cpp b/esphome/core/component.cpp index aba5dc729c..9ef30081aa 100644 --- a/esphome/core/component.cpp +++ b/esphome/core/component.cpp @@ -248,6 +248,9 @@ bool Component::cancel_defer(const std::string &name) { // NOLINT void Component::defer(const std::string &name, std::function &&f) { // NOLINT App.scheduler.set_timeout(this, name, 0, std::move(f)); } +void Component::defer(const char *name, std::function &&f) { // NOLINT + App.scheduler.set_timeout(this, name, 0, std::move(f)); +} void Component::set_timeout(uint32_t timeout, std::function &&f) { // NOLINT App.scheduler.set_timeout(this, "", timeout, std::move(f)); } diff --git a/esphome/core/component.h b/esphome/core/component.h index ab30466e2d..3734473a02 100644 --- a/esphome/core/component.h +++ b/esphome/core/component.h @@ -380,6 +380,21 @@ class Component { */ void defer(const std::string &name, std::function &&f); // NOLINT + /** Defer a callback to the next loop() call with a const char* name. + * + * IMPORTANT: The provided name pointer must remain valid for the lifetime of the deferred task. + * This means the name should be: + * - A string literal (e.g., "update") + * - A static const char* variable + * - A pointer with lifetime >= the deferred execution + * + * For dynamic strings, use the std::string overload instead. + * + * @param name The name of the defer function (must have static lifetime) + * @param f The callback + */ + void defer(const char *name, std::function &&f); // NOLINT + /// Defer a callback to the next loop() call. void defer(std::function &&f); // NOLINT