2019-04-17 12:06:00 +02:00
|
|
|
#include "esphome/core/component.h"
|
2021-06-10 13:04:40 +02:00
|
|
|
|
|
|
|
#include "esphome/core/application.h"
|
2019-04-17 12:06:00 +02:00
|
|
|
#include "esphome/core/esphal.h"
|
2021-06-10 13:04:40 +02:00
|
|
|
#include "esphome/core/helpers.h"
|
2019-04-17 12:06:00 +02:00
|
|
|
#include "esphome/core/log.h"
|
2021-06-10 13:04:40 +02:00
|
|
|
#include <utility>
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
namespace esphome {
|
|
|
|
|
2021-06-10 22:19:44 +02:00
|
|
|
static const char *const TAG = "component";
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
namespace setup_priority {
|
|
|
|
|
|
|
|
const float BUS = 1000.0f;
|
|
|
|
const float IO = 900.0f;
|
|
|
|
const float HARDWARE = 800.0f;
|
|
|
|
const float DATA = 600.0f;
|
|
|
|
const float PROCESSOR = 400.0;
|
2021-06-08 11:56:21 +12:00
|
|
|
const float BLUETOOTH = 350.0f;
|
|
|
|
const float AFTER_BLUETOOTH = 300.0f;
|
2019-04-17 12:06:00 +02:00
|
|
|
const float WIFI = 250.0f;
|
|
|
|
const float AFTER_WIFI = 200.0f;
|
|
|
|
const float AFTER_CONNECTION = 100.0f;
|
|
|
|
const float LATE = -100.0f;
|
|
|
|
|
|
|
|
} // namespace setup_priority
|
|
|
|
|
|
|
|
const uint32_t COMPONENT_STATE_MASK = 0xFF;
|
|
|
|
const uint32_t COMPONENT_STATE_CONSTRUCTION = 0x00;
|
|
|
|
const uint32_t COMPONENT_STATE_SETUP = 0x01;
|
|
|
|
const uint32_t COMPONENT_STATE_LOOP = 0x02;
|
|
|
|
const uint32_t COMPONENT_STATE_FAILED = 0x03;
|
|
|
|
const uint32_t STATUS_LED_MASK = 0xFF00;
|
|
|
|
const uint32_t STATUS_LED_OK = 0x0000;
|
|
|
|
const uint32_t STATUS_LED_WARNING = 0x0100;
|
|
|
|
const uint32_t STATUS_LED_ERROR = 0x0200;
|
|
|
|
|
2021-06-10 22:19:44 +02:00
|
|
|
uint32_t global_state = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
float Component::get_loop_priority() const { return 0.0f; }
|
|
|
|
|
|
|
|
float Component::get_setup_priority() const { return setup_priority::DATA; }
|
|
|
|
|
|
|
|
void Component::setup() {}
|
|
|
|
|
|
|
|
void Component::loop() {}
|
|
|
|
|
|
|
|
void Component::set_interval(const std::string &name, uint32_t interval, std::function<void()> &&f) { // NOLINT
|
2019-06-07 14:26:40 +02:00
|
|
|
App.scheduler.set_interval(this, name, interval, std::move(f));
|
2019-04-17 12:06:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Component::cancel_interval(const std::string &name) { // NOLINT
|
2019-06-07 14:26:40 +02:00
|
|
|
return App.scheduler.cancel_interval(this, name);
|
2019-04-17 12:06:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Component::set_timeout(const std::string &name, uint32_t timeout, std::function<void()> &&f) { // NOLINT
|
2019-06-07 14:26:40 +02:00
|
|
|
return App.scheduler.set_timeout(this, name, timeout, std::move(f));
|
2019-04-17 12:06:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Component::cancel_timeout(const std::string &name) { // NOLINT
|
2019-06-07 14:26:40 +02:00
|
|
|
return App.scheduler.cancel_timeout(this, name);
|
2019-04-17 12:06:00 +02:00
|
|
|
}
|
|
|
|
|
2019-06-09 17:03:51 +02:00
|
|
|
void Component::call_loop() { this->loop(); }
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2019-06-09 17:03:51 +02:00
|
|
|
void Component::call_setup() { this->setup(); }
|
2019-04-17 12:06:00 +02:00
|
|
|
uint32_t Component::get_component_state() const { return this->component_state_; }
|
2019-06-09 17:03:51 +02:00
|
|
|
void Component::call() {
|
|
|
|
uint32_t state = this->component_state_ & COMPONENT_STATE_MASK;
|
|
|
|
switch (state) {
|
|
|
|
case COMPONENT_STATE_CONSTRUCTION:
|
|
|
|
// State Construction: Call setup and set state to setup
|
|
|
|
this->component_state_ &= ~COMPONENT_STATE_MASK;
|
|
|
|
this->component_state_ |= COMPONENT_STATE_SETUP;
|
|
|
|
this->call_setup();
|
|
|
|
break;
|
|
|
|
case COMPONENT_STATE_SETUP:
|
|
|
|
// State setup: Call first loop and set state to loop
|
|
|
|
this->component_state_ &= ~COMPONENT_STATE_MASK;
|
|
|
|
this->component_state_ |= COMPONENT_STATE_LOOP;
|
|
|
|
this->call_loop();
|
|
|
|
break;
|
|
|
|
case COMPONENT_STATE_LOOP:
|
|
|
|
// State loop: Call loop
|
|
|
|
this->call_loop();
|
|
|
|
break;
|
|
|
|
case COMPONENT_STATE_FAILED:
|
|
|
|
// State failed: Do nothing
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2019-04-17 12:06:00 +02:00
|
|
|
}
|
2021-08-23 20:49:19 +02:00
|
|
|
const char *Component::get_component_source() const {
|
|
|
|
if (this->component_source_ == nullptr)
|
|
|
|
return "<unknown>";
|
|
|
|
return this->component_source_;
|
|
|
|
}
|
2019-04-17 12:06:00 +02:00
|
|
|
void Component::mark_failed() {
|
2021-08-23 20:49:19 +02:00
|
|
|
ESP_LOGE(TAG, "Component %s was marked as failed.", this->get_component_source());
|
2019-04-17 12:06:00 +02:00
|
|
|
this->component_state_ &= ~COMPONENT_STATE_MASK;
|
|
|
|
this->component_state_ |= COMPONENT_STATE_FAILED;
|
|
|
|
this->status_set_error();
|
|
|
|
}
|
2019-06-07 14:26:40 +02:00
|
|
|
void Component::defer(std::function<void()> &&f) { // NOLINT
|
|
|
|
App.scheduler.set_timeout(this, "", 0, std::move(f));
|
|
|
|
}
|
|
|
|
bool Component::cancel_defer(const std::string &name) { // NOLINT
|
|
|
|
return App.scheduler.cancel_timeout(this, name);
|
2019-04-17 12:06:00 +02:00
|
|
|
}
|
|
|
|
void Component::defer(const std::string &name, std::function<void()> &&f) { // NOLINT
|
2019-06-07 14:26:40 +02:00
|
|
|
App.scheduler.set_timeout(this, name, 0, std::move(f));
|
2019-04-17 12:06:00 +02:00
|
|
|
}
|
|
|
|
void Component::set_timeout(uint32_t timeout, std::function<void()> &&f) { // NOLINT
|
2019-06-07 14:26:40 +02:00
|
|
|
App.scheduler.set_timeout(this, "", timeout, std::move(f));
|
2019-04-17 12:06:00 +02:00
|
|
|
}
|
|
|
|
void Component::set_interval(uint32_t interval, std::function<void()> &&f) { // NOLINT
|
2019-10-18 15:33:18 +02:00
|
|
|
App.scheduler.set_interval(this, "", interval, std::move(f));
|
2019-04-17 12:06:00 +02:00
|
|
|
}
|
|
|
|
bool Component::is_failed() { return (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_FAILED; }
|
|
|
|
bool Component::can_proceed() { return true; }
|
|
|
|
bool Component::status_has_warning() { return this->component_state_ & STATUS_LED_WARNING; }
|
|
|
|
bool Component::status_has_error() { return this->component_state_ & STATUS_LED_ERROR; }
|
|
|
|
void Component::status_set_warning() {
|
|
|
|
this->component_state_ |= STATUS_LED_WARNING;
|
|
|
|
App.app_state_ |= STATUS_LED_WARNING;
|
|
|
|
}
|
|
|
|
void Component::status_set_error() {
|
|
|
|
this->component_state_ |= STATUS_LED_ERROR;
|
|
|
|
App.app_state_ |= STATUS_LED_ERROR;
|
|
|
|
}
|
|
|
|
void Component::status_clear_warning() { this->component_state_ &= ~STATUS_LED_WARNING; }
|
|
|
|
void Component::status_clear_error() { this->component_state_ &= ~STATUS_LED_ERROR; }
|
|
|
|
void Component::status_momentary_warning(const std::string &name, uint32_t length) {
|
|
|
|
this->status_set_warning();
|
|
|
|
this->set_timeout(name, length, [this]() { this->status_clear_warning(); });
|
|
|
|
}
|
|
|
|
void Component::status_momentary_error(const std::string &name, uint32_t length) {
|
|
|
|
this->status_set_error();
|
|
|
|
this->set_timeout(name, length, [this]() { this->status_clear_error(); });
|
|
|
|
}
|
|
|
|
void Component::dump_config() {}
|
|
|
|
float Component::get_actual_setup_priority() const {
|
2019-06-07 14:26:40 +02:00
|
|
|
if (isnan(this->setup_priority_override_))
|
|
|
|
return this->get_setup_priority();
|
|
|
|
return this->setup_priority_override_;
|
2019-04-17 12:06:00 +02:00
|
|
|
}
|
|
|
|
void Component::set_setup_priority(float priority) { this->setup_priority_override_ = priority; }
|
2019-12-07 18:28:55 +01:00
|
|
|
|
2019-12-04 16:03:37 +01:00
|
|
|
bool Component::has_overridden_loop() const {
|
|
|
|
#ifdef CLANG_TIDY
|
|
|
|
bool loop_overridden = true;
|
|
|
|
bool call_loop_overridden = true;
|
|
|
|
#else
|
2019-12-07 18:53:20 +01:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wpmf-conversions"
|
2019-12-04 16:03:37 +01:00
|
|
|
bool loop_overridden = (void *) (this->*(&Component::loop)) != (void *) (&Component::loop);
|
|
|
|
bool call_loop_overridden = (void *) (this->*(&Component::call_loop)) != (void *) (&Component::call_loop);
|
2019-12-07 18:53:20 +01:00
|
|
|
#pragma GCC diagnostic pop
|
2019-12-04 16:03:37 +01:00
|
|
|
#endif
|
|
|
|
return loop_overridden || call_loop_overridden;
|
|
|
|
}
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
PollingComponent::PollingComponent(uint32_t update_interval) : Component(), update_interval_(update_interval) {}
|
|
|
|
|
|
|
|
void PollingComponent::call_setup() {
|
|
|
|
// Let the polling component subclass setup their HW.
|
|
|
|
this->setup();
|
|
|
|
|
|
|
|
// Register interval.
|
|
|
|
this->set_interval("update", this->get_update_interval(), [this]() { this->update(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t PollingComponent::get_update_interval() const { return this->update_interval_; }
|
|
|
|
void PollingComponent::set_update_interval(uint32_t update_interval) { this->update_interval_ = update_interval; }
|
|
|
|
|
|
|
|
const std::string &Nameable::get_name() const { return this->name_; }
|
|
|
|
void Nameable::set_name(const std::string &name) {
|
|
|
|
this->name_ = name;
|
|
|
|
this->calc_object_id_();
|
|
|
|
}
|
2021-06-10 13:04:40 +02:00
|
|
|
Nameable::Nameable(std::string name) : name_(std::move(name)) { this->calc_object_id_(); }
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
const std::string &Nameable::get_object_id() { return this->object_id_; }
|
|
|
|
bool Nameable::is_internal() const { return this->internal_; }
|
|
|
|
void Nameable::set_internal(bool internal) { this->internal_ = internal; }
|
|
|
|
void Nameable::calc_object_id_() {
|
2020-07-14 18:45:42 +02:00
|
|
|
this->object_id_ = sanitize_string_allowlist(to_lowercase_underscore(this->name_), HOSTNAME_CHARACTER_ALLOWLIST);
|
2019-04-17 12:06:00 +02:00
|
|
|
// FNV-1 hash
|
|
|
|
this->object_id_hash_ = fnv1_hash(this->object_id_);
|
|
|
|
}
|
|
|
|
uint32_t Nameable::get_object_id_hash() { return this->object_id_hash_; }
|
|
|
|
|
2021-08-10 13:45:31 +12:00
|
|
|
bool Nameable::is_disabled_by_default() const { return this->disabled_by_default_; }
|
|
|
|
void Nameable::set_disabled_by_default(bool disabled_by_default) { this->disabled_by_default_ = disabled_by_default; }
|
|
|
|
|
2021-08-23 20:49:19 +02:00
|
|
|
WarnIfComponentBlockingGuard::WarnIfComponentBlockingGuard(Component *component) {
|
|
|
|
component_ = component;
|
|
|
|
started_ = millis();
|
|
|
|
}
|
|
|
|
WarnIfComponentBlockingGuard::~WarnIfComponentBlockingGuard() {
|
|
|
|
uint32_t now = millis();
|
|
|
|
if (now - started_ > 50) {
|
|
|
|
const char *src = component_ == nullptr ? "<null>" : component_->get_component_source();
|
|
|
|
ESP_LOGV(TAG, "Component %s took a long time for an operation (%.2f s).", src, (now - started_) / 1e3f);
|
|
|
|
ESP_LOGV(TAG, "Components should block for at most 20-30ms.");
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
} // namespace esphome
|