ESPHome 2025.5.0
Loading...
Searching...
No Matches
automation.cpp
Go to the documentation of this file.
1#include "automation.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace number {
6
7static const char *const TAG = "number.automation";
8
9union convert {
10 float from;
11 uint32_t to;
12};
13
15 float local_min = this->min_.value(0.0);
16 float local_max = this->max_.value(0.0);
17 convert hash = {.from = (local_max - local_min)};
18 uint32_t myhash = hash.to ^ this->parent_->get_object_id_hash();
19 this->rtc_ = global_preferences->make_preference<bool>(myhash);
20 bool initial_state;
21 if (this->rtc_.load(&initial_state)) {
22 this->previous_in_range_ = initial_state;
23 }
24
25 this->parent_->add_on_state_callback([this](float state) { this->on_state_(state); });
26}
28
30 if (std::isnan(state))
31 return;
32
33 float local_min = this->min_.value(state);
34 float local_max = this->max_.value(state);
35
36 bool in_range;
37 if (std::isnan(local_min) && std::isnan(local_max)) {
38 in_range = this->previous_in_range_;
39 } else if (std::isnan(local_min)) {
40 in_range = state <= local_max;
41 } else if (std::isnan(local_max)) {
42 in_range = state >= local_min;
43 } else {
44 in_range = local_min <= state && state <= local_max;
45 }
46
47 if (in_range != this->previous_in_range_ && in_range) {
48 this->trigger(state);
49 }
50
51 this->previous_in_range_ = in_range;
52 this->rtc_.save(&in_range);
53}
54
55} // namespace number
56} // 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_object_id_hash()
void add_on_state_callback(std::function< void(float)> &&callback)
Definition number.cpp:16
TemplatableValue< float, float > min_
Definition automation.h:67
float get_setup_priority() const override
TemplatableValue< float, float > max_
Definition automation.h:68
bool state
Definition fan.h:0
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:18
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
ESPPreferences * global_preferences