Add restore_value to template number (#2041)

This commit is contained in:
Jesse Hills
2021-07-20 15:40:42 +12:00
committed by GitHub
parent 7619507e6c
commit 99d2db42cd
3 changed files with 39 additions and 16 deletions
@@ -7,16 +7,20 @@ namespace template_ {
static const char *const TAG = "template.number";
void TemplateNumber::setup() {
if (this->f_.has_value() || !this->optimistic_)
if (this->f_.has_value())
return;
this->pref_ = global_preferences.make_preference<float>(this->get_object_id_hash());
float value;
if (!this->pref_.load(&value)) {
if (!isnan(this->initial_value_))
value = this->initial_value_;
else
value = this->traits.get_min_value();
if (!this->restore_value_) {
value = this->initial_value_;
} else {
this->pref_ = global_preferences.make_preference<float>(this->get_object_id_hash());
if (!this->pref_.load(&value)) {
if (!isnan(this->initial_value_))
value = this->initial_value_;
else
value = this->traits.get_min_value();
}
}
this->publish_state(value);
}
@@ -35,10 +39,11 @@ void TemplateNumber::update() {
void TemplateNumber::control(float value) {
this->set_trigger_->trigger(value);
if (this->optimistic_) {
if (this->optimistic_)
this->publish_state(value);
if (this->restore_value_)
this->pref_.save(&value);
}
}
void TemplateNumber::dump_config() {
LOG_NUMBER("", "Template Number", this);