ESPHome 2025.12.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::number {
5
6static const char *const TAG = "number.automation";
7
8union convert {
9 float from;
10 uint32_t to;
11};
12
14 float local_min = this->min_.value(0.0);
15 float local_max = this->max_.value(0.0);
16 convert hash = {.from = (local_max - local_min)};
17 uint32_t myhash = hash.to ^ this->parent_->get_preference_hash();
18 this->rtc_ = global_preferences->make_preference<bool>(myhash);
19 bool initial_state;
20 if (this->rtc_.load(&initial_state)) {
21 this->previous_in_range_ = initial_state;
22 }
23
24 this->parent_->add_on_state_callback([this](float state) { this->on_state_(state); });
25}
27
29 if (std::isnan(state))
30 return;
31
32 float local_min = this->min_.value(state);
33 float local_max = this->max_.value(state);
34
35 bool in_range;
36 if (std::isnan(local_min) && std::isnan(local_max)) {
37 in_range = this->previous_in_range_;
38 } else if (std::isnan(local_min)) {
39 in_range = state <= local_max;
40 } else if (std::isnan(local_max)) {
41 in_range = state >= local_min;
42 } else {
43 in_range = local_min <= state && state <= local_max;
44 }
45
46 if (in_range != this->previous_in_range_ && in_range) {
47 this->trigger(state);
48 }
49
50 this->previous_in_range_ = in_range;
51 this->rtc_.save(&in_range);
52}
53
54} // namespace esphome::number
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.
void trigger(const Ts &...x)
Definition automation.h:204
void add_on_state_callback(std::function< void(float)> &&callback)
Definition number.cpp:41
TemplatableValue< float, float > min_
Definition automation.h:66
float get_setup_priority() const override
TemplatableValue< float, float > max_
Definition automation.h:67
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:80
ESPPreferences * global_preferences