Fix MQTT client_id changed (#579)

Fixes https://github.com/esphome/issues/issues/323
This commit is contained in:
Otto Winter 2019-05-31 10:37:10 +02:00
parent 605e365405
commit ebb5d58c14
No known key found for this signature in database
GPG key ID: DB66C0BE6013F97E
2 changed files with 7 additions and 3 deletions

View file

@ -104,7 +104,7 @@ CONFIG_SCHEMA = cv.All(cv.Schema({
cv.Optional(CONF_PORT, default=1883): cv.port, cv.Optional(CONF_PORT, default=1883): cv.port,
cv.Optional(CONF_USERNAME, default=''): cv.string, cv.Optional(CONF_USERNAME, default=''): cv.string,
cv.Optional(CONF_PASSWORD, 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, 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_RETAIN, default=True): cv.boolean,
cv.Optional(CONF_DISCOVERY_PREFIX, default="homeassistant"): cv.publish_topic, cv.Optional(CONF_DISCOVERY_PREFIX, default="homeassistant"): cv.publish_topic,
@ -161,6 +161,7 @@ def to_code(config):
cg.add(var.set_broker_port(config[CONF_PORT])) cg.add(var.set_broker_port(config[CONF_PORT]))
cg.add(var.set_username(config[CONF_USERNAME])) cg.add(var.set_username(config[CONF_USERNAME]))
cg.add(var.set_password(config[CONF_PASSWORD])) cg.add(var.set_password(config[CONF_PASSWORD]))
if CONF_CLIENT_ID in config:
cg.add(var.set_client_id(config[CONF_CLIENT_ID])) cg.add(var.set_client_id(config[CONF_CLIENT_ID]))
discovery = config[CONF_DISCOVERY] discovery = config[CONF_DISCOVERY]

View file

@ -15,7 +15,10 @@ namespace mqtt {
static const char *TAG = "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 // Connection
void MQTTClientComponent::setup() { void MQTTClientComponent::setup() {