Binary Sensor

In esphomelib, every component that exposes a binary state, is a BinarySensor.

To create your own binary sensor, simply subclass BinarySensor and call BinarySensor::publish_state() to tell the frontend that you have a new state. Inversion is automatically done for you when publishing state and can be changed by the user with BinarySensor::set_inverted().

Example Usage

// Basic
App.register_binary_sensor(custom_binary_sensor);
// GPIO Binary Sensor
App.make_gpio_binary_sensor("Window Open", 36);

See Application::register_binary_sensor() and Application::make_gpio_binary_sensor().

API Reference

BinarySensor

class binary_sensor::BinarySensor

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.

Inherits from Nameable

Subclassed by binary_sensor::ESP32TouchBinarySensor, binary_sensor::GPIOBinarySensorComponent, binary_sensor::StatusBinarySensor, binary_sensor::TemplateBinarySensor, ESP32BLEDevice, remote::RemoteReceiver

Public Functions

binary_sensor::BinarySensorBinarySensor(const std::string &name)

Construct a binary sensor with the specified name.

Parameters
  • name: Name of this binary sensor.

void binary_sensor::BinarySensoradd_on_state_callback(binary_callback_t &&callback)

Set callback for state changes.

Parameters
  • callback: The void(bool) callback.

void binary_sensor::BinarySensorpublish_state(bool state)

Publish a new state.

Inverted input is handled by this method and sub-classes don’t need to worry about inverting themselves.

Parameters
  • state: The new state.

bool binary_sensor::BinarySensorget_value() const

Get the current boolean value of this binary sensor.

void binary_sensor::BinarySensorset_device_class(const std::string &device_class)

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

std::string binary_sensor::BinarySensorget_device_class()

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

PressTrigger *binary_sensor::BinarySensormake_press_trigger()
ReleaseTrigger *binary_sensor::BinarySensormake_release_trigger()
ClickTrigger *binary_sensor::BinarySensormake_click_trigger(uint32_t min_length, uint32_t max_length)
DoubleClickTrigger *binary_sensor::BinarySensormake_double_click_trigger(uint32_t min_length, uint32_t max_length)
void binary_sensor::BinarySensoradd_filter(Filter *filter)
void binary_sensor::BinarySensoradd_filters(std::vector<Filter *> filters)
void binary_sensor::BinarySensorsend_value_(bool value)

Public Members

bool binary_sensor::BinarySensorvalue = {false}

Protected Functions

std::string binary_sensor::BinarySensordevice_class()

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

Protected Attributes

CallbackManager<void(bool)> binary_sensor::BinarySensorstate_callback_ = {}
optional<std::string> binary_sensor::BinarySensordevice_class_ = {}

Stores the override of the device class.

Filter *binary_sensor::BinarySensorfilter_list_ = {nullptr}

MQTTBinarySensorComponent

class binary_sensor::MQTTBinarySensorComponent

Simple MQTT front-end component for a binary_sensor.

After construction of this class, it should be connected to the BinarySensor by setting the callback returned by create_on_new_state_callback() in BinarySensor::on_new_state().

Inherits from mqtt::MQTTComponent

Public Functions

binary_sensor::MQTTBinarySensorComponentMQTTBinarySensorComponent(BinarySensor *binary_sensor)

Construct a MQTTBinarySensorComponent.

Parameters
  • binary_sensor: The binary sensor.

void binary_sensor::MQTTBinarySensorComponentsetup()

Send discovery.

void binary_sensor::MQTTBinarySensorComponentsend_discovery(JsonBuffer &buffer, JsonObject &obj, mqtt::SendDiscoveryConfig &config)

Send Home Assistant discovery info.

const std::string &binary_sensor::MQTTBinarySensorComponentget_payload_on() const

Get the payload this binary sensor uses for an ON value.

void binary_sensor::MQTTBinarySensorComponentset_payload_on(std::string payload_on)

Set the custom payload this binary sensor uses for an ON value.

const std::string &binary_sensor::MQTTBinarySensorComponentget_payload_off() const

Get the payload this binary sensor uses for an OFF value.

void binary_sensor::MQTTBinarySensorComponentset_payload_off(std::string payload_off)

Set the custom payload this binary sensor uses for an OFF value.

void binary_sensor::MQTTBinarySensorComponentsend_initial_state()
void binary_sensor::MQTTBinarySensorComponentpublish_state(bool state)
bool binary_sensor::MQTTBinarySensorComponentis_internal()

Protected Functions

std::string binary_sensor::MQTTBinarySensorComponentfriendly_name() const

Return the friendly name of this binary sensor.

std::string binary_sensor::MQTTBinarySensorComponentcomponent_type() const

“binary_sensor” component type.

Protected Attributes

BinarySensor *binary_sensor::MQTTBinarySensorComponentbinary_sensor_
std::string binary_sensor::MQTTBinarySensorComponentpayload_on_ = {"ON"}
std::string binary_sensor::MQTTBinarySensorComponentpayload_off_ = {"OFF"}