ESPHome 2026.11.0-dev
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_FILTER
20 if (this->filter_list_ == nullptr) {
21#endif
22 this->send_state_internal(new_state);
23#ifdef USE_BINARY_SENSOR_FILTER
24 } else {
25 this->filter_list_->input(new_state);
26 }
27#endif
28}
30 this->invalidate_state();
31 this->publish_state(new_state);
32}
33bool BinarySensor::set_new_state(const optional<bool> &new_state) {
34 if (StatefulEntityBase::set_new_state(new_state)) {
35 // weirdly, this file could be compiled even without USE_BINARY_SENSOR defined
36#if defined(USE_BINARY_SENSOR) && defined(USE_CONTROLLER_REGISTRY)
37 ControllerRegistry::notify_binary_sensor_update(this);
38#endif
39 ESP_LOGV(TAG, "'%s' >> %s", LOG_STR_ARG(this->get_log_name()), ONOFFMAYBE(new_state));
40 return true;
41 }
42 return false;
43}
44
45#ifdef USE_BINARY_SENSOR_FILTER
47 filter->parent_ = this;
48 if (this->filter_list_ == nullptr) {
49 this->filter_list_ = filter;
50 } else {
51 Filter *last_filter = this->filter_list_;
52 while (last_filter->next_ != nullptr)
53 last_filter = last_filter->next_;
54 last_filter->next_ = filter;
55 }
56}
57void BinarySensor::add_filters(std::initializer_list<Filter *> filters) {
58 for (Filter *filter : filters) {
59 this->add_filter(filter);
60 }
61}
62#endif // USE_BINARY_SENSOR_FILTER
63bool BinarySensor::is_status_binary_sensor() const { return false; }
64
65} // 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...
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:20
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.