ESPHome 2025.5.0
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
10void 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}
32void 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}
58void BinarySensor::add_filters(const std::vector<Filter *> &filters) {
59 for (Filter *filter : filters) {
60 this->add_filter(filter);
61 }
62}
63bool BinarySensor::has_state() const { return this->has_state_; }
64bool BinarySensor::is_status_binary_sensor() const { return false; }
65
66} // namespace binary_sensor
67
68} // namespace esphome
bool next(T value)
Feeds the next item in the series to the deduplicator and returns whether this is a duplicate.
Definition helpers.h:520
const StringRef & get_name() const
void add_filters(const std::vector< Filter * > &filters)
virtual bool has_state() const
Return whether this binary sensor has outputted a state.
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 state
The current reported state of the binary sensor.
CallbackManager< void(bool)> state_callback_
void send_state_internal(bool state, bool is_initial)
void add_on_state_callback(std::function< void(bool)> &&callback)
Add a callback to be notified of state changes.
virtual bool is_status_binary_sensor() const
void publish_state(bool state)
Publish a new state to the front-end.
void input(bool value, bool is_initial)
Definition filter.cpp:22
bool state
Definition fan.h:0
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7