ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
binary_sensor.cpp
Go to the documentation of this file.
1#include "binary_sensor.h"
4#include "esphome/core/log.h"
5
7
8ESPHOME_LOG_TAG(TAG, "binary_sensor");
9
10// Function implementation of LOG_BINARY_SENSOR macro to reduce code size
11void log_binary_sensor(const char *tag, const char *prefix, const char *type, BinarySensor *obj) {
12 if (!log_entity_header(tag, prefix, type, obj)) {
13 return;
14 }
15 LOG_ENTITY_DEVICE_CLASS(tag, prefix, *obj);
16}
17
18void BinarySensor::publish_state(bool new_state) {
19#ifdef USE_BINARY_SENSOR_RAW_CALLBACK
20 this->raw_callback_.call(new_state);
21#endif
22#ifdef USE_BINARY_SENSOR_FILTER
23 if (this->filter_list_ == nullptr) {
24#endif
25 this->send_state_internal(new_state);
26#ifdef USE_BINARY_SENSOR_FILTER
27 } else {
28 this->filter_list_->input(new_state);
29 }
30#endif
31}
33 this->invalidate_state();
34 this->publish_state(new_state);
35}
36bool BinarySensor::set_new_state(const optional<bool> &new_state) {
37#ifdef USE_BINARY_SENSOR_FILTER
38 // An unknown state drops each filter's pending output and last value, so the next value is not dropped as a repeat
39 if (!new_state.has_value()) {
40 for (Filter *filter = this->filter_list_; filter != nullptr; filter = filter->next_)
41 filter->reset();
42 }
43#endif
44 if (StatefulEntityBase::set_new_state(new_state)) {
45 // weirdly, this file could be compiled even without USE_BINARY_SENSOR defined
46#if defined(USE_BINARY_SENSOR) && defined(USE_CONTROLLER_REGISTRY)
47 ControllerRegistry::notify_binary_sensor_update(this);
48#endif
49 ESP_LOGV(TAG, "'%s' >> %s", LOG_STR_ARG(this->get_log_name()), ONOFFMAYBE(new_state));
50 return true;
51 }
52 return false;
53}
54
55#ifdef USE_BINARY_SENSOR_FILTER
57 filter->parent_ = this;
58 if (this->filter_list_ == nullptr) {
59 this->filter_list_ = filter;
60 } else {
61 Filter *last_filter = this->filter_list_;
62 while (last_filter->next_ != nullptr)
63 last_filter = last_filter->next_;
64 last_filter->next_ = filter;
65 }
66}
67void BinarySensor::add_filters(std::initializer_list<Filter *> filters) {
68 for (Filter *filter : filters) {
69 this->add_filter(filter);
70 }
71}
72#endif // USE_BINARY_SENSOR_FILTER
73bool BinarySensor::is_status_binary_sensor() const { return false; }
74
75} // namespace esphome::binary_sensor
const LogString * get_log_name() const
Get the name for a "%s" log argument (LOG_STR_ARG).
virtual bool set_new_state(const optional< T > &new_state)
Apply a new state, de-duplicating and firing callbacks as needed.
Base class for all binary_sensor-type classes.
void add_filters(std::initializer_list< Filter * > filters)
void publish_state(bool new_state)
Publish a new state to the front-end.
void send_state_internal(bool new_state)
void publish_initial_state(bool new_state)
Publish the initial state, this will not make the callback manager send callbacks and is meant only f...
LazyCallbackManager< void(bool)> raw_callback_
bool set_new_state(const optional< bool > &new_state) override
virtual bool is_status_binary_sensor() const
Return whether this binary sensor has outputted a state.
virtual void input(bool value)
Definition filter.cpp:26
uint8_t type
void log_binary_sensor(const char *tag, const char *prefix, const char *type, BinarySensor *obj)
ESPHOME_LOG_TAG(TAG, "binary_sensor.automation")
bool log_entity_header(const char *tag, const char *prefix, const char *type, const EntityBase *obj)
Log "<prefix><type> '<name>'" for a dump_config header; returns false when obj is null.