12 static const char *
const TAG =
"component";
14 namespace setup_priority {
16 const float BUS = 1000.0f;
17 const float IO = 900.0f;
28 const float LATE = -100.0f;
61 std::function<
RetryResult(uint8_t)> &&f,
float backoff_increase_factor) {
62 App.
scheduler.
set_retry(
this, name, initial_wait_time, max_attempts, std::move(f), backoff_increase_factor);
81 if (this->is_failed()) {
82 ESP_LOGE(TAG,
" Component %s is marked FAILED", this->get_component_source());
92 this->component_state_ &= ~COMPONENT_STATE_MASK;
98 this->component_state_ &= ~COMPONENT_STATE_MASK;
114 if (this->component_source_ ==
nullptr)
116 return this->component_source_;
119 ESP_LOGE(TAG,
"Component %s was marked as failed.", this->get_component_source());
120 this->component_state_ &= ~COMPONENT_STATE_MASK;
122 this->status_set_error();
140 float backoff_increase_factor) {
141 App.
scheduler.
set_retry(
this,
"", initial_wait_time, max_attempts, std::move(f), backoff_increase_factor);
145 return (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_LOOP ||
153 if ((this->component_state_ & STATUS_LED_WARNING) != 0)
157 ESP_LOGW(TAG,
"Component %s set Warning flag: %s", this->get_component_source(), message);
160 if ((this->component_state_ & STATUS_LED_ERROR) != 0)
164 ESP_LOGE(TAG,
"Component %s set Error flag: %s", this->get_component_source(), message);
167 if ((this->component_state_ & STATUS_LED_WARNING) == 0)
169 this->component_state_ &= ~STATUS_LED_WARNING;
170 ESP_LOGW(TAG,
"Component %s cleared Warning flag", this->get_component_source());
173 if ((this->component_state_ & STATUS_LED_ERROR) == 0)
175 this->component_state_ &= ~STATUS_LED_ERROR;
176 ESP_LOGE(TAG,
"Component %s cleared Error flag", this->get_component_source());
179 this->status_set_warning();
180 this->set_timeout(name, length, [
this]() { this->status_clear_warning(); });
183 this->status_set_error();
184 this->set_timeout(name, length, [
this]() { this->status_clear_error(); });
188 if (std::isnan(this->setup_priority_override_))
189 return this->get_setup_priority();
190 return this->setup_priority_override_;
195 #if defined(USE_HOST) || defined(CLANG_TIDY) 196 bool loop_overridden =
true;
197 bool call_loop_overridden =
true;
199 #pragma GCC diagnostic push 200 #pragma GCC diagnostic ignored "-Wpmf-conversions" 203 #pragma GCC diagnostic pop 205 return loop_overridden || call_loop_overridden;
232 : started_(
millis()), component_(component) {}
237 ESP_LOGW(TAG,
"Component %s took a long time for an operation (%" PRIu32
" ms).", src, (now -
started_));
238 ESP_LOGW(TAG,
"Components should block for at most 30 ms.");
const uint32_t COMPONENT_STATE_LOOP
const uint32_t COMPONENT_STATE_FAILED
uint32_t update_interval_
const float DATA
For components that import data from directly connected sensors like DHT.
virtual void loop()
This method will be called repeatedly.
void set_interval(const std::string &name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a unique name.
const float BEFORE_CONNECTION
For components that should be initialized after WiFi and before API is connected. ...
const float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
virtual float get_loop_priority() const
priority of loop().
~WarnIfComponentBlockingGuard()
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
void status_set_warning(const char *message="unspecified")
bool status_has_warning() const
bool cancel_timeout(Component *component, const std::string &name)
void status_momentary_warning(const std::string &name, uint32_t length=5000)
float get_actual_setup_priority() const
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
bool cancel_interval(const std::string &name)
Cancel an interval function.
const uint32_t STATUS_LED_OK
void set_setup_priority(float priority)
void defer(const std::string &name, std::function< void()> &&f)
Defer a callback to the next loop() call.
const float LATE
For components that should be initialized at the very end of the setup process.
bool has_overridden_loop() const
virtual void dump_config()
const float AFTER_BLUETOOTH
virtual void call_dump_config()
uint32_t IRAM_ATTR HOT millis()
void status_momentary_error(const std::string &name, uint32_t length=5000)
bool status_has_error() const
virtual void call_setup()
void set_retry(const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, std::function< RetryResult(uint8_t)> &&f, float backoff_increase_factor=1.0f)
Set an retry function with a unique name.
void set_retry(Component *component, const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, std::function< RetryResult(uint8_t)> func, float backoff_increase_factor=1.0f)
void status_set_error(const char *message="unspecified")
uint32_t get_component_state() const
const float BUS
For communication buses like i2c/spi.
virtual void setup()
Where the component's initialization should happen.
const uint32_t COMPONENT_STATE_SETUP
virtual float get_setup_priority() const
priority of setup().
void status_clear_warning()
const uint32_t COMPONENT_STATE_CONSTRUCTION
bool cancel_defer(const std::string &name)
Cancel a defer callback using the specified name, name must not be empty.
const float PROCESSOR
For components that use data from sensors like displays.
const char * get_component_source() const
Get the integration where this component was declared as a string.
Application App
Global storage of Application pointer - only one Application can exist.
virtual bool can_proceed()
bool cancel_retry(Component *component, const std::string &name)
WarnIfComponentBlockingGuard(Component *component)
const uint32_t COMPONENT_STATE_MASK
bool cancel_retry(const std::string &name)
Cancel a retry function.
virtual uint32_t get_update_interval() const
Get the update interval in ms of this sensor.
const uint32_t STATUS_LED_WARNING
void status_clear_error()
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
void call_setup() override
virtual void mark_failed()
Mark this component as failed.
bool cancel_interval(Component *component, const std::string &name)
const float IO
For components that represent GPIO pins like PCF8573.
const uint32_t STATUS_LED_ERROR
void set_timeout(Component *component, const std::string &name, uint32_t timeout, std::function< void()> func)
Implementation of SPI Controller mode.
const uint32_t STATUS_LED_MASK
virtual void set_update_interval(uint32_t update_interval)
Manually set the update interval in ms for this polling object.
void set_interval(Component *component, const std::string &name, uint32_t interval, std::function< void()> func)