ESPHome  2023.3.0
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 std::string BinarySensor::device_class() { return ""; }
48  if (this->device_class_.has_value())
49  return *this->device_class_;
50 #pragma GCC diagnostic push
51 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
52  return this->device_class();
53 #pragma GCC diagnostic pop
54 }
56  filter->parent_ = this;
57  if (this->filter_list_ == nullptr) {
58  this->filter_list_ = filter;
59  } else {
60  Filter *last_filter = this->filter_list_;
61  while (last_filter->next_ != nullptr)
62  last_filter = last_filter->next_;
63  last_filter->next_ = filter;
64  }
65 }
66 void BinarySensor::add_filters(const std::vector<Filter *> &filters) {
67  for (Filter *filter : filters) {
68  this->add_filter(filter);
69  }
70 }
71 bool BinarySensor::has_state() const { return this->has_state_; }
72 bool BinarySensor::is_status_binary_sensor() const { return false; }
73 
74 } // namespace binary_sensor
75 
76 } // 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:490
virtual bool is_status_binary_sensor() const
const std::string & get_name() const
Definition: entity_base.cpp:11
bool has_value() const
Definition: optional.h:87
virtual std::string device_class()
Override this to set the default device class.
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:97
void publish_state(bool state)
Publish a new state to the front-end.
void set_device_class(const std::string &device_class)
Manually set the Home Assistant device class (see binary_sensor::device_class)
BinarySensor * parent_
Definition: filter.h:26
void add_filters(const std::vector< Filter *> &filters)
CallbackManager< void(bool)> state_callback_
Definition: binary_sensor.h:92
void add_on_state_callback(std::function< void(bool)> &&callback)
Add a callback to be notified of state changes.
Definition: a4988.cpp:4
std::string get_device_class()
Get the device class for this binary sensor, using the manual override if specified.
optional< std::string > device_class_
Stores the override of the device class.
Definition: binary_sensor.h:93