ESPHome 2026.2.1
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 if (this->filter_list_ == nullptr) {
22 this->send_state_internal(new_state);
23 } else {
24 this->filter_list_->input(new_state);
25 }
26}
28 this->invalidate_state();
29 this->publish_state(new_state);
30}
32 // copy the new state to the visible property for backwards compatibility, before any callbacks
33 this->state = new_state;
34 // Note that set_new_state_ de-dups and will only trigger callbacks if the state has actually changed
35 this->set_new_state(new_state);
36}
37
39 if (StatefulEntityBase::set_new_state(new_state)) {
40 // weirdly, this file could be compiled even without USE_BINARY_SENSOR defined
41#if defined(USE_BINARY_SENSOR) && defined(USE_CONTROLLER_REGISTRY)
43#endif
44 ESP_LOGD(TAG, "'%s' >> %s", this->get_name().c_str(), ONOFFMAYBE(new_state));
45 return true;
46 }
47 return false;
48}
49
51 filter->parent_ = this;
52 if (this->filter_list_ == nullptr) {
53 this->filter_list_ = filter;
54 } else {
55 Filter *last_filter = this->filter_list_;
56 while (last_filter->next_ != nullptr)
57 last_filter = last_filter->next_;
58 last_filter->next_ = filter;
59 }
60}
61void BinarySensor::add_filters(std::initializer_list<Filter *> filters) {
62 for (Filter *filter : filters) {
63 this->add_filter(filter);
64 }
65}
66bool BinarySensor::is_status_binary_sensor() const { return false; }
67
68} // 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:24
uint16_t type
void log_binary_sensor(const char *tag, const char *prefix, const char *type, BinarySensor *obj)