ESPHome 2025.12.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
6namespace esphome {
7
8namespace binary_sensor {
9
10static const char *const TAG = "binary_sensor";
11
12// Function implementation of LOG_BINARY_SENSOR macro to reduce code size
13void log_binary_sensor(const char *tag, const char *prefix, const char *type, BinarySensor *obj) {
14 if (obj == nullptr) {
15 return;
16 }
17
18 ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());
19
20 if (!obj->get_device_class_ref().empty()) {
21 ESP_LOGCONFIG(tag, "%s Device Class: '%s'", prefix, obj->get_device_class_ref().c_str());
22 }
23}
24
25void BinarySensor::publish_state(bool new_state) {
26 if (this->filter_list_ == nullptr) {
27 this->send_state_internal(new_state);
28 } else {
29 this->filter_list_->input(new_state);
30 }
31}
33 this->invalidate_state();
34 this->publish_state(new_state);
35}
37 // copy the new state to the visible property for backwards compatibility, before any callbacks
38 this->state = new_state;
39 // Note that set_state_ de-dups and will only trigger callbacks if the state has actually changed
40 if (this->set_state_(new_state)) {
41 ESP_LOGD(TAG, "'%s': New state is %s", this->get_name().c_str(), ONOFF(new_state));
42#if defined(USE_BINARY_SENSOR) && defined(USE_CONTROLLER_REGISTRY)
44#endif
45 }
46}
47
49 filter->parent_ = this;
50 if (this->filter_list_ == nullptr) {
51 this->filter_list_ = filter;
52 } else {
53 Filter *last_filter = this->filter_list_;
54 while (last_filter->next_ != nullptr)
55 last_filter = last_filter->next_;
56 last_filter->next_ = filter;
57 }
58}
59void BinarySensor::add_filters(std::initializer_list<Filter *> filters) {
60 for (Filter *filter : filters) {
61 this->add_filter(filter);
62 }
63}
64bool BinarySensor::is_status_binary_sensor() const { return false; }
65
66} // namespace binary_sensor
67
68} // namespace esphome
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
bool set_state_(const optional< bool > &state)
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...
virtual bool is_status_binary_sensor() const
Return whether this binary sensor has outputted a state.
virtual void input(bool value)
Definition filter.cpp:18
uint16_t type
void log_binary_sensor(const char *tag, const char *prefix, const char *type, BinarySensor *obj)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7