mirror of
https://github.com/esphome/esphome.git
synced 2025-03-12 13:48:14 +00:00
[preferences] Better handling of flash_write_interval (#8199)
This commit is contained in:
parent
977333a73c
commit
4d8f58db94
@ -1,6 +1,6 @@
|
||||
from esphome.const import CONF_ID
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_ID
|
||||
|
||||
CODEOWNERS = ["@esphome/core"]
|
||||
|
||||
@ -11,9 +11,7 @@ CONF_FLASH_WRITE_INTERVAL = "flash_write_interval"
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(IntervalSyncer),
|
||||
cv.Optional(
|
||||
CONF_FLASH_WRITE_INTERVAL, default="60s"
|
||||
): cv.positive_time_period_milliseconds,
|
||||
cv.Optional(CONF_FLASH_WRITE_INTERVAL, default="60s"): cv.update_interval,
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA)
|
||||
|
||||
|
@ -8,15 +8,22 @@ namespace preferences {
|
||||
|
||||
class IntervalSyncer : public Component {
|
||||
public:
|
||||
void set_write_interval(uint32_t write_interval) { write_interval_ = write_interval; }
|
||||
void set_write_interval(uint32_t write_interval) { this->write_interval_ = write_interval; }
|
||||
void setup() override {
|
||||
set_interval(write_interval_, []() { global_preferences->sync(); });
|
||||
if (this->write_interval_ != 0) {
|
||||
set_interval(this->write_interval_, []() { global_preferences->sync(); });
|
||||
}
|
||||
}
|
||||
void loop() override {
|
||||
if (this->write_interval_ == 0) {
|
||||
global_preferences->sync();
|
||||
}
|
||||
}
|
||||
void on_shutdown() override { global_preferences->sync(); }
|
||||
float get_setup_priority() const override { return setup_priority::BUS; }
|
||||
|
||||
protected:
|
||||
uint32_t write_interval_;
|
||||
uint32_t write_interval_{60000};
|
||||
};
|
||||
|
||||
} // namespace preferences
|
||||
|
2
tests/components/preferences/test.esp32-idf.yaml
Normal file
2
tests/components/preferences/test.esp32-idf.yaml
Normal file
@ -0,0 +1,2 @@
|
||||
preferences:
|
||||
flash_write_interval: 20s
|
Loading…
x
Reference in New Issue
Block a user