ESPHome 2025.12.1
Loading...
Searching...
No Matches
template_number.cpp
Go to the documentation of this file.
1#include "template_number.h"
2#include "esphome/core/log.h"
3
4namespace esphome::template_ {
5
6static const char *const TAG = "template.number";
7
9 if (this->f_.has_value())
10 return;
11
12 float value;
13 if (!this->restore_value_) {
14 value = this->initial_value_;
15 } else {
17 if (!this->pref_.load(&value)) {
18 if (!std::isnan(this->initial_value_)) {
19 value = this->initial_value_;
20 } else {
21 value = this->traits.get_min_value();
22 }
23 }
24 }
25 this->publish_state(value);
26}
27
29 if (!this->f_.has_value())
30 return;
31
32 auto val = this->f_();
33 if (val.has_value()) {
34 this->publish_state(*val);
35 }
36}
37
38void TemplateNumber::control(float value) {
39 this->set_trigger_->trigger(value);
40
41 if (this->optimistic_)
42 this->publish_state(value);
43
44 if (this->restore_value_)
45 this->pref_.save(&value);
46}
48 LOG_NUMBER("", "Template Number", this);
49 ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
50 LOG_UPDATE_INTERVAL(this);
51}
52
53} // namespace esphome::template_
bool save(const T *src)
Definition preferences.h:21
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
uint32_t get_preference_hash()
Get a unique hash for storing preferences/settings for this entity.
bool has_value() const
Check if a lambda is set.
void trigger(const Ts &...x)
Inform the parent automation that the event has triggered.
Definition automation.h:204
void publish_state(float state)
Definition number.cpp:31
NumberTraits traits
Definition number.h:39
void control(float value) override
mopeka_std_values val[4]
const char *const TAG
Definition spi.cpp:8
ESPPreferences * global_preferences