Files
esphome/esphome/components/binary_sensor/binary_sensor.cpp
T
J. Nick Koston 5b8a1623b0 [core] Move entity state publish and setting logs to VERBOSE level
Benchmarking in #15034 showed that esp_log_printf and vsnprintf_chk
dominate sensor publish time (95%+ of internal_send_state_to_frontend).
At the default DEBUG log level, every state change generates log output
that overwhelms the log output making it hard to actually debug and
wastes significant CPU time on string formatting.

Move all entity state publish (>>) and setting logs from ESP_LOGD to
ESP_LOGV across all base entity types: sensor, binary_sensor, switch,
number, text_sensor, select, text, event, lock, cover, valve, climate,
fan, light, media_player, alarm_control_panel, datetime, update, and
water_heater.
2026-03-24 18:13:09 -10:00

75 lines
2.3 KiB
C++

#include "binary_sensor.h"
#include "esphome/core/defines.h"
#include "esphome/core/controller_registry.h"
#include "esphome/core/log.h"
namespace esphome::binary_sensor {
static const char *const TAG = "binary_sensor";
// Function implementation of LOG_BINARY_SENSOR macro to reduce code size
void log_binary_sensor(const char *tag, const char *prefix, const char *type, BinarySensor *obj) {
if (obj == nullptr) {
return;
}
ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());
LOG_ENTITY_DEVICE_CLASS(tag, prefix, *obj);
}
void BinarySensor::publish_state(bool new_state) {
#ifdef USE_BINARY_SENSOR_FILTER
if (this->filter_list_ == nullptr) {
#endif
this->send_state_internal(new_state);
#ifdef USE_BINARY_SENSOR_FILTER
} else {
this->filter_list_->input(new_state);
}
#endif
}
void BinarySensor::publish_initial_state(bool new_state) {
this->invalidate_state();
this->publish_state(new_state);
}
void BinarySensor::send_state_internal(bool new_state) {
// copy the new state to the visible property for backwards compatibility, before any callbacks
this->state = new_state;
// Note that set_new_state_ de-dups and will only trigger callbacks if the state has actually changed
this->set_new_state(new_state);
}
bool BinarySensor::set_new_state(const optional<bool> &new_state) {
if (StatefulEntityBase::set_new_state(new_state)) {
// weirdly, this file could be compiled even without USE_BINARY_SENSOR defined
#if defined(USE_BINARY_SENSOR) && defined(USE_CONTROLLER_REGISTRY)
ControllerRegistry::notify_binary_sensor_update(this);
#endif
ESP_LOGV(TAG, "'%s' >> %s", this->get_name().c_str(), ONOFFMAYBE(new_state));
return true;
}
return false;
}
#ifdef USE_BINARY_SENSOR_FILTER
void BinarySensor::add_filter(Filter *filter) {
filter->parent_ = this;
if (this->filter_list_ == nullptr) {
this->filter_list_ = filter;
} else {
Filter *last_filter = this->filter_list_;
while (last_filter->next_ != nullptr)
last_filter = last_filter->next_;
last_filter->next_ = filter;
}
}
void BinarySensor::add_filters(std::initializer_list<Filter *> filters) {
for (Filter *filter : filters) {
this->add_filter(filter);
}
}
#endif // USE_BINARY_SENSOR_FILTER
bool BinarySensor::is_status_binary_sensor() const { return false; }
} // namespace esphome::binary_sensor