diff --git a/esphome/components/template/text/template_text.cpp b/esphome/components/template/text/template_text.cpp index 5acbb6e15a..ac2ed7e60d 100644 --- a/esphome/components/template/text/template_text.cpp +++ b/esphome/components/template/text/template_text.cpp @@ -20,6 +20,10 @@ void TemplateText::setup() { // Need std::string for pref_->setup() to fill from flash std::string value{this->initial_value_ != nullptr ? this->initial_value_ : ""}; + // For future hash migration: use migrate_entity_preference_() with: + // old_key = get_preference_hash() + extra + // new_key = get_preference_hash_v2() + extra + // See: https://github.com/esphome/backlog/issues/85 uint32_t key = this->get_preference_hash(); key += this->traits.get_min_length() << 2; key += this->traits.get_max_length() << 4; diff --git a/esphome/core/entity_base.cpp b/esphome/core/entity_base.cpp index 07df13008d..ebc3990fb9 100644 --- a/esphome/core/entity_base.cpp +++ b/esphome/core/entity_base.cpp @@ -92,6 +92,23 @@ StringRef EntityBase::get_object_id_to(std::span buf) c uint32_t EntityBase::get_object_id_hash() { return this->object_id_hash_; } +// Migrate preference data from old_key to new_key if they differ. +// This helper is exposed so callers with custom key computation (like TextPrefs) +// can use it for manual migration. See: https://github.com/esphome/backlog/issues/85 +// +// FUTURE IMPLEMENTATION: +// This will require raw load/save methods on ESPPreferenceObject that take uint8_t* and size. +// void EntityBase::migrate_entity_preference_(size_t size, uint32_t old_key, uint32_t new_key) { +// if (old_key == new_key) +// return; +// auto old_pref = global_preferences->make_preference(size, old_key); +// auto new_pref = global_preferences->make_preference(size, new_key); +// SmallBufferWithHeapFallback<64> buffer(size); +// if (old_pref.load(buffer.data(), size)) { +// new_pref.save(buffer.data(), size); +// } +// } + ESPPreferenceObject EntityBase::make_entity_preference_(size_t size, uint32_t version) { // This helper centralizes preference creation to enable fixing hash collisions. // See: https://github.com/esphome/backlog/issues/85 @@ -103,22 +120,13 @@ ESPPreferenceObject EntityBase::make_entity_preference_(size_t size, uint32_t ve // This causes entities to overwrite each other's stored preferences. // // FUTURE MIGRATION: When implementing get_preference_hash_v2() that hashes - // the original entity name (not sanitized object_id), migration logic goes here: + // the original entity name (not sanitized object_id): // // uint32_t old_key = this->get_preference_hash() ^ version; // uint32_t new_key = this->get_preference_hash_v2() ^ version; - // if (old_key != new_key) { - // auto old_pref = global_preferences->make_preference(size, old_key); - // auto new_pref = global_preferences->make_preference(size, new_key); - // SmallBufferWithHeapFallback<64> buffer(size); - // if (old_pref.load(buffer.data(), size)) { - // new_pref.save(buffer.data(), size); - // } - // } + // this->migrate_entity_preference_(size, old_key, new_key); // return global_preferences->make_preference(size, new_key); // - // This will require raw load/save methods on ESPPreferenceObject (uint8_t*, size). - // uint32_t key = this->get_preference_hash() ^ version; return global_preferences->make_preference(size, key); }