ESPHome  2024.2.0
mqtt_component.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "esphome/core/defines.h"
4 
5 #ifdef USE_MQTT
6 
7 #include <memory>
8 
12 #include "mqtt_client.h"
13 
14 namespace esphome {
15 namespace mqtt {
16 
19  bool state_topic{true};
20  bool command_topic{true};
21 };
22 
23 #define LOG_MQTT_COMPONENT(state_topic, command_topic) \
24  if (state_topic) { \
25  ESP_LOGCONFIG(TAG, " State Topic: '%s'", this->get_state_topic_().c_str()); \
26  } \
27  if (command_topic) { \
28  ESP_LOGCONFIG(TAG, " Command Topic: '%s'", this->get_command_topic_().c_str()); \
29  }
30 
31 #define MQTT_COMPONENT_CUSTOM_TOPIC_(name, type) \
32  protected: \
33  std::string custom_##name##_##type##_topic_{}; \
34 \
35  public: \
36  void set_custom_##name##_##type##_topic(const std::string &topic) { this->custom_##name##_##type##_topic_ = topic; } \
37  std::string get_##name##_##type##_topic() const { \
38  if (this->custom_##name##_##type##_topic_.empty()) \
39  return this->get_default_topic_for_(#name "/" #type); \
40  return this->custom_##name##_##type##_topic_; \
41  }
42 
43 #define MQTT_COMPONENT_CUSTOM_TOPIC(name, type) MQTT_COMPONENT_CUSTOM_TOPIC_(name, type)
44 
61 class MQTTComponent : public Component {
62  public:
64  explicit MQTTComponent();
65 
67  void call_setup() override;
68 
69  void call_loop() override;
70 
71  void call_dump_config() override;
72 
74  virtual void send_discovery(JsonObject root, SendDiscoveryConfig &config) = 0;
75 
76  virtual bool send_initial_state() = 0;
77 
78  virtual bool is_internal();
79 
81  void set_retain(bool retain);
82  bool get_retain() const;
83 
85  void disable_discovery();
86  bool is_discovery_enabled() const;
87 
89  virtual std::string component_type() const = 0;
90 
92  void set_custom_state_topic(const char *custom_state_topic);
94  void set_custom_command_topic(const char *custom_command_topic);
96  void set_command_retain(bool command_retain);
97 
99  float get_setup_priority() const override;
100 
105  void set_availability(std::string topic, std::string payload_available, std::string payload_not_available);
106  void disable_availability();
107 
109  void schedule_resend_state();
110 
116  bool publish(const std::string &topic, const std::string &payload);
117 
123  bool publish_json(const std::string &topic, const json::json_build_t &f);
124 
131  void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos = 0);
132 
142  void subscribe_json(const std::string &topic, const mqtt_json_callback_t &callback, uint8_t qos = 0);
143 
144  protected:
146  std::string get_discovery_topic_(const MQTTDiscoveryInfo &discovery_info) const;
147 
153  std::string get_default_topic_for_(const std::string &suffix) const;
154 
158  virtual const EntityBase *get_entity() const = 0;
159 
165  virtual std::string unique_id();
166 
168  virtual std::string friendly_name() const;
169 
171  virtual std::string get_icon() const;
172 
174  virtual bool is_disabled_by_default() const;
175 
177  std::string get_state_topic_() const;
178 
180  std::string get_command_topic_() const;
181 
182  bool is_connected_() const;
183 
185  bool send_discovery_();
186 
187  // ========== INTERNAL METHODS ==========
188  // (In most use cases you won't need these)
190  std::string get_default_object_id_() const;
191 
192  StringRef custom_state_topic_{};
193  StringRef custom_command_topic_{};
194 
195  std::unique_ptr<Availability> availability_;
196 
197  bool has_custom_state_topic_{false};
198  bool has_custom_command_topic_{false};
199 
200  bool command_retain_{false};
201  bool retain_{true};
202  bool discovery_enabled_{true};
203  bool resend_state_{false};
204 };
205 
206 } // namespace mqtt
207 } // namespace esphome
208 
209 #endif // USE_MQTt
Internal struct for MQTT Home Assistant discovery.
Definition: mqtt_client.h:79
std::function< void(const std::string &, const std::string &)> mqtt_callback_t
Callback for MQTT subscriptions.
Definition: mqtt_client.h:35
bool state_topic
If the state topic should be included. Defaults to true.
StringRef is a reference to a string owned by something else.
Definition: string_ref.h:21
bool command_topic
If the command topic should be included. Default to true.
Simple Helper struct used for Home Assistant MQTT send_discovery().
std::function< void(JsonObject)> json_build_t
Callback function typedef for building JsonObjects.
Definition: json_util.h:20
std::unique_ptr< Availability > availability_
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
std::function< void(const std::string &, JsonObject)> mqtt_json_callback_t
Definition: mqtt_client.h:36
MQTTComponent is the base class for all components that interact with MQTT to expose certain function...