ESPHome 2026.3.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
8static const char *const 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 (obj == nullptr) {
13 return;
14 }
15
16 ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());
17 LOG_ENTITY_DEVICE_CLASS(tag, prefix, *obj);
18}
19
20void BinarySensor::publish_state(bool new_state) {
21#ifdef USE_BINARY_SENSOR_FILTER
22 if (this->filter_list_ == nullptr) {
23#endif
24 this->send_state_internal(new_state);
25#ifdef USE_BINARY_SENSOR_FILTER
26 } else {
27 this->filter_list_->input(new_state);
28 }
29#endif
30}
32 this->invalidate_state();
33 this->publish_state(new_state);
34}
36 // copy the new state to the visible property for backwards compatibility, before any callbacks
37 this->state = new_state;
38 // Note that set_new_state_ de-dups and will only trigger callbacks if the state has actually changed
39 this->set_new_state(new_state);
40}
41
43 if (StatefulEntityBase::set_new_state(new_state)) {
44 // weirdly, this file could be compiled even without USE_BINARY_SENSOR defined
45#if defined(USE_BINARY_SENSOR) && defined(USE_CONTROLLER_REGISTRY)
47#endif
48 ESP_LOGD(TAG, "'%s' >> %s", this->get_name().c_str(), ONOFFMAYBE(new_state));
49 return true;
50 }
51 return false;
52}
53
54#ifdef USE_BINARY_SENSOR_FILTER
56 filter->parent_ = this;
57 if (this->filter_list_ == nullptr) {
58 this->filter_list_ = filter;
59 } else {
60 Filter *last_filter = this->filter_list_;
61 while (last_filter->next_ != nullptr)
62 last_filter = last_filter->next_;
63 last_filter->next_ = filter;
64 }
65}
66void BinarySensor::add_filters(std::initializer_list<Filter *> filters) {
67 for (Filter *filter : filters) {
68 this->add_filter(filter);
69 }
70}
71#endif // USE_BINARY_SENSOR_FILTER
72bool BinarySensor::is_status_binary_sensor() const { return false; }
73
74} // namespace esphome::binary_sensor
static void notify_binary_sensor_update(binary_sensor::BinarySensor *obj)
const StringRef & get_name() const
virtual bool set_new_state(const optional< T > &new_state)
Set a new state for this entity.
constexpr const char * c_str() const
Definition string_ref.h:73
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 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:27
uint16_t type
void log_binary_sensor(const char *tag, const char *prefix, const char *type, BinarySensor *obj)