From e0c783d3a7a0c259a512ca2cef3116f548062231 Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Mon, 4 Nov 2024 23:11:46 +0100 Subject: [PATCH 1/4] allow to specify session_id for connect() --- adafruit_minimqtt/adafruit_minimqtt.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index 5028dcd..e09c694 100644 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -399,6 +399,7 @@ def connect( host: Optional[str] = None, port: Optional[int] = None, keep_alive: Optional[int] = None, + session_id: Optional[str] = None, ) -> int: """Initiates connection with the MQTT Broker. Will perform exponential back-off on connect failures. @@ -408,7 +409,8 @@ def connect( :param int port: Network port of the remote broker. :param int keep_alive: Maximum period allowed for communication within single connection attempt, in seconds. - + :param str session_id: unique session ID, + used for multiple simultaneous connections to the same host """ last_exception = None @@ -430,6 +432,7 @@ def connect( host=host, port=port, keep_alive=keep_alive, + session_id=session_id ) self._reset_reconnect_backoff() return ret @@ -482,6 +485,7 @@ def _connect( # noqa: PLR0912, PLR0915, Too many branches, Too many statements host: Optional[str] = None, port: Optional[int] = None, keep_alive: Optional[int] = None, + session_id: Optional[str] = None, ) -> int: """Initiates connection with the MQTT Broker. @@ -489,6 +493,8 @@ def _connect( # noqa: PLR0912, PLR0915, Too many branches, Too many statements :param str host: Hostname or IP address of the remote broker. :param int port: Network port of the remote broker. :param int keep_alive: Maximum period allowed for communication, in seconds. + :param str session_id: unique session ID, + used for multiple simultaneous connections to the same host """ if host: @@ -511,6 +517,7 @@ def _connect( # noqa: PLR0912, PLR0915, Too many branches, Too many statements self.broker, self.port, proto="mqtt:", + session_id=session_id, timeout=self._socket_timeout, is_ssl=self._is_ssl, ssl_context=self._ssl_context, From 58ff51c254b76205cc4928ac33bf5cae33150f59 Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Mon, 4 Nov 2024 23:15:18 +0100 Subject: [PATCH 2/4] add comma --- adafruit_minimqtt/adafruit_minimqtt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index e09c694..510888b 100644 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -432,7 +432,7 @@ def connect( host=host, port=port, keep_alive=keep_alive, - session_id=session_id + session_id=session_id, ) self._reset_reconnect_backoff() return ret From 269d10bdcdb9a945cf26d59a9b8f8637a064305f Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Mon, 4 Nov 2024 23:20:48 +0100 Subject: [PATCH 3/4] suppress warning about too many arguments --- adafruit_minimqtt/adafruit_minimqtt.py | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index 510888b..6b400f5 100644 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -393,7 +393,7 @@ def username_pw_set(self, username: str, password: Optional[str] = None) -> None if password is not None: self._password = password - def connect( + def connect( # noqa: PLR0913, too many arguments in function definition self, clean_session: bool = True, host: Optional[str] = None, @@ -464,22 +464,7 @@ def connect( raise MMQTTException(exc_msg) from last_exception raise MMQTTException(exc_msg) - def _send_bytes( - self, - buffer: Union[bytes, bytearray, memoryview], - ): - bytes_sent: int = 0 - bytes_to_send = len(buffer) - view = memoryview(buffer) - while bytes_sent < bytes_to_send: - try: - bytes_sent += self._sock.send(view[bytes_sent:]) - except OSError as exc: - if exc.errno == EAGAIN: - continue - raise - - def _connect( # noqa: PLR0912, PLR0915, Too many branches, Too many statements + def _connect( # noqa: PLR0912, PLR0913, PLR0915, Too many branches, Too many arguments, Too many statements self, clean_session: bool = True, host: Optional[str] = None, From dfaf68edc7e93f8dbde06debf6be4a47f4776122 Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Thu, 2 Jan 2025 17:48:58 +0100 Subject: [PATCH 4/4] restore _send_bytes() --- adafruit_minimqtt/adafruit_minimqtt.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index 6b400f5..5a21dee 100644 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -464,6 +464,21 @@ def connect( # noqa: PLR0913, too many arguments in function definition raise MMQTTException(exc_msg) from last_exception raise MMQTTException(exc_msg) + def _send_bytes( + self, + buffer: Union[bytes, bytearray, memoryview], + ): + bytes_sent: int = 0 + bytes_to_send = len(buffer) + view = memoryview(buffer) + while bytes_sent < bytes_to_send: + try: + bytes_sent += self._sock.send(view[bytes_sent:]) + except OSError as exc: + if exc.errno == EAGAIN: + continue + raise + def _connect( # noqa: PLR0912, PLR0913, PLR0915, Too many branches, Too many arguments, Too many statements self, clean_session: bool = True,