12#ifdef USE_RUNTIME_STATS
18static const char *
const TAG =
"component";
19static const char *
const UNSPECIFIED_MESSAGE =
"unspecified";
39std::unique_ptr<std::vector<std::pair<const Component *, const char *>>> component_error_messages;
42std::unique_ptr<std::vector<std::pair<const Component *, float>>> setup_priority_overrides;
45namespace setup_priority {
47const float BUS = 1000.0f;
48const float IO = 900.0f;
90 App.scheduler.set_interval(
this, name, interval, std::move(f));
94 App.scheduler.set_interval(
this, name, interval, std::move(f));
98 return App.scheduler.cancel_interval(
this, name);
102 return App.scheduler.cancel_interval(
this, name);
106 std::function<
RetryResult(uint8_t)> &&f,
float backoff_increase_factor) {
107 App.scheduler.set_retry(
this, name, initial_wait_time, max_attempts, std::move(f), backoff_increase_factor);
111 return App.scheduler.cancel_retry(
this, name);
115 App.scheduler.set_timeout(
this, name, timeout, std::move(f));
119 App.scheduler.set_timeout(
this, name, timeout, std::move(f));
123 return App.scheduler.cancel_timeout(
this, name);
127 return App.scheduler.cancel_timeout(
this, name);
136 const char *error_msg =
nullptr;
137 if (component_error_messages) {
138 for (
const auto &pair : *component_error_messages) {
139 if (pair.first ==
this) {
140 error_msg = pair.second;
146 error_msg ? error_msg : UNSPECIFIED_MESSAGE);
158#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
159 uint32_t start_time =
millis();
162#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
163 uint32_t setup_time =
millis() - start_time;
253 App.scheduler.set_timeout(
this,
static_cast<const char *
>(
nullptr), 0, std::move(f));
256 return App.scheduler.cancel_timeout(
this, name);
259 App.scheduler.set_timeout(
this, name, 0, std::move(f));
262 App.scheduler.set_timeout(
this, name, 0, std::move(f));
265 App.scheduler.set_timeout(
this,
static_cast<const char *
>(
nullptr), timeout, std::move(f));
268 App.scheduler.set_interval(
this,
static_cast<const char *
>(
nullptr), interval, std::move(f));
271 float backoff_increase_factor) {
272 App.scheduler.set_retry(
this,
"", initial_wait_time, max_attempts, std::move(f), backoff_increase_factor);
289 ESP_LOGW(TAG,
"%s set Warning flag: %s", this->
get_component_source(), message ? message : UNSPECIFIED_MESSAGE);
296 ESP_LOGE(TAG,
"%s set Error flag: %s", this->
get_component_source(), message ? message : UNSPECIFIED_MESSAGE);
297 if (message !=
nullptr) {
299 if (!component_error_messages) {
300 component_error_messages = std::make_unique<std::vector<std::pair<const Component *, const char *>>>();
303 for (
auto &pair : *component_error_messages) {
304 if (pair.first ==
this) {
305 pair.second = message;
310 component_error_messages->emplace_back(
this, message);
336 if (setup_priority_overrides) {
338 for (
const auto &pair : *setup_priority_overrides) {
339 if (pair.first ==
this) {
348 if (!setup_priority_overrides) {
349 setup_priority_overrides = std::make_unique<std::vector<std::pair<const Component *, float>>>();
351 setup_priority_overrides->reserve(10);
355 for (
auto &pair : *setup_priority_overrides) {
356 if (pair.first ==
this) {
363 setup_priority_overrides->emplace_back(
this,
priority);
367#if defined(USE_HOST) || defined(CLANG_TIDY)
368 bool loop_overridden =
true;
369 bool call_loop_overridden =
true;
371#pragma GCC diagnostic push
372#pragma GCC diagnostic ignored "-Wpmf-conversions"
375#pragma GCC diagnostic pop
377 return loop_overridden || call_loop_overridden;
403 : started_(start_time), component_(component) {}
405 uint32_t curr_time =
millis();
407 uint32_t blocking_time = curr_time - this->
started_;
409#ifdef USE_RUNTIME_STATS
423 ESP_LOGW(TAG,
"%s took a long time for an operation (%" PRIu32
" ms)", src, blocking_time);
424 ESP_LOGW(TAG,
"Components should block for at most 30 ms");
434 setup_priority_overrides.reset();
void enable_component_loop_(Component *component)
void disable_component_loop_(Component *component)
volatile bool has_pending_enable_loop_requests_
virtual void mark_failed()
Mark this component as failed.
virtual void call_dump_config()
virtual float get_setup_priority() const
priority of setup().
virtual void setup()
Where the component's initialization should happen.
float get_actual_setup_priority() const
bool has_overridden_loop() const
uint8_t get_component_state() const
void status_set_warning(const char *message=nullptr)
void set_interval(const std::string &name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a unique name.
bool should_warn_of_blocking(uint32_t blocking_time)
volatile bool pending_enable_loop_
ISR-safe flag for enable_loop_soon_any_context.
virtual bool can_proceed()
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
virtual float get_loop_priority() const
priority of loop().
void status_clear_error()
void enable_loop_soon_any_context()
Thread and ISR-safe version of enable_loop() that can be called from any context.
bool is_in_loop_state() const
Check if this component has completed setup and is in the loop state.
void status_momentary_error(const std::string &name, uint32_t length=5000)
const char * get_component_source() const
Get the integration where this component was declared as a string.
uint16_t warn_if_blocking_over_
Warn if blocked for this many ms (max 65.5s)
bool cancel_retry(const std::string &name)
Cancel a retry function.
uint8_t component_state_
State of this component - each bit has a purpose: Bits 0-2: Component state (0x00=CONSTRUCTION,...
void status_momentary_warning(const std::string &name, uint32_t length=5000)
virtual void dump_config()
void enable_loop()
Enable this component's loop.
void set_component_state_(uint8_t state)
Helper to set component state (clears state bits and sets new state)
bool status_has_warning() const
bool status_has_error() const
bool cancel_interval(const std::string &name)
Cancel an interval function.
void disable_loop()
Disable this component's loop.
bool cancel_defer(const std::string &name)
Cancel a defer callback using the specified name, name must not be empty.
virtual void loop()
This method will be called repeatedly.
void reset_to_construction_state()
Reset this component back to the construction state to allow setup to run again.
const char * component_source_
void set_setup_priority(float priority)
void defer(const std::string &name, std::function< void()> &&f)
Defer a callback to the next loop() call.
void status_clear_warning()
virtual void call_setup()
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
void status_set_error(const char *message=nullptr)
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.
virtual uint32_t get_update_interval() const
Get the update interval in ms of this sensor.
void call_setup() override
virtual void set_update_interval(uint32_t update_interval)
Manually set the update interval in ms for this polling object.
uint32_t update_interval_
~WarnIfComponentBlockingGuard()
WarnIfComponentBlockingGuard(Component *component, uint32_t start_time)
void record_component_time(Component *component, uint32_t duration_ms, uint32_t current_time)
const float BUS
For communication buses like i2c/spi.
const float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
const float DATA
For components that import data from directly connected sensors like DHT.
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
const float BEFORE_CONNECTION
For components that should be initialized after WiFi and before API is connected.
const float IO
For components that represent GPIO pins like PCF8573.
const float LATE
For components that should be initialized at the very end of the setup process.
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
const float PROCESSOR
For components that use data from sensors like displays.
const float AFTER_BLUETOOTH
Providing packet encoding functions for exchanging data with a remote host.
const uint8_t COMPONENT_STATE_SETUP
const uint8_t COMPONENT_STATE_CONSTRUCTION
const uint8_t STATUS_LED_MASK
const uint16_t WARN_IF_BLOCKING_INCREMENT_MS
How long the blocking time must be larger to warn again.
runtime_stats::RuntimeStatsCollector * global_runtime_stats
const uint8_t COMPONENT_STATE_FAILED
const uint8_t COMPONENT_STATE_MASK
const uint8_t COMPONENT_STATE_LOOP
const uint16_t WARN_IF_BLOCKING_OVER_MS
Initial blocking time allowed without warning.
void clear_setup_priority_overrides()
const uint8_t STATUS_LED_OK
const uint8_t STATUS_LED_WARNING
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.
const uint8_t COMPONENT_STATE_LOOP_DONE
const uint8_t STATUS_LED_ERROR