ESPHome  2024.4.1
binary_sensor.cpp
Go to the documentation of this file.
1 #include "binary_sensor.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 
6 namespace binary_sensor {
7 
8 static const char *const TAG = "binary_sensor";
9 
10 void BinarySensor::add_on_state_callback(std::function<void(bool)> &&callback) {
11  this->state_callback_.add(std::move(callback));
12 }
13 
15  if (!this->publish_dedup_.next(state))
16  return;
17  if (this->filter_list_ == nullptr) {
18  this->send_state_internal(state, false);
19  } else {
20  this->filter_list_->input(state, false);
21  }
22 }
24  if (!this->publish_dedup_.next(state))
25  return;
26  if (this->filter_list_ == nullptr) {
27  this->send_state_internal(state, true);
28  } else {
29  this->filter_list_->input(state, true);
30  }
31 }
32 void BinarySensor::send_state_internal(bool state, bool is_initial) {
33  if (is_initial) {
34  ESP_LOGD(TAG, "'%s': Sending initial state %s", this->get_name().c_str(), ONOFF(state));
35  } else {
36  ESP_LOGD(TAG, "'%s': Sending state %s", this->get_name().c_str(), ONOFF(state));
37  }
38  this->has_state_ = true;
39  this->state = state;
40  if (!is_initial || this->publish_initial_state_) {
41  this->state_callback_.call(state);
42  }
43 }
44 
46 
48  filter->parent_ = this;
49  if (this->filter_list_ == nullptr) {
50  this->filter_list_ = filter;
51  } else {
52  Filter *last_filter = this->filter_list_;
53  while (last_filter->next_ != nullptr)
54  last_filter = last_filter->next_;
55  last_filter->next_ = filter;
56  }
57 }
58 void BinarySensor::add_filters(const std::vector<Filter *> &filters) {
59  for (Filter *filter : filters) {
60  this->add_filter(filter);
61  }
62 }
63 bool BinarySensor::has_state() const { return this->has_state_; }
64 bool BinarySensor::is_status_binary_sensor() const { return false; }
65 
66 } // namespace binary_sensor
67 
68 } // namespace esphome
void publish_initial_state(bool state)
Publish the initial state, this will not make the callback manager send callbacks and is meant only f...
bool next(T value)
Feeds the next item in the series to the deduplicator and returns whether this is a duplicate...
Definition: helpers.h:497
virtual bool is_status_binary_sensor() const
virtual bool has_state() const
Return whether this binary sensor has outputted a state.
void send_state_internal(bool state, bool is_initial)
void input(bool value, bool is_initial)
Definition: filter.cpp:22
bool state
The current reported state of the binary sensor.
Definition: binary_sensor.h:61
Deduplicator< bool > publish_dedup_
Definition: binary_sensor.h:82
void publish_state(bool state)
Publish a new state to the front-end.
BinarySensor * parent_
Definition: filter.h:27
void add_filters(const std::vector< Filter *> &filters)
CallbackManager< void(bool)> state_callback_
Definition: binary_sensor.h:78
void add_on_state_callback(std::function< void(bool)> &&callback)
Add a callback to be notified of state changes.
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
const StringRef & get_name() const
Definition: entity_base.cpp:10