ESPHome  2022.9.2
binary_sensor.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "esphome/core/helpers.h"
7 
8 namespace esphome {
9 
10 namespace binary_sensor {
11 
12 #define LOG_BINARY_SENSOR(prefix, type, obj) \
13  if ((obj) != nullptr) { \
14  ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
15  if (!(obj)->get_device_class().empty()) { \
16  ESP_LOGCONFIG(TAG, "%s Device Class: '%s'", prefix, (obj)->get_device_class().c_str()); \
17  } \
18  }
19 
26 class BinarySensor : public EntityBase {
27  public:
28  explicit BinarySensor();
29 
34  void add_on_state_callback(std::function<void(bool)> &&callback);
35 
40  void publish_state(bool state);
41 
47  void publish_initial_state(bool state);
48 
50  bool state;
51 
53  void set_device_class(const std::string &device_class);
54 
56  std::string get_device_class();
57 
58  void add_filter(Filter *filter);
59  void add_filters(const std::vector<Filter *> &filters);
60 
61  void set_publish_initial_state(bool publish_initial_state) { this->publish_initial_state_ = publish_initial_state; }
62 
63  // ========== INTERNAL METHODS ==========
64  // (In most use cases you won't need these)
65  void send_state_internal(bool state, bool is_initial);
66 
68  virtual bool has_state() const;
69 
70  virtual bool is_status_binary_sensor() const;
71 
72  // ========== OVERRIDE METHODS ==========
73  // (You'll only need this when creating your own custom binary sensor)
78  virtual std::string device_class();
79 
80  protected:
83  Filter *filter_list_{nullptr};
84  bool has_state_{false};
87 };
88 
90  public:
91  bool has_state() const override { return true; }
92 };
93 
94 } // namespace binary_sensor
95 } // 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:61
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)
bool state
The current reported state of the binary sensor.
Definition: binary_sensor.h:50
Deduplicator< bool > publish_dedup_
Definition: binary_sensor.h:86
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)
void add_filters(const std::vector< Filter *> &filters)
CallbackManager< void(bool)> state_callback_
Definition: binary_sensor.h:81
void add_on_state_callback(std::function< void(bool)> &&callback)
Add a callback to be notified of state changes.
Definition: a4988.cpp:4
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:26
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:82