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