From 8a4a69afef4869d660cf7d40b757e778390fe5be Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Wed, 27 Jul 2016 00:25:46 -0400 Subject: [PATCH] Add MQTT auth support This commit adds config options and support for connecting to an mqtt broker with auth. Change-Id: I1fad16d2cd3941dd414e7469c1ecb6a2ba96e1f9 --- README.rst | 4 ++++ germqtt/germqtt.py | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 25c711a..c3972ab 100644 --- a/README.rst +++ b/README.rst @@ -51,6 +51,10 @@ can set: if your broker uses a non-default port. * **keepalive** - Used to set the keepalive time for connections to the MQTT broker. By default this is set to 60 seconds. + * **username** - Used to set the auth username to connect to the MQTT broker + with. + * **password** - Used to set the auth password to connect to the MQTT broker + with. A username must be set for this option to be used. Other Settings -------------- diff --git a/germqtt/germqtt.py b/germqtt/germqtt.py index 97a2016..7da7435 100644 --- a/germqtt/germqtt.py +++ b/germqtt/germqtt.py @@ -116,10 +116,26 @@ def _main(args, config): else: keepalive = 60 + # Configure auth + auth = None + if config.has_option('mqtt', 'username'): + mqtt_username = config.get('mqtt', 'username') + else: + mqtt_username = None + if config.has_option('mqtt', 'password'): + mqtt_password = config.get('mqtt', 'password') + else: + mqtt_password = None + if mqtt_username: + auth = {'username': mqtt_username} + if mqtt_password: + auth['password'] = mqtt_password + mqttqueue = PushMQTT( config.get('mqtt', 'hostname'), port=mqtt_port, - keepalive=keepalive) + keepalive=keepalive, + auth=auth) base_topic = config.get('mqtt', 'topic') while True: