ESPHome  2022.9.2
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 
11 #include "mqtt_client.h"
12 
13 namespace esphome {
14 namespace mqtt {
15 
18  bool state_topic{true};
19  bool command_topic{true};
20 };
21 
22 #define LOG_MQTT_COMPONENT(state_topic, command_topic) \
23  if (state_topic) { \
24  ESP_LOGCONFIG(TAG, " State Topic: '%s'", this->get_state_topic_().c_str()); \
25  } \
26  if (command_topic) { \
27  ESP_LOGCONFIG(TAG, " Command Topic: '%s'", this->get_command_topic_().c_str()); \
28  }
29 
30 #define MQTT_COMPONENT_CUSTOM_TOPIC_(name, type) \
31  protected: \
32  std::string custom_##name##_##type##_topic_{}; \
33 \
34  public: \
35  void set_custom_##name##_##type##_topic(const std::string &topic) { this->custom_##name##_##type##_topic_ = topic; } \
36  std::string get_##name##_##type##_topic() const { \
37  if (this->custom_##name##_##type##_topic_.empty()) \
38  return this->get_default_topic_for_(#name "/" #type); \
39  return this->custom_##name##_##type##_topic_; \
40  }
41 
42 #define MQTT_COMPONENT_CUSTOM_TOPIC(name, type) MQTT_COMPONENT_CUSTOM_TOPIC_(name, type)
43 
60 class MQTTComponent : public Component {
61  public:
63  explicit MQTTComponent();
64 
66  void call_setup() override;
67 
68  void call_loop() override;
69 
70  void call_dump_config() override;
71 
73  virtual void send_discovery(JsonObject root, SendDiscoveryConfig &config) = 0;
74 
75  virtual bool send_initial_state() = 0;
76 
77  virtual bool is_internal();
78 
80  void set_retain(bool retain);
81  bool get_retain() const;
82 
84  void disable_discovery();
85  bool is_discovery_enabled() const;
86 
88  virtual std::string component_type() const = 0;
89 
91  void set_custom_state_topic(const std::string &custom_state_topic);
93  void set_custom_command_topic(const std::string &custom_command_topic);
95  void set_command_retain(bool command_retain);
96 
98  float get_setup_priority() const override;
99 
104  void set_availability(std::string topic, std::string payload_available, std::string payload_not_available);
105  void disable_availability();
106 
108  void schedule_resend_state();
109 
115  bool publish(const std::string &topic, const std::string &payload);
116 
122  bool publish_json(const std::string &topic, const json::json_build_t &f);
123 
130  void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos = 0);
131 
141  void subscribe_json(const std::string &topic, const mqtt_json_callback_t &callback, uint8_t qos = 0);
142 
143  protected:
145  std::string get_discovery_topic_(const MQTTDiscoveryInfo &discovery_info) const;
146 
152  std::string get_default_topic_for_(const std::string &suffix) const;
153 
157  virtual const EntityBase *get_entity() const = 0;
158 
164  virtual std::string unique_id();
165 
167  virtual std::string friendly_name() const;
168 
170  virtual std::string get_icon() const;
171 
173  virtual bool is_disabled_by_default() const;
174 
176  std::string get_state_topic_() const;
177 
179  std::string get_command_topic_() const;
180 
181  bool is_connected_() const;
182 
184  bool send_discovery_();
185 
186  // ========== INTERNAL METHODS ==========
187  // (In most use cases you won't need these)
189  std::string get_default_object_id_() const;
190 
191  std::string custom_state_topic_{};
192  std::string custom_command_topic_{};
193  bool command_retain_{false};
194  bool retain_{true};
195  bool discovery_enabled_{true};
196  std::unique_ptr<Availability> availability_;
197  bool resend_state_{false};
198 };
199 
200 } // namespace mqtt
201 } // namespace esphome
202 
203 #endif // USE_MQTt
Internal struct for MQTT Home Assistant discovery.
Definition: mqtt_client.h:75
std::function< void(const std::string &, const std::string &)> mqtt_callback_t
Callback for MQTT subscriptions.
Definition: mqtt_client.h:31
bool state_topic
If the state topic should be included. Defaults to true.
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_
Definition: a4988.cpp:4
std::function< void(const std::string &, JsonObject)> mqtt_json_callback_t
Definition: mqtt_client.h:32
MQTTComponent is the base class for all components that interact with MQTT to expose certain function...