ESPHome 2025.8.0b1
Loading...
Searching...
No Matches
binary_sensor.h
Go to the documentation of this file.
1#pragma once
2
6
7#include <vector>
8
9namespace esphome {
10
11namespace binary_sensor {
12
13#define LOG_BINARY_SENSOR(prefix, type, obj) \
14 if ((obj) != nullptr) { \
15 ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
16 if (!(obj)->get_device_class().empty()) { \
17 ESP_LOGCONFIG(TAG, "%s Device Class: '%s'", prefix, (obj)->get_device_class().c_str()); \
18 } \
19 }
20
21#define SUB_BINARY_SENSOR(name) \
22 protected: \
23 binary_sensor::BinarySensor *name##_binary_sensor_{nullptr}; \
24\
25 public: \
26 void set_##name##_binary_sensor(binary_sensor::BinarySensor *binary_sensor) { \
27 this->name##_binary_sensor_ = binary_sensor; \
28 }
29
37 public:
38 explicit BinarySensor(){};
39
44 void publish_state(bool new_state);
45
51 void publish_initial_state(bool new_state);
52
53 void add_filter(Filter *filter);
54 void add_filters(const std::vector<Filter *> &filters);
55
56 // ========== INTERNAL METHODS ==========
57 // (In most use cases you won't need these)
58 void send_state_internal(bool new_state);
59
61 virtual bool is_status_binary_sensor() const;
62
63 // For backward compatibility, provide an accessible property
64
65 bool state{};
66
67 protected:
69};
70
72 public:
73 bool has_state() const override { return true; }
74};
75
76} // namespace binary_sensor
77} // namespace esphome
An entity that has a state.
Base class for all binary_sensor-type classes.
void add_filters(const std::vector< 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...
virtual bool is_status_binary_sensor() const
Return whether this binary sensor has outputted a state.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7