ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
binary_sensor.h
Go to the documentation of this file.
1#pragma once
2
5#ifdef USE_BINARY_SENSOR_FILTER
7#endif
8
9#include <initializer_list>
10
11namespace esphome::binary_sensor {
12
13class BinarySensor;
14void log_binary_sensor(const char *tag, const char *prefix, const char *type, BinarySensor *obj);
15
16#define LOG_BINARY_SENSOR(prefix, type, obj) log_binary_sensor(TAG, prefix, LOG_STR_LITERAL(type), obj)
17
18#define SUB_BINARY_SENSOR(name) \
19 protected: \
20 binary_sensor::BinarySensor *name##_binary_sensor_{nullptr}; \
21\
22 public: \
23 void set_##name##_binary_sensor(binary_sensor::BinarySensor *binary_sensor) { \
24 this->name##_binary_sensor_ = binary_sensor; \
25 }
26
33class BinarySensor : public StatefulEntityBase<bool> {
34 public:
35 // User provided, not "= default": `new(p) BinarySensor()` would zero-fill .bss that is already zero.
36 explicit BinarySensor() {}
37
38 const bool &get_state() const override { return this->state; }
40
45 void publish_state(bool new_state);
46
52 void publish_initial_state(bool new_state);
53
54#ifdef USE_BINARY_SENSOR_FILTER
55 void add_filter(Filter *filter);
56 void add_filters(std::initializer_list<Filter *> filters);
57#endif
58
59#ifdef USE_BINARY_SENSOR_RAW_CALLBACK
62 template<typename F> void add_on_raw_state_callback(F &&callback) {
63 this->raw_callback_.add(std::forward<F>(callback));
64 }
65#endif
66
67 // ========== INTERNAL METHODS ==========
68 // (In most use cases you won't need these)
69 void send_state_internal(bool new_state) {
70 // Fast path: skip virtual dispatch when state hasn't changed
71 if (this->flags_.has_state && this->state == new_state)
72 return;
73 this->set_new_state(new_state);
74 }
75
77 virtual bool is_status_binary_sensor() const;
78
80 bool state{};
81
82 protected:
83 bool get_trigger_on_initial_state() const override { return this->trigger_on_initial_state_; }
84 void set_state_value(const bool &value) override { this->state = value; }
85
87#ifdef USE_BINARY_SENSOR_FILTER
89#endif
90#ifdef USE_BINARY_SENSOR_RAW_CALLBACK
92#endif
93
94 bool set_new_state(const optional<bool> &new_state) override;
95};
96
98 public:
100};
101
102} // namespace esphome::binary_sensor
struct esphome::EntityBase::EntityFlags flags_
void set_has_state(bool state)
Base class for entities that track a typed state value with change-detection and callbacks.
Base class for all binary_sensor-type classes.
void add_filters(std::initializer_list< Filter * > filters)
void publish_state(bool new_state)
Publish a new state to the front-end.
void send_state_internal(bool new_state)
bool state
The current state of this binary sensor. Also used as the backing storage for StatefulEntityBase.
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...
void set_trigger_on_initial_state(bool value)
const bool & get_state() const override
void add_on_raw_state_callback(F &&callback)
Add a callback that is called on every publish_state(), before filters and without de-duplication,...
void set_state_value(const bool &value) override
LazyCallbackManager< void(bool)> raw_callback_
bool set_new_state(const optional< bool > &new_state) override
bool get_trigger_on_initial_state() const override
virtual bool is_status_binary_sensor() const
Return whether this binary sensor has outputted a state.
uint8_t type
void log_binary_sensor(const char *tag, const char *prefix, const char *type, BinarySensor *obj)
uint32_t value
Definition hal.cpp:41