From 17173ba3a31fc2f3adb936c14206d53bad9b57da Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 29 Apr 2026 10:15:30 -0500 Subject: [PATCH] [scheduler] Make get_name() return SELF_POINTER slot too So debug-only iteration logs in Scheduler::call() (the periodic items dump and the per-item Running line) print the actual self pointer instead of 'self:(nil)'. Both STATIC_STRING and SELF_POINTER use the same pointer union member, so a single accessor is the natural shape; drop the unused get_self() helper that was added for this and update the union member comments to mention SELF_POINTER and NUMERIC_ID_INTERNAL. --- esphome/core/scheduler.h | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/esphome/core/scheduler.h b/esphome/core/scheduler.h index 671665f4c0..a97f45b15f 100644 --- a/esphome/core/scheduler.h +++ b/esphome/core/scheduler.h @@ -180,8 +180,8 @@ class Scheduler { Component *component; // Optimized name storage using tagged union - zero heap allocation union { - const char *static_name; // For STATIC_STRING (string literals, no allocation) - uint32_t hash_or_id; // For HASHED_STRING or NUMERIC_ID + const char *static_name; // For STATIC_STRING (string literals) and SELF_POINTER (caller's `this`) + uint32_t hash_or_id; // For HASHED_STRING, NUMERIC_ID, and NUMERIC_ID_INTERNAL } name_; uint32_t interval; // Split time to handle millis() rollover. The scheduler combines the 32-bit millis() @@ -248,20 +248,18 @@ class Scheduler { SchedulerItem(SchedulerItem &&) = delete; SchedulerItem &operator=(SchedulerItem &&) = delete; - // Helper to get the static name (only valid for STATIC_STRING type) - const char *get_name() const { return (name_type_ == NameType::STATIC_STRING) ? name_.static_name : nullptr; } + // Helper to get the pointer-slot value (valid for STATIC_STRING and SELF_POINTER types). + // Both share the same union member, so callers (e.g. log formatters) can read either uniformly. + const char *get_name() const { + return (name_type_ == NameType::STATIC_STRING || name_type_ == NameType::SELF_POINTER) ? name_.static_name + : nullptr; + } // Helper to get the hash or numeric ID (only valid for HASHED_STRING / NUMERIC_ID / NUMERIC_ID_INTERNAL types) uint32_t get_name_hash_or_id() const { return (name_type_ != NameType::STATIC_STRING && name_type_ != NameType::SELF_POINTER) ? name_.hash_or_id : 0; } - // Helper to get the self pointer (only valid for SELF_POINTER type). - // The pointer is stored in the same union slot as `static_name` since both are pointer-width. - const void *get_self() const { - return (name_type_ == NameType::SELF_POINTER) ? static_cast(name_.static_name) : nullptr; - } - // Helper to get the name type NameType get_name_type() const { return name_type_; }