ESPHome 2025.11.0b4
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 {
5namespace template_ {
6
7static const char *const TAG = "template.number";
8
10 if (this->f_.has_value())
11 return;
12
13 float value;
14 if (!this->restore_value_) {
15 value = this->initial_value_;
16 } else {
18 if (!this->pref_.load(&value)) {
19 if (!std::isnan(this->initial_value_)) {
20 value = this->initial_value_;
21 } else {
22 value = this->traits.get_min_value();
23 }
24 }
25 }
26 this->publish_state(value);
27}
28
30 if (!this->f_.has_value())
31 return;
32
33 auto val = this->f_();
34 if (val.has_value()) {
35 this->publish_state(*val);
36 }
37}
38
39void TemplateNumber::control(float value) {
40 this->set_trigger_->trigger(value);
41
42 if (this->optimistic_)
43 this->publish_state(value);
44
45 if (this->restore_value_)
46 this->pref_.save(&value);
47}
49 LOG_NUMBER("", "Template Number", this);
50 ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
51 LOG_UPDATE_INTERVAL(this);
52}
53
54} // namespace template_
55} // namespace esphome
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:169
void publish_state(float state)
Definition number.cpp:32
NumberTraits traits
Definition number.h:40
void control(float value) override
mopeka_std_values val[4]
const char *const TAG
Definition spi.cpp:8
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
ESPPreferences * global_preferences