ESPHome  2024.8.3
mqtt_event.cpp
Go to the documentation of this file.
1 #include "mqtt_event.h"
2 #include "esphome/core/log.h"
3 
4 #include "mqtt_const.h"
5 
6 #ifdef USE_MQTT
7 #ifdef USE_EVENT
8 
9 namespace esphome {
10 namespace mqtt {
11 
12 static const char *const TAG = "mqtt.event";
13 
14 using namespace esphome::event;
15 
17 
19  JsonArray event_types = root.createNestedArray(MQTT_EVENT_TYPES);
20  for (const auto &event_type : this->event_->get_event_types())
21  event_types.add(event_type);
22 
23  if (!this->event_->get_device_class().empty())
24  root[MQTT_DEVICE_CLASS] = this->event_->get_device_class();
25 
26  config.command_topic = false;
27 }
28 
30  this->event_->add_on_event_callback([this](const std::string &event_type) { this->publish_event_(event_type); });
31 }
32 
34  ESP_LOGCONFIG(TAG, "MQTT Event '%s': ", this->event_->get_name().c_str());
35  ESP_LOGCONFIG(TAG, "Event Types: ");
36  for (const auto &event_type : this->event_->get_event_types()) {
37  ESP_LOGCONFIG(TAG, "- %s", event_type.c_str());
38  }
39  LOG_MQTT_COMPONENT(true, true);
40 }
41 
42 bool MQTTEventComponent::publish_event_(const std::string &event_type) {
43  return this->publish_json(this->get_state_topic_(),
44  [event_type](JsonObject root) { root[MQTT_EVENT_TYPE] = event_type; });
45 }
46 
47 std::string MQTTEventComponent::component_type() const { return "event"; }
48 const EntityBase *MQTTEventComponent::get_entity() const { return this->event_; }
49 
50 } // namespace mqtt
51 } // namespace esphome
52 
53 #endif
54 #endif // USE_MQTT
std::string get_device_class()
Get the device class, using the manual override if set.
Definition: entity_base.cpp:78
constexpr const char *const MQTT_EVENT_TYPES
Definition: mqtt_const.h:78
std::string component_type() const override
Definition: mqtt_event.cpp:47
std::set< std::string > get_event_types() const
Definition: event.h:28
MQTTEventComponent(event::Event *event)
Definition: mqtt_event.cpp:16
void add_on_event_callback(std::function< void(const std::string &event_type)> &&callback)
Definition: event.cpp:19
bool publish_json(const std::string &topic, const json::json_build_t &f)
Construct and send a JSON MQTT message.
bool command_topic
If the command topic should be included. Default to true.
const EntityBase * get_entity() const override
Definition: mqtt_event.cpp:48
Simple Helper struct used for Home Assistant MQTT send_discovery().
constexpr const char * c_str() const
Definition: string_ref.h:68
constexpr const char *const MQTT_EVENT_TYPE
Definition: mqtt_const.h:77
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
Definition: mqtt_event.cpp:18
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool publish_event_(const std::string &event_type)
Definition: mqtt_event.cpp:42
constexpr const char *const MQTT_DEVICE_CLASS
Definition: mqtt_const.h:57
std::string get_state_topic_() const
Get the MQTT topic that new states will be shared to.
const StringRef & get_name() const
Definition: entity_base.cpp:10