ESPHome  2024.12.2
mqtt_binary_sensor.cpp
Go to the documentation of this file.
1 #include "mqtt_binary_sensor.h"
2 #include "esphome/core/log.h"
3 
4 #include "mqtt_const.h"
5 
6 #ifdef USE_MQTT
7 #ifdef USE_BINARY_SENSOR
8 
9 namespace esphome {
10 namespace mqtt {
11 
12 static const char *const TAG = "mqtt.binary_sensor";
13 
14 std::string MQTTBinarySensorComponent::component_type() const { return "binary_sensor"; }
16 
18  this->binary_sensor_->add_on_state_callback([this](bool state) { this->publish_state(state); });
19 }
20 
22  ESP_LOGCONFIG(TAG, "MQTT Binary Sensor '%s':", this->binary_sensor_->get_name().c_str());
23  LOG_MQTT_COMPONENT(true, false)
24 }
26  : binary_sensor_(binary_sensor) {
28  this->set_custom_state_topic(mqtt::global_mqtt_client->get_availability().topic.c_str());
29  }
30 }
31 
33  if (!this->binary_sensor_->get_device_class().empty())
36  root[MQTT_PAYLOAD_ON] = mqtt::global_mqtt_client->get_availability().payload_available;
38  root[MQTT_PAYLOAD_OFF] = mqtt::global_mqtt_client->get_availability().payload_not_available;
39  config.command_topic = false;
40 }
42  if (this->binary_sensor_->has_state()) {
43  return this->publish_state(this->binary_sensor_->state);
44  } else {
45  return true;
46  }
47 }
50  return true;
51 
52  const char *state_s = state ? "ON" : "OFF";
53  return this->publish(this->get_state_topic_(), state_s);
54 }
55 
56 } // namespace mqtt
57 } // namespace esphome
58 
59 #endif
60 #endif // USE_MQTT
std::string get_device_class()
Get the device class, using the manual override if set.
Definition: entity_base.cpp:78
virtual bool is_status_binary_sensor() const
void set_custom_state_topic(const char *custom_state_topic)
Set a custom state topic. Set to "" for default behavior.
bool command_topic
If the command topic should be included. Default to true.
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
constexpr const char *const MQTT_PAYLOAD_ON
Definition: mqtt_const.h:151
MQTTClientComponent * global_mqtt_client
constexpr const char *const MQTT_PAYLOAD_OFF
Definition: mqtt_const.h:149
const EntityBase * get_entity() const override
virtual bool has_state() const
Return whether this binary sensor has outputted a state.
Simple Helper struct used for Home Assistant MQTT send_discovery().
bool state
The current reported state of the binary sensor.
Definition: binary_sensor.h:61
std::string component_type() const override
esphome::binary_sensor::BinarySensor * binary_sensor
Definition: statsd.h:41
constexpr const char * c_str() const
Definition: string_ref.h:68
void add_on_state_callback(std::function< void(bool)> &&callback)
Add a callback to be notified of state changes.
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
binary_sensor::BinarySensor * binary_sensor_
constexpr const char *const MQTT_DEVICE_CLASS
Definition: mqtt_const.h:58
std::string get_state_topic_() const
Get the MQTT topic that new states will be shared to.
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
MQTTBinarySensorComponent(binary_sensor::BinarySensor *binary_sensor)
Construct a MQTTBinarySensorComponent.
const StringRef & get_name() const
Definition: entity_base.cpp:10
bool state
Definition: fan.h:34