ESPHome 2025.10.0-dev
Loading...
Searching...
No Matches
binary_sensor.cpp
Go to the documentation of this file.
1#include "binary_sensor.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5
6namespace binary_sensor {
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_state_ de-dups and will only trigger callbacks if the state has actually changed
38 if (this->set_state_(new_state)) {
39 ESP_LOGD(TAG, "'%s': New state is %s", this->get_name().c_str(), ONOFF(new_state));
40 }
41}
42
44 filter->parent_ = this;
45 if (this->filter_list_ == nullptr) {
46 this->filter_list_ = filter;
47 } else {
48 Filter *last_filter = this->filter_list_;
49 while (last_filter->next_ != nullptr)
50 last_filter = last_filter->next_;
51 last_filter->next_ = filter;
52 }
53}
54void BinarySensor::add_filters(const std::vector<Filter *> &filters) {
55 for (Filter *filter : filters) {
56 this->add_filter(filter);
57 }
58}
59bool BinarySensor::is_status_binary_sensor() const { return false; }
60
61} // namespace binary_sensor
62
63} // namespace esphome
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(const std::vector< 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:19
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