From d8dd8cf36c853ccaddfcd10cbd7b9c591b7dc453 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 12 Oct 2026 16:57:20 -1000 Subject: [PATCH] Keep the flash object id hash in entity_base and read it with progmem_read_byte --- esphome/core/entity_base.cpp | 14 +++++++++++++- esphome/core/helpers.h | 12 ------------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/esphome/core/entity_base.cpp b/esphome/core/entity_base.cpp index 7bf55c0354..45d11e68a3 100644 --- a/esphome/core/entity_base.cpp +++ b/esphome/core/entity_base.cpp @@ -1,5 +1,6 @@ #include "esphome/core/entity_base.h" #include "esphome/core/application.h" +#include "esphome/core/hal.h" #include "esphome/core/helpers.h" #include "esphome/core/log.h" #include "esphome/core/progmem.h" @@ -13,6 +14,17 @@ ESPHOME_LOG_TAG(TAG, "entity_base"); #ifdef USE_ESP8266 namespace { +// fnv1_hash_object_id() for a name in flash +uint32_t fnv1_hash_object_id_progmem(const char *str, size_t len) { + uint32_t hash = FNV1_OFFSET_BASIS; + for (size_t i = 0; i < len; i++) { + hash *= FNV1_PRIME; + char c = static_cast(progmem_read_byte(reinterpret_cast(str + i))); + hash ^= static_cast(to_sanitized_char(to_snake_case_char(c))); + } + return hash; +} + // RAM copies made by the deprecated get_name(), only for entities whose name is asked for. Remove before 2027.5.0 struct NameCopy { NameCopy *next; @@ -71,7 +83,7 @@ void EntityBase::configure_entity_(const char *name, uint32_t object_id_hash, ui // Static name - codegen precomputes the hash; external register_* callers may pass 0 if (object_id_hash == 0) { #ifdef USE_ESP8266 - object_id_hash = fnv1_hash_object_id_P(this->name_.progmem_ptr(), this->name_.size()); + object_id_hash = fnv1_hash_object_id_progmem(this->name_.progmem_ptr(), this->name_.size()); #else object_id_hash = fnv1_hash_object_id(this->name_.progmem_ptr(), this->name_.size()); #endif diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 2d6cd1a96d..bcb03b010f 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -1145,18 +1145,6 @@ inline uint32_t fnv1_hash_object_id(const char *str, size_t len) { return hash; } -#ifdef USE_ESP8266 -/// fnv1_hash_object_id() for a string in flash (PROGMEM). -inline uint32_t fnv1_hash_object_id_P(PGM_P str, size_t len) { - uint32_t hash = FNV1_OFFSET_BASIS; - for (size_t i = 0; i < len; i++) { - hash *= FNV1_PRIME; - hash ^= static_cast(to_sanitized_char(to_snake_case_char(static_cast(pgm_read_byte(str + i))))); - } - return hash; -} -#endif - #ifdef USE_ESP8266 // ESP8266: Use vsnprintf_P to keep format strings in flash (PROGMEM) // Format strings must be wrapped with PSTR() macro