1
0
mirror of https://github.com/esphome/esphome.git synced 2024-10-05 18:30:57 +01:00

Add clean_session as configurable option to the MQTT component (#7501)

This commit is contained in:
victorclaessen 2024-09-26 23:57:51 +02:00 committed by GitHub
parent c55b4f5e1b
commit 3df25a183a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 9 additions and 0 deletions

View File

@ -11,6 +11,7 @@ from esphome.const import (
CONF_BIRTH_MESSAGE,
CONF_BROKER,
CONF_CERTIFICATE_AUTHORITY,
CONF_CLEAN_SESSION,
CONF_CLIENT_CERTIFICATE,
CONF_CLIENT_CERTIFICATE_KEY,
CONF_CLIENT_ID,
@ -209,6 +210,7 @@ CONFIG_SCHEMA = cv.All(
cv.Optional(CONF_PORT, default=1883): cv.port,
cv.Optional(CONF_USERNAME, default=""): cv.string,
cv.Optional(CONF_PASSWORD, default=""): cv.string,
cv.Optional(CONF_CLEAN_SESSION, default=False): cv.boolean,
cv.Optional(CONF_CLIENT_ID): cv.string,
cv.SplitDefault(CONF_IDF_SEND_ASYNC, esp32_idf=False): cv.All(
cv.boolean, cv.only_with_esp_idf
@ -325,6 +327,7 @@ async def to_code(config):
cg.add(var.set_broker_port(config[CONF_PORT]))
cg.add(var.set_username(config[CONF_USERNAME]))
cg.add(var.set_password(config[CONF_PASSWORD]))
cg.add(var.set_clean_session(config[CONF_CLEAN_SESSION]))
if CONF_CLIENT_ID in config:
cg.add(var.set_client_id(config[CONF_CLIENT_ID]))

View File

@ -147,6 +147,7 @@ void MQTTClientComponent::dump_config() {
this->ip_.str().c_str());
ESP_LOGCONFIG(TAG, " Username: " LOG_SECRET("'%s'"), this->credentials_.username.c_str());
ESP_LOGCONFIG(TAG, " Client ID: " LOG_SECRET("'%s'"), this->credentials_.client_id.c_str());
ESP_LOGCONFIG(TAG, " Clean Session: %s", YESNO(this->credentials_.clean_session));
if (this->is_discovery_ip_enabled()) {
ESP_LOGCONFIG(TAG, " Discovery IP enabled");
}
@ -246,6 +247,7 @@ void MQTTClientComponent::start_connect_() {
this->mqtt_backend_.disconnect();
this->mqtt_backend_.set_client_id(this->credentials_.client_id.c_str());
this->mqtt_backend_.set_clean_session(this->credentials_.clean_session);
const char *username = nullptr;
if (!this->credentials_.username.empty())
username = this->credentials_.username.c_str();

View File

@ -51,6 +51,7 @@ struct MQTTCredentials {
std::string username;
std::string password;
std::string client_id; ///< The client ID. Will automatically be truncated to 23 characters.
bool clean_session; ///< Whether the session will be cleaned or remembered between connects.
};
/// Simple data struct for Home Assistant component availability.
@ -254,6 +255,7 @@ class MQTTClientComponent : public Component {
void set_username(const std::string &username) { this->credentials_.username = username; }
void set_password(const std::string &password) { this->credentials_.password = password; }
void set_client_id(const std::string &client_id) { this->credentials_.client_id = client_id; }
void set_clean_session(const bool &clean_session) { this->credentials_.clean_session = clean_session; }
void set_on_connect(mqtt_on_connect_callback_t &&callback);
void set_on_disconnect(mqtt_on_disconnect_callback_t &&callback);

View File

@ -120,6 +120,7 @@ CONF_CHANNELS = "channels"
CONF_CHARACTERISTIC_UUID = "characteristic_uuid"
CONF_CHECK = "check"
CONF_CHIPSET = "chipset"
CONF_CLEAN_SESSION = "clean_session"
CONF_CLEAR_IMPEDANCE = "clear_impedance"
CONF_CLIENT_CERTIFICATE = "client_certificate"
CONF_CLIENT_CERTIFICATE_KEY = "client_certificate_key"

View File

@ -10,6 +10,7 @@ mqtt:
port: 1883
username: debug
password: debug
clean_session: True
client_id: someclient
use_abbreviations: false
discovery: true