mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-31 02:08:26 +02:00
Add restore_value to template number (#2041)
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user