ESPHome  1.15.3
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
esphome::binary_sensor::BinarySensor Class Reference

Base class for all binary_sensor-type classes. More...

#include <binary_sensor.h>

Inheritance diagram for esphome::binary_sensor::BinarySensor:
esphome::Nameable esphome::binary_sensor::BinarySensorInitiallyOff esphome::esp32_touch::ESP32TouchBinarySensor esphome::gpio::GPIOBinarySensor esphome::homeassistant::HomeassistantBinarySensor esphome::mpr121::MPR121Channel esphome::pn532::PN532BinarySensor esphome::status::StatusBinarySensor esphome::sx1509::SX1509BinarySensor esphome::template_::TemplateBinarySensor esphome::ttp229_bsf::TTP229BSFChannel esphome::ttp229_lsf::TTP229Channel esphome::tuya::TuyaBinarySensor

Public Member Functions

 BinarySensor ()
 
 BinarySensor (const std::string &name)
 Construct a binary sensor with the specified name. More...
 
void add_on_state_callback (std::function< void(bool)> &&callback)
 Add a callback to be notified of state changes. More...
 
void publish_state (bool state)
 Publish a new state to the front-end. More...
 
void publish_initial_state (bool state)
 Publish the initial state, this will not make the callback manager send callbacks and is meant only for the initial state on boot. More...
 
void set_device_class (const std::string &device_class)
 Manually set the Home Assistant device class (see binary_sensor::device_class) More...
 
std::string get_device_class ()
 Get the device class for this binary sensor, using the manual override if specified. More...
 
void add_filter (Filter *filter)
 
void add_filters (std::vector< Filter *> filters)
 
void send_state_internal (bool state, bool is_initial)
 
virtual bool has_state () const
 Return whether this binary sensor has outputted a state. More...
 
virtual bool is_status_binary_sensor () const
 
virtual std::string device_class ()
 Get the default device class for this sensor, or empty string for no default. More...
 
- Public Member Functions inherited from esphome::Nameable
 Nameable ()
 
 Nameable (const std::string &name)
 
const std::string & get_name () const
 
void set_name (const std::string &name)
 
const std::string & get_object_id ()
 Get the sanitized name of this nameable as an ID. Caching it internally. More...
 
uint32_t get_object_id_hash ()
 
bool is_internal () const
 
void set_internal (bool internal)
 

Data Fields

bool state
 The current reported state of the binary sensor. More...
 

Protected Member Functions

uint32_t hash_base () override
 
- Protected Member Functions inherited from esphome::Nameable
void calc_object_id_ ()
 

Protected Attributes

CallbackManager< void(bool)> state_callback_ {}
 
optional< std::string > device_class_ {}
 Stores the override of the device class. More...
 
Filterfilter_list_ {nullptr}
 
bool has_state_ {false}
 
Deduplicator< bool > publish_dedup_
 
- Protected Attributes inherited from esphome::Nameable
std::string name_
 
std::string object_id_
 
uint32_t object_id_hash_
 
bool internal_ {false}
 

Detailed Description

Base class for all binary_sensor-type classes.

This class includes a callback that components such as MQTT can subscribe to for state changes. The sub classes should notify the front-end of new states via the publish_state() method which handles inverted inputs for you.

Definition at line 25 of file binary_sensor.h.

Constructor & Destructor Documentation

◆ BinarySensor() [1/2]

esphome::binary_sensor::BinarySensor::BinarySensor ( )
explicit

Definition at line 46 of file binary_sensor.cpp.

◆ BinarySensor() [2/2]

esphome::binary_sensor::BinarySensor::BinarySensor ( const std::string &  name)
explicit

Construct a binary sensor with the specified name.

Parameters
nameName of this binary sensor.

Definition at line 45 of file binary_sensor.cpp.

Member Function Documentation

◆ add_filter()

void esphome::binary_sensor::BinarySensor::add_filter ( Filter filter)

Definition at line 53 of file binary_sensor.cpp.

◆ add_filters()

void esphome::binary_sensor::BinarySensor::add_filters ( std::vector< Filter *>  filters)

Definition at line 64 of file binary_sensor.cpp.

◆ add_on_state_callback()

void esphome::binary_sensor::BinarySensor::add_on_state_callback ( std::function< void(bool)> &&  callback)

Add a callback to be notified of state changes.

Parameters
callbackThe void(bool) callback.

Definition at line 10 of file binary_sensor.cpp.

◆ device_class()

std::string esphome::binary_sensor::BinarySensor::device_class ( )
virtual

Get the default device class for this sensor, or empty string for no default.

Definition at line 44 of file binary_sensor.cpp.

◆ get_device_class()

std::string esphome::binary_sensor::BinarySensor::get_device_class ( )

Get the device class for this binary sensor, using the manual override if specified.

Definition at line 48 of file binary_sensor.cpp.

◆ has_state()

bool esphome::binary_sensor::BinarySensor::has_state ( ) const
virtual

Return whether this binary sensor has outputted a state.

Reimplemented in esphome::binary_sensor::BinarySensorInitiallyOff.

Definition at line 69 of file binary_sensor.cpp.

◆ hash_base()

uint32_t esphome::binary_sensor::BinarySensor::hash_base ( )
overrideprotectedvirtual

Implements esphome::Nameable.

Definition at line 70 of file binary_sensor.cpp.

◆ is_status_binary_sensor()

bool esphome::binary_sensor::BinarySensor::is_status_binary_sensor ( ) const
virtual

Reimplemented in esphome::status::StatusBinarySensor.

Definition at line 71 of file binary_sensor.cpp.

◆ publish_initial_state()

void esphome::binary_sensor::BinarySensor::publish_initial_state ( bool  state)

Publish the initial state, this will not make the callback manager send callbacks and is meant only for the initial state on boot.

Parameters
stateThe new state.

Definition at line 23 of file binary_sensor.cpp.

◆ publish_state()

void esphome::binary_sensor::BinarySensor::publish_state ( bool  state)

Publish a new state to the front-end.

Parameters
stateThe new state.

Definition at line 14 of file binary_sensor.cpp.

◆ send_state_internal()

void esphome::binary_sensor::BinarySensor::send_state_internal ( bool  state,
bool  is_initial 
)

Definition at line 32 of file binary_sensor.cpp.

◆ set_device_class()

void esphome::binary_sensor::BinarySensor::set_device_class ( const std::string &  device_class)

Manually set the Home Assistant device class (see binary_sensor::device_class)

Definition at line 47 of file binary_sensor.cpp.

Field Documentation

◆ device_class_

optional<std::string> esphome::binary_sensor::BinarySensor::device_class_ {}
protected

Stores the override of the device class.

Definition at line 83 of file binary_sensor.h.

◆ filter_list_

Filter* esphome::binary_sensor::BinarySensor::filter_list_ {nullptr}
protected

Definition at line 84 of file binary_sensor.h.

◆ has_state_

bool esphome::binary_sensor::BinarySensor::has_state_ {false}
protected

Definition at line 85 of file binary_sensor.h.

◆ publish_dedup_

Deduplicator<bool> esphome::binary_sensor::BinarySensor::publish_dedup_
protected

Definition at line 86 of file binary_sensor.h.

◆ state

bool esphome::binary_sensor::BinarySensor::state

The current reported state of the binary sensor.

Definition at line 54 of file binary_sensor.h.

◆ state_callback_

CallbackManager<void(bool)> esphome::binary_sensor::BinarySensor::state_callback_ {}
protected

Definition at line 82 of file binary_sensor.h.


The documentation for this class was generated from the following files: