Skip to content

Commit

Permalink
Enable credential passing for client
Browse files Browse the repository at this point in the history
The client works great for me, I love how intuitive it is to use. Since my mqtt server requires authentication and I saw no builtin way to use it I added it this way to the client startup.

In paho one calls _client.set_username_pw after the client is instantiated but with the context manager setting up the context fails. Because one never enterst the context one never gets a chance to pass the credentials to the client.
  • Loading branch information
gluap authored and frederikaalund committed Apr 13, 2020
1 parent f541f1e commit fabb413
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion asyncio_mqtt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


class Client:
def __init__(self, hostname, port=1883, *, logger=None):
def __init__(self, hostname, port=1883, *, username=None, password=None, logger=None):
self._hostname = hostname
self._port = port
self._loop = asyncio.get_event_loop()
Expand All @@ -38,6 +38,9 @@ def __init__(self, hostname, port=1883, *, logger=None):
if logger is None:
logger = MQTT_LOGGER
self._client.enable_logger(logger)

if username is not None and password is not None:
self._client.username_pw_set(username=username, password=password)

async def connect(self):
self._client.connect(self._hostname, self._port, 60)
Expand Down

0 comments on commit fabb413

Please sign in to comment.