ESPHome 2025.9.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
10void BinarySensor::publish_state(bool new_state) {
11 if (this->filter_list_ == nullptr) {
12 this->send_state_internal(new_state);
13 } else {
14 this->filter_list_->input(new_state);
15 }
16}
18 this->invalidate_state();
19 this->publish_state(new_state);
20}
22 // copy the new state to the visible property for backwards compatibility, before any callbacks
23 this->state = new_state;
24 // Note that set_state_ de-dups and will only trigger callbacks if the state has actually changed
25 if (this->set_state_(new_state)) {
26 ESP_LOGD(TAG, "'%s': New state is %s", this->get_name().c_str(), ONOFF(new_state));
27 }
28}
29
31 filter->parent_ = this;
32 if (this->filter_list_ == nullptr) {
33 this->filter_list_ = filter;
34 } else {
35 Filter *last_filter = this->filter_list_;
36 while (last_filter->next_ != nullptr)
37 last_filter = last_filter->next_;
38 last_filter->next_ = filter;
39 }
40}
41void BinarySensor::add_filters(const std::vector<Filter *> &filters) {
42 for (Filter *filter : filters) {
43 this->add_filter(filter);
44 }
45}
46bool BinarySensor::is_status_binary_sensor() const { return false; }
47
48} // namespace binary_sensor
49
50} // namespace esphome
const StringRef & get_name() const
bool set_state_(const optional< bool > &state)
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
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7