ESPHome  2022.12.8
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 
28 class BinarySensor : public EntityBase {
29  public:
30  explicit BinarySensor();
31 
36  void add_on_state_callback(std::function<void(bool)> &&callback);
37 
42  void publish_state(bool state);
43 
49  void publish_initial_state(bool state);
50 
52  bool state;
53 
55  void set_device_class(const std::string &device_class);
56 
58  std::string get_device_class();
59 
60  void add_filter(Filter *filter);
61  void add_filters(const std::vector<Filter *> &filters);
62 
63  void set_publish_initial_state(bool publish_initial_state) { this->publish_initial_state_ = publish_initial_state; }
64 
65  // ========== INTERNAL METHODS ==========
66  // (In most use cases you won't need these)
67  void send_state_internal(bool state, bool is_initial);
68 
70  virtual bool has_state() const;
71 
72  virtual bool is_status_binary_sensor() const;
73 
74  // ========== OVERRIDE METHODS ==========
75  // (You'll only need this when creating your own custom binary sensor)
80  virtual std::string device_class();
81 
82  protected:
85  Filter *filter_list_{nullptr};
86  bool has_state_{false};
89 };
90 
92  public:
93  bool has_state() const override { return true; }
94 };
95 
96 } // namespace binary_sensor
97 } // 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:63
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:52
Deduplicator< bool > publish_dedup_
Definition: binary_sensor.h:88
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:83
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:28
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:84