From ebb5d58c14709863122bf9c05f5b7e79f8422286 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Fri, 31 May 2019 10:37:10 +0200 Subject: [PATCH] Fix MQTT client_id changed (#579) Fixes https://github.com/esphome/issues/issues/323 --- esphome/components/mqtt/__init__.py | 5 +++-- esphome/components/mqtt/mqtt_client.cpp | 5 ++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/esphome/components/mqtt/__init__.py b/esphome/components/mqtt/__init__.py index c363f93d06..c1305d8422 100644 --- a/esphome/components/mqtt/__init__.py +++ b/esphome/components/mqtt/__init__.py @@ -104,7 +104,7 @@ CONFIG_SCHEMA = cv.All(cv.Schema({ 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_CLIENT_ID, default=lambda: CORE.name): cv.string, + cv.Optional(CONF_CLIENT_ID): cv.string, cv.Optional(CONF_DISCOVERY, default=True): cv.Any(cv.boolean, cv.one_of("CLEAN", upper=True)), cv.Optional(CONF_DISCOVERY_RETAIN, default=True): cv.boolean, cv.Optional(CONF_DISCOVERY_PREFIX, default="homeassistant"): cv.publish_topic, @@ -161,7 +161,8 @@ 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_client_id(config[CONF_CLIENT_ID])) + if CONF_CLIENT_ID in config: + cg.add(var.set_client_id(config[CONF_CLIENT_ID])) discovery = config[CONF_DISCOVERY] discovery_retain = config[CONF_DISCOVERY_RETAIN] diff --git a/esphome/components/mqtt/mqtt_client.cpp b/esphome/components/mqtt/mqtt_client.cpp index 819dbd6119..7433fcc9b3 100644 --- a/esphome/components/mqtt/mqtt_client.cpp +++ b/esphome/components/mqtt/mqtt_client.cpp @@ -15,7 +15,10 @@ namespace mqtt { static const char *TAG = "mqtt"; -MQTTClientComponent::MQTTClientComponent() { global_mqtt_client = this; } +MQTTClientComponent::MQTTClientComponent() { + global_mqtt_client = this; + this->credentials_.client_id = App.get_name() + "-" + get_mac_address(); +} // Connection void MQTTClientComponent::setup() {