ESPHome 2026.3.0-dev
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
34 public:
35 explicit BinarySensor(){};
36
41 void publish_state(bool new_state);
42
48 void publish_initial_state(bool new_state);
49
50#ifdef USE_BINARY_SENSOR_FILTER
51 void add_filter(Filter *filter);
52 void add_filters(std::initializer_list<Filter *> filters);
53#endif
54
55 // ========== INTERNAL METHODS ==========
56 // (In most use cases you won't need these)
57 void send_state_internal(bool new_state);
58
60 virtual bool is_status_binary_sensor() const;
61
62 // For backward compatibility, provide an accessible property
63
64 bool state{};
65
66 protected:
67#ifdef USE_BINARY_SENSOR_FILTER
69#endif
70
71 bool set_new_state(const optional<bool> &new_state) override;
72};
73
75 public:
76 bool has_state() const override { return true; }
77};
78
79} // namespace esphome::binary_sensor
An entity that has a state.
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 publish_initial_state(bool new_state)
Publish the initial state, this will not make the callback manager send callbacks and is meant only f...
bool set_new_state(const optional< bool > &new_state) override
virtual bool is_status_binary_sensor() const
Return whether this binary sensor has outputted a state.
uint16_t type
void log_binary_sensor(const char *tag, const char *prefix, const char *type, BinarySensor *obj)