mirror of
https://github.com/esphome/esphome.git
synced 2024-11-25 08:28:12 +01:00
Remove min_save_interval from intergration and total_daily_energy (#3498)
This commit is contained in:
parent
6f83a49c63
commit
bca96f91b2
7 changed files with 15 additions and 32 deletions
|
@ -10,14 +10,13 @@ static const char *const TAG = "integration";
|
||||||
|
|
||||||
void IntegrationSensor::setup() {
|
void IntegrationSensor::setup() {
|
||||||
if (this->restore_) {
|
if (this->restore_) {
|
||||||
this->rtc_ = global_preferences->make_preference<float>(this->get_object_id_hash());
|
this->pref_ = global_preferences->make_preference<float>(this->get_object_id_hash());
|
||||||
float preference_value = 0;
|
float preference_value = 0;
|
||||||
this->rtc_.load(&preference_value);
|
this->pref_.load(&preference_value);
|
||||||
this->result_ = preference_value;
|
this->result_ = preference_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->last_update_ = millis();
|
this->last_update_ = millis();
|
||||||
this->last_save_ = this->last_update_;
|
|
||||||
|
|
||||||
this->publish_and_save_(this->result_);
|
this->publish_and_save_(this->result_);
|
||||||
this->sensor_->add_on_state_callback([this](float state) { this->process_sensor_value_(state); });
|
this->sensor_->add_on_state_callback([this](float state) { this->process_sensor_value_(state); });
|
||||||
|
|
|
@ -28,7 +28,6 @@ class IntegrationSensor : public sensor::Sensor, public Component {
|
||||||
void setup() override;
|
void setup() override;
|
||||||
void dump_config() override;
|
void dump_config() override;
|
||||||
float get_setup_priority() const override { return setup_priority::DATA; }
|
float get_setup_priority() const override { return setup_priority::DATA; }
|
||||||
void set_min_save_interval(uint32_t min_interval) { this->min_save_interval_ = min_interval; }
|
|
||||||
void set_sensor(Sensor *sensor) { sensor_ = sensor; }
|
void set_sensor(Sensor *sensor) { sensor_ = sensor; }
|
||||||
void set_time(IntegrationSensorTime time) { time_ = time; }
|
void set_time(IntegrationSensorTime time) { time_ = time; }
|
||||||
void set_method(IntegrationMethod method) { method_ = method; }
|
void set_method(IntegrationMethod method) { method_ = method; }
|
||||||
|
@ -56,22 +55,18 @@ class IntegrationSensor : public sensor::Sensor, public Component {
|
||||||
void publish_and_save_(double result) {
|
void publish_and_save_(double result) {
|
||||||
this->result_ = result;
|
this->result_ = result;
|
||||||
this->publish_state(result);
|
this->publish_state(result);
|
||||||
float result_f = result;
|
if (this->restore_) {
|
||||||
const uint32_t now = millis();
|
float result_f = result;
|
||||||
if (now - this->last_save_ < this->min_save_interval_)
|
this->pref_.save(&result_f);
|
||||||
return;
|
}
|
||||||
this->last_save_ = now;
|
|
||||||
this->rtc_.save(&result_f);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sensor::Sensor *sensor_;
|
sensor::Sensor *sensor_;
|
||||||
IntegrationSensorTime time_;
|
IntegrationSensorTime time_;
|
||||||
IntegrationMethod method_;
|
IntegrationMethod method_;
|
||||||
bool restore_;
|
bool restore_;
|
||||||
ESPPreferenceObject rtc_;
|
ESPPreferenceObject pref_;
|
||||||
|
|
||||||
uint32_t last_save_{0};
|
|
||||||
uint32_t min_save_interval_{0};
|
|
||||||
uint32_t last_update_;
|
uint32_t last_update_;
|
||||||
double result_{0.0f};
|
double result_{0.0f};
|
||||||
float last_value_{0.0f};
|
float last_value_{0.0f};
|
||||||
|
|
|
@ -35,7 +35,6 @@ INTEGRATION_METHODS = {
|
||||||
|
|
||||||
CONF_TIME_UNIT = "time_unit"
|
CONF_TIME_UNIT = "time_unit"
|
||||||
CONF_INTEGRATION_METHOD = "integration_method"
|
CONF_INTEGRATION_METHOD = "integration_method"
|
||||||
CONF_MIN_SAVE_INTERVAL = "min_save_interval"
|
|
||||||
|
|
||||||
|
|
||||||
def inherit_unit_of_measurement(uom, config):
|
def inherit_unit_of_measurement(uom, config):
|
||||||
|
@ -58,9 +57,9 @@ CONFIG_SCHEMA = sensor.SENSOR_SCHEMA.extend(
|
||||||
INTEGRATION_METHODS, lower=True
|
INTEGRATION_METHODS, lower=True
|
||||||
),
|
),
|
||||||
cv.Optional(CONF_RESTORE, default=False): cv.boolean,
|
cv.Optional(CONF_RESTORE, default=False): cv.boolean,
|
||||||
cv.Optional(
|
cv.Optional("min_save_interval"): cv.invalid(
|
||||||
CONF_MIN_SAVE_INTERVAL, default="0s"
|
"min_save_interval was removed in 2022.8.0. Please use the `preferences` -> `flash_write_interval` to adjust."
|
||||||
): cv.positive_time_period_milliseconds,
|
),
|
||||||
}
|
}
|
||||||
).extend(cv.COMPONENT_SCHEMA)
|
).extend(cv.COMPONENT_SCHEMA)
|
||||||
|
|
||||||
|
@ -97,7 +96,6 @@ async def to_code(config):
|
||||||
cg.add(var.set_time(config[CONF_TIME_UNIT]))
|
cg.add(var.set_time(config[CONF_TIME_UNIT]))
|
||||||
cg.add(var.set_method(config[CONF_INTEGRATION_METHOD]))
|
cg.add(var.set_method(config[CONF_INTEGRATION_METHOD]))
|
||||||
cg.add(var.set_restore(config[CONF_RESTORE]))
|
cg.add(var.set_restore(config[CONF_RESTORE]))
|
||||||
cg.add(var.set_min_save_interval(config[CONF_MIN_SAVE_INTERVAL]))
|
|
||||||
|
|
||||||
|
|
||||||
@automation.register_action(
|
@automation.register_action(
|
||||||
|
|
|
@ -17,7 +17,6 @@ from esphome.core.entity_helpers import inherit_property_from
|
||||||
DEPENDENCIES = ["time"]
|
DEPENDENCIES = ["time"]
|
||||||
|
|
||||||
CONF_POWER_ID = "power_id"
|
CONF_POWER_ID = "power_id"
|
||||||
CONF_MIN_SAVE_INTERVAL = "min_save_interval"
|
|
||||||
total_daily_energy_ns = cg.esphome_ns.namespace("total_daily_energy")
|
total_daily_energy_ns = cg.esphome_ns.namespace("total_daily_energy")
|
||||||
TotalDailyEnergyMethod = total_daily_energy_ns.enum("TotalDailyEnergyMethod")
|
TotalDailyEnergyMethod = total_daily_energy_ns.enum("TotalDailyEnergyMethod")
|
||||||
TOTAL_DAILY_ENERGY_METHODS = {
|
TOTAL_DAILY_ENERGY_METHODS = {
|
||||||
|
@ -49,9 +48,9 @@ CONFIG_SCHEMA = (
|
||||||
cv.GenerateID(CONF_TIME_ID): cv.use_id(time.RealTimeClock),
|
cv.GenerateID(CONF_TIME_ID): cv.use_id(time.RealTimeClock),
|
||||||
cv.Required(CONF_POWER_ID): cv.use_id(sensor.Sensor),
|
cv.Required(CONF_POWER_ID): cv.use_id(sensor.Sensor),
|
||||||
cv.Optional(CONF_RESTORE, default=True): cv.boolean,
|
cv.Optional(CONF_RESTORE, default=True): cv.boolean,
|
||||||
cv.Optional(
|
cv.Optional("min_save_interval"): cv.invalid(
|
||||||
CONF_MIN_SAVE_INTERVAL, default="0s"
|
"`min_save_interval` was removed in 2022.6.0. Please use the `preferences` -> `flash_write_interval` to adjust."
|
||||||
): cv.positive_time_period_milliseconds,
|
),
|
||||||
cv.Optional(CONF_METHOD, default="right"): cv.enum(
|
cv.Optional(CONF_METHOD, default="right"): cv.enum(
|
||||||
TOTAL_DAILY_ENERGY_METHODS, lower=True
|
TOTAL_DAILY_ENERGY_METHODS, lower=True
|
||||||
),
|
),
|
||||||
|
@ -90,5 +89,4 @@ async def to_code(config):
|
||||||
time_ = await cg.get_variable(config[CONF_TIME_ID])
|
time_ = await cg.get_variable(config[CONF_TIME_ID])
|
||||||
cg.add(var.set_time(time_))
|
cg.add(var.set_time(time_))
|
||||||
cg.add(var.set_restore(config[CONF_RESTORE]))
|
cg.add(var.set_restore(config[CONF_RESTORE]))
|
||||||
cg.add(var.set_min_save_interval(config[CONF_MIN_SAVE_INTERVAL]))
|
|
||||||
cg.add(var.set_method(config[CONF_METHOD]))
|
cg.add(var.set_method(config[CONF_METHOD]))
|
||||||
|
|
|
@ -16,7 +16,6 @@ void TotalDailyEnergy::setup() {
|
||||||
this->publish_state_and_save(initial_value);
|
this->publish_state_and_save(initial_value);
|
||||||
|
|
||||||
this->last_update_ = millis();
|
this->last_update_ = millis();
|
||||||
this->last_save_ = this->last_update_;
|
|
||||||
|
|
||||||
this->parent_->add_on_state_callback([this](float state) { this->process_new_state_(state); });
|
this->parent_->add_on_state_callback([this](float state) { this->process_new_state_(state); });
|
||||||
}
|
}
|
||||||
|
@ -43,12 +42,9 @@ void TotalDailyEnergy::loop() {
|
||||||
void TotalDailyEnergy::publish_state_and_save(float state) {
|
void TotalDailyEnergy::publish_state_and_save(float state) {
|
||||||
this->total_energy_ = state;
|
this->total_energy_ = state;
|
||||||
this->publish_state(state);
|
this->publish_state(state);
|
||||||
const uint32_t now = millis();
|
if (this->restore_) {
|
||||||
if (now - this->last_save_ < this->min_save_interval_) {
|
this->pref_.save(&state);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
this->last_save_ = now;
|
|
||||||
this->pref_.save(&state);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TotalDailyEnergy::process_new_state_(float state) {
|
void TotalDailyEnergy::process_new_state_(float state) {
|
||||||
|
|
|
@ -18,7 +18,6 @@ enum TotalDailyEnergyMethod {
|
||||||
class TotalDailyEnergy : public sensor::Sensor, public Component {
|
class TotalDailyEnergy : public sensor::Sensor, public Component {
|
||||||
public:
|
public:
|
||||||
void set_restore(bool restore) { restore_ = restore; }
|
void set_restore(bool restore) { restore_ = restore; }
|
||||||
void set_min_save_interval(uint32_t min_interval) { this->min_save_interval_ = min_interval; }
|
|
||||||
void set_time(time::RealTimeClock *time) { time_ = time; }
|
void set_time(time::RealTimeClock *time) { time_ = time; }
|
||||||
void set_parent(Sensor *parent) { parent_ = parent; }
|
void set_parent(Sensor *parent) { parent_ = parent; }
|
||||||
void set_method(TotalDailyEnergyMethod method) { method_ = method; }
|
void set_method(TotalDailyEnergyMethod method) { method_ = method; }
|
||||||
|
@ -39,7 +38,6 @@ class TotalDailyEnergy : public sensor::Sensor, public Component {
|
||||||
uint16_t last_day_of_year_{};
|
uint16_t last_day_of_year_{};
|
||||||
uint32_t last_update_{0};
|
uint32_t last_update_{0};
|
||||||
uint32_t last_save_{0};
|
uint32_t last_save_{0};
|
||||||
uint32_t min_save_interval_{0};
|
|
||||||
bool restore_;
|
bool restore_;
|
||||||
float total_energy_{0.0f};
|
float total_energy_{0.0f};
|
||||||
float last_power_state_{0.0f};
|
float last_power_state_{0.0f};
|
||||||
|
|
|
@ -600,7 +600,6 @@ sensor:
|
||||||
sensor: hlw8012_power
|
sensor: hlw8012_power
|
||||||
name: "Integration Sensor lazy"
|
name: "Integration Sensor lazy"
|
||||||
time_unit: s
|
time_unit: s
|
||||||
min_save_interval: 60s
|
|
||||||
- platform: hmc5883l
|
- platform: hmc5883l
|
||||||
address: 0x68
|
address: 0x68
|
||||||
field_strength_x:
|
field_strength_x:
|
||||||
|
|
Loading…
Reference in a new issue