[preferences] Compile out loop() when flash_write_interval is non-zero (#14943)

This commit is contained in:
J. Nick Koston
2026-03-19 14:11:06 -10:00
committed by GitHub
parent c2a96ea293
commit 902258b56e
3 changed files with 13 additions and 11 deletions
+5 -1
View File
@@ -21,5 +21,9 @@ CONFIG_SCHEMA = cv.Schema(
@coroutine_with_priority(CoroPriority.PREFERENCES)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
cg.add(var.set_write_interval(config[CONF_FLASH_WRITE_INTERVAL]))
write_interval = config[CONF_FLASH_WRITE_INTERVAL]
if write_interval.total_milliseconds == 0:
cg.add_define("USE_PREFERENCES_SYNC_EVERY_LOOP")
else:
cg.add(var.set_write_interval(write_interval))
await cg.register_component(var, config)
+7 -10
View File
@@ -8,24 +8,21 @@ namespace preferences {
class IntervalSyncer final : public Component {
public:
#ifdef USE_PREFERENCES_SYNC_EVERY_LOOP
void loop() override { global_preferences->sync(); }
#else
void set_write_interval(uint32_t write_interval) { this->write_interval_ = write_interval; }
void setup() override {
if (this->write_interval_ != 0) {
set_interval(this->write_interval_, []() { global_preferences->sync(); });
// When using interval-based syncing, we don't need the loop
this->disable_loop();
}
}
void loop() override {
if (this->write_interval_ == 0) {
global_preferences->sync();
}
this->set_interval(this->write_interval_, []() { global_preferences->sync(); });
}
#endif
void on_shutdown() override { global_preferences->sync(); }
float get_setup_priority() const override { return setup_priority::BUS; }
#ifndef USE_PREFERENCES_SYNC_EVERY_LOOP
protected:
uint32_t write_interval_{60000};
#endif
};
} // namespace preferences
+1
View File
@@ -118,6 +118,7 @@
#define USE_NUMBER
#define USE_OUTPUT
#define USE_POWER_SUPPLY
#define USE_PREFERENCES_SYNC_EVERY_LOOP
#define USE_QR_CODE
#define USE_SAFE_MODE_CALLBACK
#define USE_SELECT