ESPHome  2024.10.2
binary_sensor.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "esphome/core/helpers.h"
7 
8 #include <vector>
9 
10 namespace esphome {
11 
12 namespace binary_sensor {
13 
14 #define LOG_BINARY_SENSOR(prefix, type, obj) \
15  if ((obj) != nullptr) { \
16  ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
17  if (!(obj)->get_device_class().empty()) { \
18  ESP_LOGCONFIG(TAG, "%s Device Class: '%s'", prefix, (obj)->get_device_class().c_str()); \
19  } \
20  }
21 
22 #define SUB_BINARY_SENSOR(name) \
23  protected: \
24  binary_sensor::BinarySensor *name##_binary_sensor_{nullptr}; \
25 \
26  public: \
27  void set_##name##_binary_sensor(binary_sensor::BinarySensor *binary_sensor) { \
28  this->name##_binary_sensor_ = binary_sensor; \
29  }
30 
38  public:
39  explicit BinarySensor();
40 
45  void add_on_state_callback(std::function<void(bool)> &&callback);
46 
51  void publish_state(bool state);
52 
58  void publish_initial_state(bool state);
59 
61  bool state;
62 
63  void add_filter(Filter *filter);
64  void add_filters(const std::vector<Filter *> &filters);
65 
66  void set_publish_initial_state(bool publish_initial_state) { this->publish_initial_state_ = publish_initial_state; }
67 
68  // ========== INTERNAL METHODS ==========
69  // (In most use cases you won't need these)
70  void send_state_internal(bool state, bool is_initial);
71 
73  virtual bool has_state() const;
74 
75  virtual bool is_status_binary_sensor() const;
76 
77  protected:
79  Filter *filter_list_{nullptr};
80  bool has_state_{false};
83 };
84 
86  public:
87  bool has_state() const override { return true; }
88 };
89 
90 } // namespace binary_sensor
91 } // 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...
virtual bool is_status_binary_sensor() const
void set_publish_initial_state(bool publish_initial_state)
Definition: binary_sensor.h:66
virtual bool has_state() const
Return whether this binary sensor has outputted a state.
void send_state_internal(bool state, bool is_initial)
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.
esphome::binary_sensor::BinarySensor * binary_sensor
Definition: statsd.h:40
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.
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37