Generate random MQTT client ID (#42934)

* Generate random MQTT client ID

* Add comment

* Apply suggestions from code review

Co-authored-by: Franck Nijhof <git@frenck.dev>

Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
Erik Montnemery 2020-11-07 16:27:09 +01:00 committed by GitHub
parent 9d650245ee
commit 7f640c4a2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,6 +10,7 @@ import os
import ssl import ssl
import time import time
from typing import Any, Callable, List, Optional, Union from typing import Any, Callable, List, Optional, Union
import uuid
import attr import attr
import certifi import certifi
@ -711,9 +712,10 @@ class MQTT:
client_id = self.conf.get(CONF_CLIENT_ID) client_id = self.conf.get(CONF_CLIENT_ID)
if client_id is None: if client_id is None:
self._mqttc = mqtt.Client(protocol=proto) # PAHO MQTT relies on the MQTT server to generate random client IDs.
else: # However, that feature is not mandatory so we generate our own.
self._mqttc = mqtt.Client(client_id, protocol=proto) client_id = mqtt.base62(uuid.uuid4().int, padding=22)
self._mqttc = mqtt.Client(client_id, protocol=proto)
# Enable logging # Enable logging
self._mqttc.enable_logger() self._mqttc.enable_logger()