ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
preferences.cpp
Go to the documentation of this file.
2#include "esphome/core/log.h"
3#include <cinttypes>
4
5namespace esphome {
6
7#ifdef USE_PREFERENCE_KEY_LOOKUP
8ESPHOME_LOG_TAG(TAG, "preferences");
9#endif
10
11bool migrate_preference(ESPPreferenceObject &new_pref, uint8_t *scratch, size_t size, [[maybe_unused]] uint32_t old_key,
12 [[maybe_unused]] uint32_t new_key) {
13 if (new_pref.load(scratch, size))
14 return true; // Current data present - never overwrite newer data with the old copy
15#ifdef USE_PREFERENCE_KEY_LOOKUP
16 // One-shot read by key: no backend is allocated for the old key, so boots with
17 // nothing to migrate (for example fresh installs) cost no heap
18 if (old_key == new_key || !global_preferences->load_from_key(old_key, scratch, size))
19 return false; // No data stored under the old key, nothing to migrate
20 if (!new_pref.save(scratch, size)) {
21 ESP_LOGW(TAG, "Pref migration %" PRIx32 " -> %" PRIx32 " failed", old_key, new_key);
22 }
23 return true;
24#else
25 return false;
26#endif
27}
28
29} // namespace esphome
ESPHOME_LOG_TAG(TAG, CRASH_LOG_TAG)
uint16_t size
Definition helpers.cpp:44
ESPPreferences * global_preferences
bool migrate_preference(ESPPreferenceObject &new_pref, uint8_t *scratch, size_t size, uint32_t old_key, uint32_t new_key)
Copy preference data stored under old_key into new_pref (created for new_key) if the keys differ and ...
static void uint32_t
watchdog_hw scratch[0]
bool load_from_key(uint32_t, uint8_t *, size_t)
Definition preferences.h:26