mqtt: add ESP-IDF >= 5.0 support (#4854)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Stijn Tintel 2023-06-21 02:53:32 +03:00 committed by GitHub
parent bfe85dd710
commit cb5a01da29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 4 deletions

View file

@ -11,6 +11,7 @@ namespace mqtt {
static const char *const TAG = "mqtt.idf"; static const char *const TAG = "mqtt.idf";
bool MQTTBackendIDF::initialize_() { bool MQTTBackendIDF::initialize_() {
#if ESP_IDF_VERSION_MAJOR < 5
mqtt_cfg_.user_context = (void *) this; mqtt_cfg_.user_context = (void *) this;
mqtt_cfg_.buffer_size = MQTT_BUFFER_SIZE; mqtt_cfg_.buffer_size = MQTT_BUFFER_SIZE;
@ -47,6 +48,41 @@ bool MQTTBackendIDF::initialize_() {
} else { } else {
mqtt_cfg_.transport = MQTT_TRANSPORT_OVER_TCP; mqtt_cfg_.transport = MQTT_TRANSPORT_OVER_TCP;
} }
#else
mqtt_cfg_.broker.address.hostname = this->host_.c_str();
mqtt_cfg_.broker.address.port = this->port_;
mqtt_cfg_.session.keepalive = this->keep_alive_;
mqtt_cfg_.session.disable_clean_session = !this->clean_session_;
if (!this->username_.empty()) {
mqtt_cfg_.credentials.username = this->username_.c_str();
if (!this->password_.empty()) {
mqtt_cfg_.credentials.authentication.password = this->password_.c_str();
}
}
if (!this->lwt_topic_.empty()) {
mqtt_cfg_.session.last_will.topic = this->lwt_topic_.c_str();
this->mqtt_cfg_.session.last_will.qos = this->lwt_qos_;
this->mqtt_cfg_.session.last_will.retain = this->lwt_retain_;
if (!this->lwt_message_.empty()) {
mqtt_cfg_.session.last_will.msg = this->lwt_message_.c_str();
mqtt_cfg_.session.last_will.msg_len = this->lwt_message_.size();
}
}
if (!this->client_id_.empty()) {
mqtt_cfg_.credentials.client_id = this->client_id_.c_str();
}
if (ca_certificate_.has_value()) {
mqtt_cfg_.broker.verification.certificate = ca_certificate_.value().c_str();
mqtt_cfg_.broker.verification.skip_cert_common_name_check = skip_cert_cn_check_;
mqtt_cfg_.broker.address.transport = MQTT_TRANSPORT_OVER_SSL;
} else {
mqtt_cfg_.broker.address.transport = MQTT_TRANSPORT_OVER_TCP;
}
#endif
auto *mqtt_client = esp_mqtt_client_init(&mqtt_cfg_); auto *mqtt_client = esp_mqtt_client_init(&mqtt_cfg_);
if (mqtt_client) { if (mqtt_client) {
handler_.reset(mqtt_client); handler_.reset(mqtt_client);
@ -78,9 +114,8 @@ void MQTTBackendIDF::mqtt_event_handler_(const Event &event) {
case MQTT_EVENT_CONNECTED: case MQTT_EVENT_CONNECTED:
ESP_LOGV(TAG, "MQTT_EVENT_CONNECTED"); ESP_LOGV(TAG, "MQTT_EVENT_CONNECTED");
// TODO session present check
this->is_connected_ = true; this->is_connected_ = true;
this->on_connect_.call(!mqtt_cfg_.disable_clean_session); this->on_connect_.call(event.session_present);
break; break;
case MQTT_EVENT_DISCONNECTED: case MQTT_EVENT_DISCONNECTED:
ESP_LOGV(TAG, "MQTT_EVENT_DISCONNECTED"); ESP_LOGV(TAG, "MQTT_EVENT_DISCONNECTED");

View file

@ -22,6 +22,7 @@ struct Event {
bool retain; bool retain;
int qos; int qos;
bool dup; bool dup;
bool session_present;
esp_mqtt_error_codes_t error_handle; esp_mqtt_error_codes_t error_handle;
// Construct from esp_mqtt_event_t // Construct from esp_mqtt_event_t
@ -36,6 +37,7 @@ struct Event {
retain(event.retain), retain(event.retain),
qos(event.qos), qos(event.qos),
dup(event.dup), dup(event.dup),
session_present(event.session_present),
error_handle(*event.error_handle) {} error_handle(*event.error_handle) {}
}; };

View file

@ -118,7 +118,7 @@ bool MQTTComponent::send_discovery_() {
} else { } else {
if (discovery_info.unique_id_generator == MQTT_MAC_ADDRESS_UNIQUE_ID_GENERATOR) { if (discovery_info.unique_id_generator == MQTT_MAC_ADDRESS_UNIQUE_ID_GENERATOR) {
char friendly_name_hash[9]; char friendly_name_hash[9];
sprintf(friendly_name_hash, "%08x", fnv1_hash(this->friendly_name())); sprintf(friendly_name_hash, "%08" PRIx32, fnv1_hash(this->friendly_name()));
friendly_name_hash[8] = 0; // ensure the hash-string ends with null friendly_name_hash[8] = 0; // ensure the hash-string ends with null
root[MQTT_UNIQUE_ID] = get_mac_address() + "-" + this->component_type() + "-" + friendly_name_hash; root[MQTT_UNIQUE_ID] = get_mac_address() + "-" + this->component_type() + "-" + friendly_name_hash;
} else { } else {

View file

@ -1,3 +1,4 @@
#include <cinttypes>
#include "mqtt_sensor.h" #include "mqtt_sensor.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
@ -26,7 +27,7 @@ void MQTTSensorComponent::setup() {
void MQTTSensorComponent::dump_config() { void MQTTSensorComponent::dump_config() {
ESP_LOGCONFIG(TAG, "MQTT Sensor '%s':", this->sensor_->get_name().c_str()); ESP_LOGCONFIG(TAG, "MQTT Sensor '%s':", this->sensor_->get_name().c_str());
if (this->get_expire_after() > 0) { if (this->get_expire_after() > 0) {
ESP_LOGCONFIG(TAG, " Expire After: %us", this->get_expire_after() / 1000); ESP_LOGCONFIG(TAG, " Expire After: %" PRIu32 "s", this->get_expire_after() / 1000);
} }
LOG_MQTT_COMPONENT(true, false) LOG_MQTT_COMPONENT(true, false)
} }