ESPHome 2025.5.0
Loading...
Searching...
No Matches
mqtt_component.h
Go to the documentation of this file.
1#pragma once
2
4
5#ifdef USE_MQTT
6
7#include <memory>
8
12#include "mqtt_client.h"
13
14namespace esphome {
15namespace 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
61class 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_qos(uint8_t qos);
82 uint8_t get_qos() const;
83
85 void set_retain(bool retain);
86 bool get_retain() const;
87
89 void disable_discovery();
90 bool is_discovery_enabled() const;
91
93 void set_subscribe_qos(uint8_t qos);
94
96 virtual std::string component_type() const = 0;
97
99 void set_custom_state_topic(const char *custom_state_topic);
101 void set_custom_command_topic(const char *custom_command_topic);
103 void set_command_retain(bool command_retain);
104
106 float get_setup_priority() const override;
107
112 void set_availability(std::string topic, std::string payload_available, std::string payload_not_available);
114
117
123 bool publish(const std::string &topic, const std::string &payload);
124
130 bool publish_json(const std::string &topic, const json::json_build_t &f);
131
138 void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos = 0);
139
149 void subscribe_json(const std::string &topic, const mqtt_json_callback_t &callback, uint8_t qos = 0);
150
151 protected:
153 std::string get_discovery_topic_(const MQTTDiscoveryInfo &discovery_info) const;
154
160 std::string get_default_topic_for_(const std::string &suffix) const;
161
165 virtual const EntityBase *get_entity() const = 0;
166
172 virtual std::string unique_id();
173
175 virtual std::string friendly_name() const;
176
178 virtual std::string get_icon() const;
179
181 virtual bool is_disabled_by_default() const;
182
184 std::string get_state_topic_() const;
185
187 std::string get_command_topic_() const;
188
189 bool is_connected_() const;
190
192 bool send_discovery_();
193
194 // ========== INTERNAL METHODS ==========
195 // (In most use cases you won't need these)
197 std::string get_default_object_id_() const;
198
201
202 std::unique_ptr<Availability> availability_;
203
206
207 bool command_retain_{false};
208 bool retain_{true};
209 uint8_t qos_{0};
210 uint8_t subscribe_qos_{0};
212 bool resend_state_{false};
213};
214
215} // namespace mqtt
216} // namespace esphome
217
218#endif // USE_MQTt
StringRef is a reference to a string owned by something else.
Definition string_ref.h:21
MQTTComponent is the base class for all components that interact with MQTT to expose certain function...
std::unique_ptr< Availability > availability_
MQTTComponent()
Constructs a MQTTComponent.
void set_custom_state_topic(const char *custom_state_topic)
Set a custom state topic. Set to "" for default behavior.
virtual bool is_disabled_by_default() const
Get whether the underlying Entity is disabled by default.
std::string get_default_object_id_() const
Generate the Home Assistant MQTT discovery object id by automatically transforming the friendly name.
void set_qos(uint8_t qos)
Set QOS for state messages.
void schedule_resend_state()
Internal method for the MQTT client base to schedule a resend of the state on reconnect.
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
void set_command_retain(bool command_retain)
Set whether command message should be retained.
bool send_discovery_()
Internal method to start sending discovery info, this will call send_discovery().
void set_subscribe_qos(uint8_t qos)
Set the QOS for subscribe messages (used in discovery).
virtual std::string unique_id()
A unique ID for this MQTT component, empty for no unique id.
void subscribe_json(const std::string &topic, const mqtt_json_callback_t &callback, uint8_t qos=0)
Subscribe to a MQTT topic and automatically parse JSON payload.
void call_setup() override
Override setup_ so that we can call send_discovery() when needed.
bool publish_json(const std::string &topic, const json::json_build_t &f)
Construct and send a JSON MQTT message.
virtual std::string friendly_name() const
Get the friendly name of this MQTT component.
std::string get_discovery_topic_(const MQTTDiscoveryInfo &discovery_info) const
Helper method to get the discovery topic for this component.
std::string get_default_topic_for_(const std::string &suffix) const
Get this components state/command/... topic.
virtual std::string get_icon() const
Get the icon field of this component.
virtual std::string component_type() const =0
Override this method to return the component type (e.g. "light", "sensor", ...)
void set_retain(bool retain)
Set whether state message should be retained.
virtual const EntityBase * get_entity() const =0
Gets the Entity served by this MQTT component.
std::string get_state_topic_() const
Get the MQTT topic that new states will be shared to.
virtual void send_discovery(JsonObject root, SendDiscoveryConfig &config)=0
Send discovery info the Home Assistant, override this.
virtual bool send_initial_state()=0
void disable_discovery()
Disable discovery. Sets friendly name to "".
float get_setup_priority() const override
MQTT_COMPONENT setup priority.
void set_availability(std::string topic, std::string payload_available, std::string payload_not_available)
Set the Home Assistant availability data.
void set_custom_command_topic(const char *custom_command_topic)
Set a custom command topic. Set to "" for default behavior.
std::string get_command_topic_() const
Get the MQTT topic for listening to commands.
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to a MQTT topic.
std::function< void(JsonObject)> json_build_t
Callback function typedef for building JsonObjects.
Definition json_util.h:20
std::function< void(const std::string &, JsonObject)> mqtt_json_callback_t
Definition mqtt_client.h:36
std::function< void(const std::string &, const std::string &)> mqtt_callback_t
Callback for MQTT subscriptions.
Definition mqtt_client.h:35
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
Internal struct for MQTT Home Assistant discovery.
Definition mqtt_client.h:80
Simple Helper struct used for Home Assistant MQTT send_discovery().
bool command_topic
If the command topic should be included. Default to true.
bool state_topic
If the state topic should be included. Defaults to true.