ESPHome  2024.3.1
automation.cpp
Go to the documentation of this file.
1 #include "automation.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace number {
6 
7 static const char *const TAG = "number.automation";
8 
9 union 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
float get_setup_priority() const override
Definition: automation.cpp:27
ESPPreferences * global_preferences
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
bool state
Definition: fan.h:34