15static const char *
const TAG =
"mqtt.component";
41 char buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
56 char sanitized_name[ESPHOME_DEVICE_NAME_MAX_LEN + 1];
59 char object_id_buf[OBJECT_ID_MAX_LEN];
66 p =
append_str(p, comp_type, strlen(comp_type));
68 p =
append_str(p, sanitized_name, strlen(sanitized_name));
74 return StringRef(buf.data(), p - buf.data());
78 size_t suffix_len)
const {
80 if (topic_prefix.empty()) {
85 char object_id_buf[OBJECT_ID_MAX_LEN];
90 p =
append_str(p, topic_prefix.data(), topic_prefix.size());
92 p =
append_str(p, comp_type, strlen(comp_type));
99 return StringRef(buf.data(), p - buf.data());
103 char buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
105 return std::string(ref.
c_str(), ref.
size());
125 char buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
127 return std::string(ref.
c_str(), ref.
size());
131 char buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
133 return std::string(ref.
c_str(), ref.
size());
137 return this->
publish(topic.c_str(), payload.data(), payload.size());
141 return this->
publish(topic.c_str(), payload, payload_length);
145 if (topic[0] ==
'\0')
151 return this->
publish(topic, payload, strlen(payload));
156 return this->
publish(topic.c_str(), payload);
160 if (topic[0] ==
'\0')
164 strncpy_P(buf,
reinterpret_cast<const char *
>(payload),
sizeof(buf) - 1);
165 buf[
sizeof(buf) - 1] =
'\0';
175 if (topic[0] ==
'\0')
183 char discovery_topic_buf[MQTT_DISCOVERY_TOPIC_MAX_LEN];
186 if (discovery_info.
clean) {
187 ESP_LOGV(TAG,
"'%s': Cleaning discovery", this->
friendly_name_().c_str());
191 ESP_LOGV(TAG,
"'%s': Sending discovery", this->
friendly_name_().c_str());
195 discovery_topic.
c_str(),
196 [
this](JsonObject root) {
197 SendDiscoveryConfig config;
198 config.state_topic = true;
199 config.command_topic = true;
201 this->send_discovery(root, config);
203 if (this->subscribe_qos_ != 0) {
204 root[MQTT_QOS] = this->subscribe_qos_;
208 root[MQTT_NAME] = this->get_entity()->has_own_name() ? this->friendly_name_() :
StringRef();
211 root[MQTT_ENABLED_BY_DEFAULT] =
false;
214 if (!icon_ref.empty()) {
215 root[MQTT_ICON] = icon_ref;
221 root[MQTT_ENTITY_CATEGORY] = EntityCategoryMqttStrings::get_progmem_str(
225 if (config.state_topic) {
226 char state_topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
229 if (config.command_topic) {
230 char command_topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
234 root[MQTT_COMMAND_RETAIN] =
true;
238 if (!avail.
topic.empty()) {
239 root[MQTT_AVAILABILITY_TOPIC] = avail.
topic;
247 char object_id_buf[OBJECT_ID_MAX_LEN];
250 char friendly_name_hash[9];
251 buf_append_printf(friendly_name_hash,
sizeof(friendly_name_hash), 0,
"%08" PRIx32,
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(),
260 root[MQTT_UNIQUE_ID] = unique_id;
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(),
268 root[MQTT_UNIQUE_ID] = unique_id_buf;
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;
280 const std::string &node_friendly_name = friendly_name_ref.empty() ? node_name : friendly_name_ref;
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;
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;
305 static const char ver_fmt[] PROGMEM = ESPHOME_VERSION
" (config hash 0x%08" PRIx32
")";
308 char version_buf[
sizeof(ver_fmt) + 8];
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";
328 if (node_area[0] !=
'\0') {
329 device_info[MQTT_DEVICE_SUGGESTED_AREA] = node_area;
332 device_info[MQTT_DEVICE_CONNECTIONS][0][0] =
"mac";
333 device_info[MQTT_DEVICE_CONNECTIONS][0][1] = mac;
335 this->qos_, discovery_info.retain);
339uint8_t MQTTComponent::get_qos()
const {
return this->qos_; }
341bool MQTTComponent::get_retain()
const {
return this->retain_; }
343bool MQTTComponent::is_discovery_enabled()
const {
347void MQTTComponent::subscribe(
const std::string &topic,
mqtt_callback_t callback, uint8_t qos) {
351void MQTTComponent::subscribe_json(
const std::string &topic,
const mqtt_json_callback_t &callback, uint8_t qos) {
355MQTTComponent::MQTTComponent() =
default;
358void MQTTComponent::disable_discovery() { this->discovery_enabled_ =
false; }
359void MQTTComponent::set_command_retain(
bool command_retain) { this->command_retain_ = command_retain; }
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);
368void MQTTComponent::disable_availability() { this->set_availability(
"",
"",
""); }
369void MQTTComponent::call_setup() {
371 this->is_internal_ = this->compute_is_internal_();
372 if (this->is_internal_)
379 if (!this->is_connected_())
382 if (this->is_discovery_enabled()) {
383 if (!this->send_discovery_()) {
384 this->schedule_resend_state();
387 if (!this->send_initial_state()) {
388 this->schedule_resend_state();
392void MQTTComponent::process_resend() {
395 if (!this->resend_state_)
398 this->resend_state_ =
false;
399 if (this->is_discovery_enabled()) {
400 if (!this->send_discovery_()) {
401 this->schedule_resend_state();
404 if (!this->send_initial_state()) {
405 this->schedule_resend_state();
408void MQTTComponent::call_dump_config() {
409 if (this->is_internal())
414void MQTTComponent::schedule_resend_state() { this->resend_state_ =
true; }
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);
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()) {
429 return this->custom_state_topic_.is_empty();
432 if (this->custom_command_topic_.has_value()) {
436 return this->custom_command_topic_.is_empty();
446 return this->get_entity()->is_internal();
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
EntityCategory get_entity_category() const
StringRef is a reference to a string owned by something else.
constexpr const char * c_str() const
constexpr size_type size() const
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.
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.
bool is_discovery_enabled() const
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.
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.
@ MQTT_DEVICE_NAME_OBJECT_ID_GENERATOR
@ MQTT_MAC_ADDRESS_UNIQUE_ID_GENERATOR
std::function< void(const std::string &, JsonObject)> mqtt_json_callback_t
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.
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.
uint32_t fnv1_hash(const char *str)
Calculate a FNV-1 hash of str.
char * str_sanitize_to(char *buffer, size_t buffer_size, const char *str)
Sanitize a string to buffer, keeping only alphanumerics, dashes, and underscores.
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.
Application App
Global storage of Application pointer - only one Application can exist.
const __FlashStringHelper * ProgmemStr
Simple data struct for Home Assistant component availability.
std::string payload_not_available
std::string topic
Empty means disabled.
std::string payload_available
Internal struct for MQTT Home Assistant discovery.
MQTTDiscoveryUniqueIdGenerator unique_id_generator
std::string prefix
The Home Assistant discovery prefix. Empty means disabled.
MQTTDiscoveryObjectIdGenerator object_id_generator