Skip to content

Commit

Permalink
Add clean_session as configurable option to the MQTT component (espho…
Browse files Browse the repository at this point in the history
  • Loading branch information
victorclaessen authored Sep 26, 2024
1 parent c55b4f5 commit 3df25a1
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions esphome/components/mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
CONF_BIRTH_MESSAGE,
CONF_BROKER,
CONF_CERTIFICATE_AUTHORITY,
CONF_CLEAN_SESSION,
CONF_CLIENT_CERTIFICATE,
CONF_CLIENT_CERTIFICATE_KEY,
CONF_CLIENT_ID,
Expand Down Expand Up @@ -209,6 +210,7 @@ def validate_fingerprint(value):
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
Expand Down Expand Up @@ -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]))

Expand Down
2 changes: 2 additions & 0 deletions esphome/components/mqtt/mqtt_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions esphome/components/mqtt/mqtt_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions esphome/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
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"
Expand Down
1 change: 1 addition & 0 deletions tests/components/mqtt/common.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mqtt:
port: 1883
username: debug
password: debug
clean_session: True
client_id: someclient
use_abbreviations: false
discovery: true
Expand Down

0 comments on commit 3df25a1

Please sign in to comment.