1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-21 20:52:20 +01:00

Configurable Flash Write Interval (#2119)

Co-authored-by: Alex <33379584+alexyao2015@users.noreply.github.com>
Co-authored-by: Otto winter <otto@otto-winter.com>
This commit is contained in:
Alex
2021-09-21 06:47:51 -05:00
committed by GitHub
parent 71fc61117b
commit 491f8cc611
10 changed files with 160 additions and 41 deletions

View File

@@ -0,0 +1,24 @@
from esphome.const import CONF_ID
import esphome.codegen as cg
import esphome.config_validation as cv
CODEOWNERS = ["@esphome/core"]
preferences_ns = cg.esphome_ns.namespace("preferences")
IntervalSyncer = preferences_ns.class_("IntervalSyncer", cg.Component)
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,
}
).extend(cv.COMPONENT_SCHEMA)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
cg.add(var.set_write_interval(config[CONF_FLASH_WRITE_INTERVAL]))
await cg.register_component(var, config)

View File

@@ -0,0 +1,23 @@
#pragma once
#include "esphome/core/preferences.h"
#include "esphome/core/component.h"
namespace esphome {
namespace preferences {
class IntervalSyncer : public Component {
public:
void set_write_interval(uint32_t write_interval) { write_interval_ = write_interval; }
void setup() override {
set_interval(write_interval_, []() { 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_;
};
} // namespace preferences
} // namespace esphome