Add MQTT auth support

This commit adds config options and support for connecting to an mqtt
broker with auth.

Change-Id: I1fad16d2cd3941dd414e7469c1ecb6a2ba96e1f9
This commit is contained in:
Matthew Treinish 2016-07-27 00:25:46 -04:00
parent 964b3fdb33
commit 8a4a69afef
No known key found for this signature in database
GPG Key ID: FD12A0F214C9E177
2 changed files with 21 additions and 1 deletions

View File

@ -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
--------------

View File

@ -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: