ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
binary_sensor.h
Go to the documentation of this file.
1#pragma once
2
6
7#include <initializer_list>
8
10
11class BinarySensor;
12void log_binary_sensor(const char *tag, const char *prefix, const char *type, BinarySensor *obj);
13
14#define LOG_BINARY_SENSOR(prefix, type, obj) log_binary_sensor(TAG, prefix, LOG_STR_LITERAL(type), obj)
15
16#define SUB_BINARY_SENSOR(name) \
17 protected: \
18 binary_sensor::BinarySensor *name##_binary_sensor_{nullptr}; \
19\
20 public: \
21 void set_##name##_binary_sensor(binary_sensor::BinarySensor *binary_sensor) { \
22 this->name##_binary_sensor_ = binary_sensor; \
23 }
24
32 public:
33 explicit BinarySensor(){};
34
39 void publish_state(bool new_state);
40
46 void publish_initial_state(bool new_state);
47
48 void add_filter(Filter *filter);
49 void add_filters(std::initializer_list<Filter *> filters);
50
51 // ========== INTERNAL METHODS ==========
52 // (In most use cases you won't need these)
53 void send_state_internal(bool new_state);
54
56 virtual bool is_status_binary_sensor() const;
57
58 // For backward compatibility, provide an accessible property
59
60 bool state{};
61
62 protected:
64
65 bool set_new_state(const optional<bool> &new_state) override;
66};
67
69 public:
70 bool has_state() const override { return true; }
71};
72
73} // 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)