Skip to content

Commit

Permalink
Pass all arguments to MQTTClient parent class constructor (#12)
Browse files Browse the repository at this point in the history
* More proper MQTTClient constructor to pass all arguments to parent class
  • Loading branch information
lostpoint-ru authored Apr 3, 2024
1 parent 34eb09c commit 0376cb0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
6 changes: 6 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
wb-common (2.1.3) stable; urgency=medium

* MQTT client wrapper: forward all constructor arguments to paho.mqtt.client.Client

-- Aleksandr Kazadaev <aleksandr.kazadaev@wirenboard.com> Wed, 03 Apr 2024 15:44:00 +0500

wb-common (2.1.2) stable; urgency=medium

* Fix PKG-INFO
Expand Down
17 changes: 12 additions & 5 deletions wb_common/mqtt_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@


class MQTTClient(paho_socket.Client):
def __init__(self, client_id_prefix: str, broker_url: str = DEFAULT_BROKER_URL, is_threaded: bool = True):
def __init__(
self,
client_id_prefix: str,
broker_url: str = DEFAULT_BROKER_URL,
is_threaded: bool = True,
*args,
**kwargs,
):
self._broker_url = urlparse(broker_url)
self._is_threaded = is_threaded
client_id = self.generate_client_id(client_id_prefix)
transport = "websockets" if self._broker_url.scheme == "ws" else "tcp"
super().__init__(client_id=client_id, transport=transport)
kwargs["client_id"] = self.generate_client_id(client_id_prefix)
kwargs["transport"] = "websockets" if self._broker_url.scheme == "ws" else "tcp"
super().__init__(*args, **kwargs)

@staticmethod
def generate_client_id(client_id_prefix: str, suffix_length: int = 8) -> str:
Expand All @@ -34,7 +41,7 @@ def start(self) -> None:
elif scheme in ["mqtt-tcp", "tcp", "ws"]:
self.connect(self._broker_url.hostname, self._broker_url.port)
else:
raise Exception("Unkown mqtt url scheme: " + scheme)
raise Exception("Unknown mqtt url scheme: " + scheme)

if self._is_threaded:
self.loop_start()
Expand Down

0 comments on commit 0376cb0

Please sign in to comment.