mirror of
https://github.com/esphome/esphome.git
synced 2024-12-02 11:44:13 +01:00
80d03a631e
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
56 lines
1.2 KiB
C++
56 lines
1.2 KiB
C++
#include "template_number.h"
|
|
#include "esphome/core/log.h"
|
|
|
|
namespace esphome {
|
|
namespace template_ {
|
|
|
|
static const char *const TAG = "template.number";
|
|
|
|
void TemplateNumber::setup() {
|
|
if (this->f_.has_value())
|
|
return;
|
|
|
|
float 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 (!std::isnan(this->initial_value_)) {
|
|
value = this->initial_value_;
|
|
} else {
|
|
value = this->traits.get_min_value();
|
|
}
|
|
}
|
|
}
|
|
this->publish_state(value);
|
|
}
|
|
|
|
void TemplateNumber::update() {
|
|
if (!this->f_.has_value())
|
|
return;
|
|
|
|
auto val = (*this->f_)();
|
|
if (!val.has_value())
|
|
return;
|
|
|
|
this->publish_state(*val);
|
|
}
|
|
|
|
void TemplateNumber::control(float value) {
|
|
this->set_trigger_->trigger(value);
|
|
|
|
if (this->optimistic_)
|
|
this->publish_state(value);
|
|
|
|
if (this->restore_value_)
|
|
this->pref_.save(&value);
|
|
}
|
|
void TemplateNumber::dump_config() {
|
|
LOG_NUMBER("", "Template Number", this);
|
|
ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
|
|
LOG_UPDATE_INTERVAL(this);
|
|
}
|
|
|
|
} // namespace template_
|
|
} // namespace esphome
|