ESPHome 2026.2.3
Loading...
Searching...
No Matches
mqtt_component.cpp
Go to the documentation of this file.
1#include "mqtt_component.h"
2
3#ifdef USE_MQTT
4
7#include "esphome/core/log.h"
10
11#include "mqtt_const.h"
12
13namespace esphome::mqtt {
14
15static const char *const TAG = "mqtt.component";
16
17// Entity category MQTT strings indexed by EntityCategory enum: NONE(0) is skipped, CONFIG(1), DIAGNOSTIC(2)
18PROGMEM_STRING_TABLE(EntityCategoryMqttStrings, "", "config", "diagnostic");
19
20// Helper functions for building topic strings on stack
21inline char *append_str(char *p, const char *s, size_t len) {
22 memcpy(p, s, len);
23 return p + len;
24}
25
26inline char *append_char(char *p, char c) {
27 *p = c;
28 return p + 1;
29}
30
31// Max lengths for stack-based topic building.
32// These limits are enforced at Python config validation time in mqtt/__init__.py
33// using cv.Length() validators for topic_prefix and discovery_prefix.
34// MQTT_COMPONENT_TYPE_MAX_LEN, MQTT_SUFFIX_MAX_LEN, and MQTT_DEFAULT_TOPIC_MAX_LEN are in mqtt_component.h.
35// ESPHOME_DEVICE_NAME_MAX_LEN and OBJECT_ID_MAX_LEN are defined in entity_base.h.
36// This ensures the stack buffers below are always large enough.
37// MQTT_DISCOVERY_PREFIX_MAX_LEN and MQTT_DISCOVERY_TOPIC_MAX_LEN are defined in mqtt_component.h
38
39// Function implementation of LOG_MQTT_COMPONENT macro to reduce code size
40void log_mqtt_component(const char *tag, MQTTComponent *obj, bool state_topic, bool command_topic) {
41 char buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
42 if (state_topic)
43 ESP_LOGCONFIG(tag, " State Topic: '%s'", obj->get_state_topic_to_(buf).c_str());
44 if (command_topic)
45 ESP_LOGCONFIG(tag, " Command Topic: '%s'", obj->get_command_topic_to_(buf).c_str());
46}
47
48void MQTTComponent::set_qos(uint8_t qos) { this->qos_ = qos; }
49
50void MQTTComponent::set_subscribe_qos(uint8_t qos) { this->subscribe_qos_ = qos; }
51
52void MQTTComponent::set_retain(bool retain) { this->retain_ = retain; }
53
54StringRef MQTTComponent::get_discovery_topic_to_(std::span<char, MQTT_DISCOVERY_TOPIC_MAX_LEN> buf,
55 const MQTTDiscoveryInfo &discovery_info) const {
56 char sanitized_name[ESPHOME_DEVICE_NAME_MAX_LEN + 1];
57 str_sanitize_to(sanitized_name, App.get_name().c_str());
58 const char *comp_type = this->component_type();
59 char object_id_buf[OBJECT_ID_MAX_LEN];
60 StringRef object_id = this->get_default_object_id_to_(object_id_buf);
61
62 char *p = buf.data();
63
64 p = append_str(p, discovery_info.prefix.data(), discovery_info.prefix.size());
65 p = append_char(p, '/');
66 p = append_str(p, comp_type, strlen(comp_type));
67 p = append_char(p, '/');
68 p = append_str(p, sanitized_name, strlen(sanitized_name));
69 p = append_char(p, '/');
70 p = append_str(p, object_id.c_str(), object_id.size());
71 p = append_str(p, "/config", 7);
72 *p = '\0';
73
74 return StringRef(buf.data(), p - buf.data());
75}
76
77StringRef MQTTComponent::get_default_topic_for_to_(std::span<char, MQTT_DEFAULT_TOPIC_MAX_LEN> buf, const char *suffix,
78 size_t suffix_len) const {
79 const std::string &topic_prefix = global_mqtt_client->get_topic_prefix();
80 if (topic_prefix.empty()) {
81 return StringRef(); // Empty topic_prefix means no default topic
82 }
83
84 const char *comp_type = this->component_type();
85 char object_id_buf[OBJECT_ID_MAX_LEN];
86 StringRef object_id = this->get_default_object_id_to_(object_id_buf);
87
88 char *p = buf.data();
89
90 p = append_str(p, topic_prefix.data(), topic_prefix.size());
91 p = append_char(p, '/');
92 p = append_str(p, comp_type, strlen(comp_type));
93 p = append_char(p, '/');
94 p = append_str(p, object_id.c_str(), object_id.size());
95 p = append_char(p, '/');
96 p = append_str(p, suffix, suffix_len);
97 *p = '\0';
98
99 return StringRef(buf.data(), p - buf.data());
100}
101
102std::string MQTTComponent::get_default_topic_for_(const std::string &suffix) const {
103 char buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
104 StringRef ref = this->get_default_topic_for_to_(buf, suffix.data(), suffix.size());
105 return std::string(ref.c_str(), ref.size());
106}
107
108StringRef MQTTComponent::get_state_topic_to_(std::span<char, MQTT_DEFAULT_TOPIC_MAX_LEN> buf) const {
109 if (this->custom_state_topic_.has_value()) {
110 // Returns ref to existing data for static/value, uses buf only for lambda case
111 return this->custom_state_topic_.ref_or_copy_to(buf.data(), buf.size());
112 }
113 return this->get_default_topic_for_to_(buf, "state", 5);
114}
115
116StringRef MQTTComponent::get_command_topic_to_(std::span<char, MQTT_DEFAULT_TOPIC_MAX_LEN> buf) const {
117 if (this->custom_command_topic_.has_value()) {
118 // Returns ref to existing data for static/value, uses buf only for lambda case
119 return this->custom_command_topic_.ref_or_copy_to(buf.data(), buf.size());
120 }
121 return this->get_default_topic_for_to_(buf, "command", 7);
122}
123
125 char buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
126 StringRef ref = this->get_state_topic_to_(buf);
127 return std::string(ref.c_str(), ref.size());
128}
129
131 char buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
132 StringRef ref = this->get_command_topic_to_(buf);
133 return std::string(ref.c_str(), ref.size());
134}
135
136bool MQTTComponent::publish(const std::string &topic, const std::string &payload) {
137 return this->publish(topic.c_str(), payload.data(), payload.size());
138}
139
140bool MQTTComponent::publish(const std::string &topic, const char *payload, size_t payload_length) {
141 return this->publish(topic.c_str(), payload, payload_length);
142}
143
144bool MQTTComponent::publish(const char *topic, const char *payload, size_t payload_length) {
145 if (topic[0] == '\0')
146 return false;
147 return global_mqtt_client->publish(topic, payload, payload_length, this->qos_, this->retain_);
148}
149
150bool MQTTComponent::publish(const char *topic, const char *payload) {
151 return this->publish(topic, payload, strlen(payload));
152}
153
154#ifdef USE_ESP8266
155bool MQTTComponent::publish(const std::string &topic, ProgmemStr payload) {
156 return this->publish(topic.c_str(), payload);
157}
158
159bool MQTTComponent::publish(const char *topic, ProgmemStr payload) {
160 if (topic[0] == '\0')
161 return false;
162 // On ESP8266, ProgmemStr is __FlashStringHelper* - need to copy from flash
163 char buf[64];
164 strncpy_P(buf, reinterpret_cast<const char *>(payload), sizeof(buf) - 1);
165 buf[sizeof(buf) - 1] = '\0';
166 return global_mqtt_client->publish(topic, buf, strlen(buf), this->qos_, this->retain_);
167}
168#endif
169
170bool MQTTComponent::publish_json(const std::string &topic, const json::json_build_t &f) {
171 return this->publish_json(topic.c_str(), f);
172}
173
174bool MQTTComponent::publish_json(const char *topic, const json::json_build_t &f) {
175 if (topic[0] == '\0')
176 return false;
177 return global_mqtt_client->publish_json(topic, f, this->qos_, this->retain_);
178}
179
182
183 char discovery_topic_buf[MQTT_DISCOVERY_TOPIC_MAX_LEN];
184 StringRef discovery_topic = this->get_discovery_topic_to_(discovery_topic_buf, discovery_info);
185
186 if (discovery_info.clean) {
187 ESP_LOGV(TAG, "'%s': Cleaning discovery", this->friendly_name_().c_str());
188 return global_mqtt_client->publish(discovery_topic.c_str(), "", 0, this->qos_, true);
189 }
190
191 ESP_LOGV(TAG, "'%s': Sending discovery", this->friendly_name_().c_str());
192
193 // NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
195 discovery_topic.c_str(),
196 [this](JsonObject root) {
197 SendDiscoveryConfig config;
198 config.state_topic = true;
199 config.command_topic = true;
200
201 this->send_discovery(root, config);
202 // Set subscription QoS (default is 0)
203 if (this->subscribe_qos_ != 0) {
204 root[MQTT_QOS] = this->subscribe_qos_;
205 }
206
207 // Fields from EntityBase
208 root[MQTT_NAME] = this->get_entity()->has_own_name() ? this->friendly_name_() : StringRef();
209
210 if (this->is_disabled_by_default_())
211 root[MQTT_ENABLED_BY_DEFAULT] = false;
212 // NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
213 const auto icon_ref = this->get_icon_ref_();
214 if (!icon_ref.empty()) {
215 root[MQTT_ICON] = icon_ref;
216 }
217 // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
218
219 const auto entity_category = this->get_entity()->get_entity_category();
220 if (entity_category != ENTITY_CATEGORY_NONE) {
221 root[MQTT_ENTITY_CATEGORY] = EntityCategoryMqttStrings::get_progmem_str(
222 static_cast<uint8_t>(entity_category), static_cast<uint8_t>(ENTITY_CATEGORY_CONFIG));
223 }
224
225 if (config.state_topic) {
226 char state_topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
227 root[MQTT_STATE_TOPIC] = this->get_state_topic_to_(state_topic_buf);
228 }
229 if (config.command_topic) {
230 char command_topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
231 root[MQTT_COMMAND_TOPIC] = this->get_command_topic_to_(command_topic_buf);
232 }
233 if (this->command_retain_)
234 root[MQTT_COMMAND_RETAIN] = true;
235
236 const Availability &avail =
237 this->availability_ == nullptr ? global_mqtt_client->get_availability() : *this->availability_;
238 if (!avail.topic.empty()) {
239 root[MQTT_AVAILABILITY_TOPIC] = avail.topic;
240 if (avail.payload_available != "online")
241 root[MQTT_PAYLOAD_AVAILABLE] = avail.payload_available;
242 if (avail.payload_not_available != "offline")
243 root[MQTT_PAYLOAD_NOT_AVAILABLE] = avail.payload_not_available;
244 }
245
247 char object_id_buf[OBJECT_ID_MAX_LEN];
248 StringRef object_id = this->get_default_object_id_to_(object_id_buf);
250 char friendly_name_hash[9];
251 buf_append_printf(friendly_name_hash, sizeof(friendly_name_hash), 0, "%08" PRIx32,
252 fnv1_hash(this->friendly_name_().c_str()));
253 // Format: mac-component_type-hash (e.g. "aabbccddeeff-sensor-12345678")
254 // MAC (12) + "-" (1) + domain (max 20) + "-" (1) + hash (8) + null (1) = 43
255 char unique_id[MAC_ADDRESS_BUFFER_SIZE + ESPHOME_DOMAIN_MAX_LEN + 11];
256 char mac_buf[MAC_ADDRESS_BUFFER_SIZE];
258 buf_append_printf(unique_id, sizeof(unique_id), 0, "%s-%s-%s", mac_buf, this->component_type(),
259 friendly_name_hash);
260 root[MQTT_UNIQUE_ID] = unique_id;
261 } else {
262 // default to almost-unique ID. It's a hack but the only way to get that
263 // gorgeous device registry view.
264 // "ESP" (3) + component_type (max 20) + object_id (max 128) + null
265 char unique_id_buf[3 + MQTT_COMPONENT_TYPE_MAX_LEN + OBJECT_ID_MAX_LEN + 1];
266 buf_append_printf(unique_id_buf, sizeof(unique_id_buf), 0, "ESP%s%s", this->component_type(),
267 object_id.c_str());
268 root[MQTT_UNIQUE_ID] = unique_id_buf;
269 }
270
271 const std::string &node_name = App.get_name();
273 // node_name (max 31) + "_" (1) + object_id (max 128) + null
274 char object_id_full[ESPHOME_DEVICE_NAME_MAX_LEN + 1 + OBJECT_ID_MAX_LEN + 1];
275 buf_append_printf(object_id_full, sizeof(object_id_full), 0, "%s_%s", node_name.c_str(), object_id.c_str());
276 root[MQTT_OBJECT_ID] = object_id_full;
277 }
278
279 const std::string &friendly_name_ref = App.get_friendly_name();
280 const std::string &node_friendly_name = friendly_name_ref.empty() ? node_name : friendly_name_ref;
281 const char *node_area = App.get_area();
282
283 JsonObject device_info = root[MQTT_DEVICE].to<JsonObject>();
284 char mac[MAC_ADDRESS_BUFFER_SIZE];
286 device_info[MQTT_DEVICE_IDENTIFIERS] = mac;
287 device_info[MQTT_DEVICE_NAME] = node_friendly_name;
288#ifdef ESPHOME_PROJECT_NAME
289 device_info[MQTT_DEVICE_SW_VERSION] = ESPHOME_PROJECT_VERSION " (ESPHome " ESPHOME_VERSION ")";
290 const char *model = std::strchr(ESPHOME_PROJECT_NAME, '.');
291 device_info[MQTT_DEVICE_MODEL] = model == nullptr ? ESPHOME_BOARD : model + 1;
292 if (model == nullptr) {
293 device_info[MQTT_DEVICE_MANUFACTURER] = ESPHOME_PROJECT_NAME;
294 } else {
295 // Extract manufacturer (part before '.') using stack buffer to avoid heap allocation
296 // memcpy is used instead of strncpy since we know the exact length and strncpy
297 // would still require manual null-termination
298 char manufacturer[sizeof(ESPHOME_PROJECT_NAME)];
299 size_t len = model - ESPHOME_PROJECT_NAME;
300 memcpy(manufacturer, ESPHOME_PROJECT_NAME, len);
301 manufacturer[len] = '\0';
302 device_info[MQTT_DEVICE_MANUFACTURER] = manufacturer;
303 }
304#else
305 static const char ver_fmt[] PROGMEM = ESPHOME_VERSION " (config hash 0x%08" PRIx32 ")";
306 // Buffer sized for format string expansion: ~4 bytes net growth from format specifier to 8 hex digits, plus
307 // safety margin
308 char version_buf[sizeof(ver_fmt) + 8];
309#ifdef USE_ESP8266
310 snprintf_P(version_buf, sizeof(version_buf), ver_fmt, App.get_config_hash());
311#else
312 snprintf(version_buf, sizeof(version_buf), ver_fmt, App.get_config_hash());
313#endif
314 device_info[MQTT_DEVICE_SW_VERSION] = version_buf;
315 device_info[MQTT_DEVICE_MODEL] = ESPHOME_BOARD;
316#if defined(USE_ESP8266) || defined(USE_ESP32)
317 device_info[MQTT_DEVICE_MANUFACTURER] = "Espressif";
318#elif defined(USE_RP2040)
319 device_info[MQTT_DEVICE_MANUFACTURER] = "Raspberry Pi";
320#elif defined(USE_BK72XX)
321 device_info[MQTT_DEVICE_MANUFACTURER] = "Beken";
322#elif defined(USE_RTL87XX)
323 device_info[MQTT_DEVICE_MANUFACTURER] = "Realtek";
324#elif defined(USE_HOST)
325 device_info[MQTT_DEVICE_MANUFACTURER] = "Host";
326#endif
327#endif
328 if (node_area[0] != '\0') {
329 device_info[MQTT_DEVICE_SUGGESTED_AREA] = node_area;
330 }
331
332 device_info[MQTT_DEVICE_CONNECTIONS][0][0] = "mac";
333 device_info[MQTT_DEVICE_CONNECTIONS][0][1] = mac;
334 },
335 this->qos_, discovery_info.retain);
336 // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
337}
338
339uint8_t MQTTComponent::get_qos() const { return this->qos_; }
340
341bool MQTTComponent::get_retain() const { return this->retain_; }
342
343bool MQTTComponent::is_discovery_enabled() const {
344 return this->discovery_enabled_ && global_mqtt_client->is_discovery_enabled();
345}
346
347void MQTTComponent::subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos) {
348 global_mqtt_client->subscribe(topic, std::move(callback), qos);
349}
350
351void MQTTComponent::subscribe_json(const std::string &topic, const mqtt_json_callback_t &callback, uint8_t qos) {
352 global_mqtt_client->subscribe_json(topic, callback, qos);
353}
354
355MQTTComponent::MQTTComponent() = default;
356
357float MQTTComponent::get_setup_priority() const { return setup_priority::AFTER_CONNECTION; }
358void MQTTComponent::disable_discovery() { this->discovery_enabled_ = false; }
359void MQTTComponent::set_command_retain(bool command_retain) { this->command_retain_ = command_retain; }
360
361void MQTTComponent::set_availability(std::string topic, std::string payload_available,
362 std::string payload_not_available) {
363 this->availability_ = make_unique<Availability>();
364 this->availability_->topic = std::move(topic);
365 this->availability_->payload_available = std::move(payload_available);
366 this->availability_->payload_not_available = std::move(payload_not_available);
367}
368void MQTTComponent::disable_availability() { this->set_availability("", "", ""); }
369void MQTTComponent::call_setup() {
370 // Cache is_internal result once during setup - topics don't change after this
371 this->is_internal_ = this->compute_is_internal_();
372 if (this->is_internal_)
373 return;
374
375 this->setup();
376
378
379 if (!this->is_connected_())
380 return;
381
382 if (this->is_discovery_enabled()) {
383 if (!this->send_discovery_()) {
384 this->schedule_resend_state();
385 }
386 }
387 if (!this->send_initial_state()) {
388 this->schedule_resend_state();
389 }
390}
391
392void MQTTComponent::process_resend() {
393 // Called by MQTTClientComponent when connected to process pending resends
394 // Note: is_internal() check not needed - internal components are never registered
395 if (!this->resend_state_)
396 return;
397
398 this->resend_state_ = false;
399 if (this->is_discovery_enabled()) {
400 if (!this->send_discovery_()) {
401 this->schedule_resend_state();
402 }
403 }
404 if (!this->send_initial_state()) {
405 this->schedule_resend_state();
406 }
407}
408void MQTTComponent::call_dump_config() {
409 if (this->is_internal())
410 return;
411
412 this->dump_config();
413}
414void MQTTComponent::schedule_resend_state() { this->resend_state_ = true; }
415bool MQTTComponent::is_connected_() const { return global_mqtt_client->is_connected(); }
416
417// Pull these properties from EntityBase if not overridden
418const StringRef &MQTTComponent::friendly_name_() const { return this->get_entity()->get_name(); }
419StringRef MQTTComponent::get_default_object_id_to_(std::span<char, OBJECT_ID_MAX_LEN> buf) const {
420 return this->get_entity()->get_object_id_to(buf);
421}
422StringRef MQTTComponent::get_icon_ref_() const { return this->get_entity()->get_icon_ref(); }
423bool MQTTComponent::is_disabled_by_default_() const { return this->get_entity()->is_disabled_by_default(); }
424bool MQTTComponent::compute_is_internal_() {
425 if (this->custom_state_topic_.has_value()) {
426 // If the custom state_topic is empty, return true as it is internal and should not publish
427 // else, return false, as it is explicitly set to a topic, so it is not internal and should publish
428 // Using is_empty() avoids heap allocation for non-lambda cases
429 return this->custom_state_topic_.is_empty();
430 }
431
432 if (this->custom_command_topic_.has_value()) {
433 // If the custom command_topic is empty, return true as it is internal and should not publish
434 // else, return false, as it is explicitly set to a topic, so it is not internal and should publish
435 // Using is_empty() avoids heap allocation for non-lambda cases
436 return this->custom_command_topic_.is_empty();
437 }
438
439 // No custom topics have been set - check topic_prefix directly to avoid allocation
440 if (global_mqtt_client->get_topic_prefix().empty()) {
441 // If the default topic prefix is empty, then the component, by default, is internal and should not publish
442 return true;
443 }
444
445 // Use ESPHome's component internal state if topic_prefix is not empty with no custom state_topic or command_topic
446 return this->get_entity()->is_internal();
447}
448
449} // namespace esphome::mqtt
450
451#endif // USE_MQTT
const std::string & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
constexpr uint32_t get_config_hash()
Get the config hash as a 32-bit integer.
const char * get_area() const
Get the area of this Application set by pre_setup().
const std::string & get_name() const
Get the name of this Application set by pre_setup().
ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.", "2026.2.0") void set_retry(const std uint32_t uint8_t std::function< RetryResult(uint8_t)> && f
Definition component.h:373
EntityCategory get_entity_category() const
Definition entity_base.h:87
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
constexpr const char * c_str() const
Definition string_ref.h:73
constexpr size_type size() const
Definition string_ref.h:74
StringRef ref_or_copy_to(char *lambda_buf, size_t lambda_buf_size) const
Get a StringRef to the string value without heap allocation when possible.
Definition automation.h:216
const std::string & get_topic_prefix() const
Get the topic prefix of this device, using default if necessary.
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 register_mqtt_component(MQTTComponent *component)
bool publish(const MQTTMessage &message)
Publish a MQTTMessage.
const MQTTDiscoveryInfo & get_discovery_info() const
Get Home Assistant discovery info.
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to an MQTT topic and call callback when a message is received.
const Availability & get_availability()
bool publish_json(const std::string &topic, const json::json_build_t &f, uint8_t qos=0, bool retain=false)
Construct and send a JSON MQTT message.
MQTTComponent is the base class for all components that interact with MQTT to expose certain function...
StringRef get_command_topic_to_(std::span< char, MQTT_DEFAULT_TOPIC_MAX_LEN > buf) const
Get the MQTT command topic into a buffer (no heap allocation for non-lambda custom topics).
TemplatableValue< std::string > custom_state_topic_
TemplatableValue< std::string > custom_command_topic_
std::unique_ptr< Availability > availability_
StringRef get_discovery_topic_to_(std::span< char, MQTT_DISCOVERY_TOPIC_MAX_LEN > buf, const MQTTDiscoveryInfo &discovery_info) const
Helper method to get the discovery topic for this component into a buffer.
bool is_disabled_by_default_() const
Get whether the underlying Entity is disabled by default.
void set_qos(uint8_t qos)
Set QOS for state messages.
StringRef get_default_topic_for_to_(std::span< char, MQTT_DEFAULT_TOPIC_MAX_LEN > buf, const char *suffix, size_t suffix_len) const
Get this components state/command/... topic into a buffer.
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
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).
bool publish_json(const std::string &topic, const json::json_build_t &f)
Construct and send a JSON MQTT message.
std::string get_default_topic_for_(const std::string &suffix) const
Get this components state/command/... topic (allocates std::string).
StringRef get_default_object_id_to_(std::span< char, OBJECT_ID_MAX_LEN > buf) const
Get the object ID for this MQTT component, writing to the provided buffer.
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 (allocates std::string).
StringRef get_state_topic_to_(std::span< char, MQTT_DEFAULT_TOPIC_MAX_LEN > buf) const
Get the MQTT state topic into a buffer (no heap allocation for non-lambda custom topics).
const StringRef & friendly_name_() const
Get the friendly name of this MQTT component.
std::string get_command_topic_() const
Get the MQTT topic for listening to commands (allocates std::string).
virtual const char * component_type() const =0
Override this method to return the component type (e.g. "light", "sensor", ...)
StringRef get_icon_ref_() const
Get the icon field of this component as StringRef.
void setup()
PROGMEM_STRING_TABLE(AlarmControlPanelStateStrings, "DISARMED", "ARMED_HOME", "ARMED_AWAY", "ARMED_NIGHT", "ARMED_VACATION", "ARMED_CUSTOM_BYPASS", "PENDING", "ARMING", "DISARMING", "TRIGGERED", "UNKNOWN")
std::function< void(JsonObject)> json_build_t
Callback function typedef for building JsonObjects.
Definition json_util.h:46
@ MQTT_DEVICE_NAME_OBJECT_ID_GENERATOR
Definition mqtt_client.h:76
@ MQTT_MAC_ADDRESS_UNIQUE_ID_GENERATOR
Definition mqtt_client.h:70
std::function< void(const std::string &, JsonObject)> mqtt_json_callback_t
Definition mqtt_client.h:39
char * append_char(char *p, char c)
char * append_str(char *p, const char *s, size_t len)
std::function< void(const std::string &, const std::string &)> mqtt_callback_t
Callback for MQTT subscriptions.
Definition mqtt_client.h:38
MQTTClientComponent * global_mqtt_client
void log_mqtt_component(const char *tag, MQTTComponent *obj, bool state_topic, bool command_topic)
const float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition component.cpp:92
std::string size_t len
Definition helpers.h:692
uint32_t fnv1_hash(const char *str)
Calculate a FNV-1 hash of str.
Definition helpers.cpp:148
@ ENTITY_CATEGORY_NONE
Definition entity_base.h:34
@ ENTITY_CATEGORY_CONFIG
Definition entity_base.h:35
char * str_sanitize_to(char *buffer, size_t buffer_size, const char *str)
Sanitize a string to buffer, keeping only alphanumerics, dashes, and underscores.
Definition helpers.cpp:210
void get_mac_address_into_buffer(std::span< char, MAC_ADDRESS_BUFFER_SIZE > buf)
Get the device MAC address into the given buffer, in lowercase hex notation.
Definition helpers.cpp:813
Application App
Global storage of Application pointer - only one Application can exist.
const __FlashStringHelper * ProgmemStr
Definition progmem.h:26
Simple data struct for Home Assistant component availability.
Definition mqtt_client.h:61
std::string payload_not_available
Definition mqtt_client.h:64
std::string topic
Empty means disabled.
Definition mqtt_client.h:62
Internal struct for MQTT Home Assistant discovery.
Definition mqtt_client.h:83
MQTTDiscoveryUniqueIdGenerator unique_id_generator
Definition mqtt_client.h:88
std::string prefix
The Home Assistant discovery prefix. Empty means disabled.
Definition mqtt_client.h:84
MQTTDiscoveryObjectIdGenerator object_id_generator
Definition mqtt_client.h:89