ESPHome  2022.11.3
component.cpp
Go to the documentation of this file.
2 
4 #include "esphome/core/hal.h"
5 #include "esphome/core/helpers.h"
6 #include "esphome/core/log.h"
7 #include <utility>
8 
9 namespace esphome {
10 
11 static const char *const TAG = "component";
12 
13 namespace setup_priority {
14 
15 const float BUS = 1000.0f;
16 const float IO = 900.0f;
17 const float HARDWARE = 800.0f;
18 const float DATA = 600.0f;
19 const float PROCESSOR = 400.0;
20 const float BLUETOOTH = 350.0f;
21 const float AFTER_BLUETOOTH = 300.0f;
22 const float WIFI = 250.0f;
23 const float ETHERNET = 250.0f;
24 const float BEFORE_CONNECTION = 220.0f;
25 const float AFTER_WIFI = 200.0f;
26 const float AFTER_CONNECTION = 100.0f;
27 const float LATE = -100.0f;
28 
29 } // namespace setup_priority
30 
31 const uint32_t COMPONENT_STATE_MASK = 0xFF;
32 const uint32_t COMPONENT_STATE_CONSTRUCTION = 0x00;
33 const uint32_t COMPONENT_STATE_SETUP = 0x01;
34 const uint32_t COMPONENT_STATE_LOOP = 0x02;
35 const uint32_t COMPONENT_STATE_FAILED = 0x03;
36 const uint32_t STATUS_LED_MASK = 0xFF00;
37 const uint32_t STATUS_LED_OK = 0x0000;
38 const uint32_t STATUS_LED_WARNING = 0x0100;
39 const uint32_t STATUS_LED_ERROR = 0x0200;
40 
41 uint32_t global_state = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
42 
43 float Component::get_loop_priority() const { return 0.0f; }
44 
46 
48 
49 void Component::loop() {}
50 
51 void Component::set_interval(const std::string &name, uint32_t interval, std::function<void()> &&f) { // NOLINT
52  App.scheduler.set_interval(this, name, interval, std::move(f));
53 }
54 
55 bool Component::cancel_interval(const std::string &name) { // NOLINT
56  return App.scheduler.cancel_interval(this, name);
57 }
58 
59 void Component::set_retry(const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts,
60  std::function<RetryResult()> &&f, float backoff_increase_factor) { // NOLINT
61  App.scheduler.set_retry(this, name, initial_wait_time, max_attempts, std::move(f), backoff_increase_factor);
62 }
63 
64 bool Component::cancel_retry(const std::string &name) { // NOLINT
65  return App.scheduler.cancel_retry(this, name);
66 }
67 
68 void Component::set_timeout(const std::string &name, uint32_t timeout, std::function<void()> &&f) { // NOLINT
69  return App.scheduler.set_timeout(this, name, timeout, std::move(f));
70 }
71 
72 bool Component::cancel_timeout(const std::string &name) { // NOLINT
73  return App.scheduler.cancel_timeout(this, name);
74 }
75 
76 void Component::call_loop() { this->loop(); }
77 void Component::call_setup() { this->setup(); }
78 void Component::call_dump_config() { this->dump_config(); }
79 
80 uint32_t Component::get_component_state() const { return this->component_state_; }
82  uint32_t state = this->component_state_ & COMPONENT_STATE_MASK;
83  switch (state) {
85  // State Construction: Call setup and set state to setup
86  this->component_state_ &= ~COMPONENT_STATE_MASK;
87  this->component_state_ |= COMPONENT_STATE_SETUP;
88  this->call_setup();
89  break;
91  // State setup: Call first loop and set state to loop
92  this->component_state_ &= ~COMPONENT_STATE_MASK;
93  this->component_state_ |= COMPONENT_STATE_LOOP;
94  this->call_loop();
95  break;
97  // State loop: Call loop
98  this->call_loop();
99  break;
100  case COMPONENT_STATE_FAILED: // NOLINT(bugprone-branch-clone)
101  // State failed: Do nothing
102  break;
103  default:
104  break;
105  }
106 }
107 const char *Component::get_component_source() const {
108  if (this->component_source_ == nullptr)
109  return "<unknown>";
110  return this->component_source_;
111 }
113  ESP_LOGE(TAG, "Component %s was marked as failed.", this->get_component_source());
114  this->component_state_ &= ~COMPONENT_STATE_MASK;
115  this->component_state_ |= COMPONENT_STATE_FAILED;
116  this->status_set_error();
117 }
118 void Component::defer(std::function<void()> &&f) { // NOLINT
119  App.scheduler.set_timeout(this, "", 0, std::move(f));
120 }
121 bool Component::cancel_defer(const std::string &name) { // NOLINT
122  return App.scheduler.cancel_timeout(this, name);
123 }
124 void Component::defer(const std::string &name, std::function<void()> &&f) { // NOLINT
125  App.scheduler.set_timeout(this, name, 0, std::move(f));
126 }
127 void Component::set_timeout(uint32_t timeout, std::function<void()> &&f) { // NOLINT
128  App.scheduler.set_timeout(this, "", timeout, std::move(f));
129 }
130 void Component::set_interval(uint32_t interval, std::function<void()> &&f) { // NOLINT
131  App.scheduler.set_interval(this, "", interval, std::move(f));
132 }
133 void Component::set_retry(uint32_t initial_wait_time, uint8_t max_attempts, std::function<RetryResult()> &&f,
134  float backoff_increase_factor) { // NOLINT
135  App.scheduler.set_retry(this, "", initial_wait_time, max_attempts, std::move(f), backoff_increase_factor);
136 }
137 bool Component::is_failed() { return (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_FAILED; }
138 bool Component::can_proceed() { return true; }
139 bool Component::status_has_warning() { return this->component_state_ & STATUS_LED_WARNING; }
140 bool Component::status_has_error() { return this->component_state_ & STATUS_LED_ERROR; }
142  this->component_state_ |= STATUS_LED_WARNING;
144 }
146  this->component_state_ |= STATUS_LED_ERROR;
148 }
149 void Component::status_clear_warning() { this->component_state_ &= ~STATUS_LED_WARNING; }
150 void Component::status_clear_error() { this->component_state_ &= ~STATUS_LED_ERROR; }
151 void Component::status_momentary_warning(const std::string &name, uint32_t length) {
152  this->status_set_warning();
153  this->set_timeout(name, length, [this]() { this->status_clear_warning(); });
154 }
155 void Component::status_momentary_error(const std::string &name, uint32_t length) {
156  this->status_set_error();
157  this->set_timeout(name, length, [this]() { this->status_clear_error(); });
158 }
161  if (std::isnan(this->setup_priority_override_))
162  return this->get_setup_priority();
163  return this->setup_priority_override_;
164 }
165 void Component::set_setup_priority(float priority) { this->setup_priority_override_ = priority; }
166 
168 #ifdef CLANG_TIDY
169  bool loop_overridden = true;
170  bool call_loop_overridden = true;
171 #else
172 #pragma GCC diagnostic push
173 #pragma GCC diagnostic ignored "-Wpmf-conversions"
174  bool loop_overridden = (void *) (this->*(&Component::loop)) != (void *) (&Component::loop);
175  bool call_loop_overridden = (void *) (this->*(&Component::call_loop)) != (void *) (&Component::call_loop);
176 #pragma GCC diagnostic pop
177 #endif
178  return loop_overridden || call_loop_overridden;
179 }
180 
181 PollingComponent::PollingComponent(uint32_t update_interval) : update_interval_(update_interval) {}
182 
184  // Let the polling component subclass setup their HW.
185  this->setup();
186 
187  // Register interval.
188  this->set_interval("update", this->get_update_interval(), [this]() { this->update(); });
189 }
190 
191 uint32_t PollingComponent::get_update_interval() const { return this->update_interval_; }
192 void PollingComponent::set_update_interval(uint32_t update_interval) { this->update_interval_ = update_interval; }
193 
195  : started_(millis()), component_(component) {}
197  uint32_t now = millis();
198  if (now - started_ > 50) {
199  const char *src = component_ == nullptr ? "<null>" : component_->get_component_source();
200  ESP_LOGV(TAG, "Component %s took a long time for an operation (%.2f s).", src, (now - started_) / 1e3f);
201  ESP_LOGV(TAG, "Components should block for at most 20-30ms.");
202  ;
203  }
204 }
205 
206 } // namespace esphome
void setup()
const uint32_t COMPONENT_STATE_LOOP
Definition: component.cpp:34
const uint32_t COMPONENT_STATE_FAILED
Definition: component.cpp:35
const char * name
Definition: stm32flash.h:78
void loop()
RetryResult
Definition: component.h:65
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:18
virtual void loop()
This method will be called repeatedly.
Definition: component.cpp:49
void set_interval(const std::string &name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a unique name.
Definition: component.cpp:51
const float BEFORE_CONNECTION
For components that should be initialized after WiFi and before API is connected. ...
Definition: component.cpp:24
const float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition: component.cpp:26
virtual float get_loop_priority() const
priority of loop().
Definition: component.cpp:43
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
Definition: component.cpp:72
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition: component.cpp:25
void set_retry(const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, std::function< RetryResult()> &&f, float backoff_increase_factor=1.0f)
Set an retry function with a unique name.
Definition: component.cpp:59
bool cancel_timeout(Component *component, const std::string &name)
Definition: scheduler.cpp:39
void status_momentary_warning(const std::string &name, uint32_t length=5000)
Definition: component.cpp:151
float get_actual_setup_priority() const
Definition: component.cpp:160
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition: component.cpp:68
bool cancel_interval(const std::string &name)
Cancel an interval function.
Definition: component.cpp:55
const uint32_t STATUS_LED_OK
Definition: component.cpp:37
void set_setup_priority(float priority)
Definition: component.cpp:165
void defer(const std::string &name, std::function< void()> &&f)
Defer a callback to the next loop() call.
Definition: component.cpp:124
const float LATE
For components that should be initialized at the very end of the setup process.
Definition: component.cpp:27
bool has_overridden_loop() const
Definition: component.cpp:167
bool status_has_warning()
Definition: component.cpp:139
virtual void dump_config()
Definition: component.cpp:159
const float AFTER_BLUETOOTH
Definition: component.cpp:21
virtual void call_dump_config()
Definition: component.cpp:78
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:26
void status_momentary_error(const std::string &name, uint32_t length=5000)
Definition: component.cpp:155
uint32_t global_state
Definition: component.cpp:41
virtual void call_setup()
Definition: component.cpp:77
uint32_t get_component_state() const
Definition: component.cpp:80
const float BUS
For communication buses like i2c/spi.
Definition: component.cpp:15
virtual void setup()
Where the component&#39;s initialization should happen.
Definition: component.cpp:47
const uint32_t COMPONENT_STATE_SETUP
Definition: component.cpp:33
virtual float get_setup_priority() const
priority of setup().
Definition: component.cpp:45
void status_clear_warning()
Definition: component.cpp:149
const uint32_t COMPONENT_STATE_CONSTRUCTION
Definition: component.cpp:32
bool cancel_defer(const std::string &name)
Cancel a defer callback using the specified name, name must not be empty.
Definition: component.cpp:121
virtual void update()=0
const float PROCESSOR
For components that use data from sensors like displays.
Definition: component.cpp:19
const char * get_component_source() const
Get the integration where this component was declared as a string.
Definition: component.cpp:107
Application App
Global storage of Application pointer - only one Application can exist.
virtual bool can_proceed()
Definition: component.cpp:138
bool cancel_retry(Component *component, const std::string &name)
Definition: scheduler.cpp:116
WarnIfComponentBlockingGuard(Component *component)
Definition: component.cpp:194
void status_set_warning()
Definition: component.cpp:141
const uint32_t COMPONENT_STATE_MASK
Definition: component.cpp:31
bool cancel_retry(const std::string &name)
Cancel a retry function.
Definition: component.cpp:64
virtual uint32_t get_update_interval() const
Get the update interval in ms of this sensor.
Definition: component.cpp:191
const uint32_t STATUS_LED_WARNING
Definition: component.cpp:38
uint8_t priority
void status_clear_error()
Definition: component.cpp:150
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:17
void call_setup() override
Definition: component.cpp:183
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:112
bool cancel_interval(Component *component, const std::string &name)
Definition: scheduler.cpp:72
const float IO
For components that represent GPIO pins like PCF8573.
Definition: component.cpp:16
const uint32_t STATUS_LED_ERROR
Definition: component.cpp:39
void set_timeout(Component *component, const std::string &name, uint32_t timeout, std::function< void()> func)
Definition: scheduler.cpp:16
Definition: a4988.cpp:4
virtual void call_loop()
Definition: component.cpp:76
const uint32_t STATUS_LED_MASK
Definition: component.cpp:36
virtual void set_update_interval(uint32_t update_interval)
Manually set the update interval in ms for this polling object.
Definition: component.cpp:192
void set_retry(Component *component, const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, std::function< RetryResult()> func, float backoff_increase_factor=1.0f)
Definition: scheduler.cpp:94
void set_interval(Component *component, const std::string &name, uint32_t interval, std::function< void()> func)
Definition: scheduler.cpp:42
bool state
Definition: fan.h:34