mirror of
https://github.com/Tautulli/Tautulli.git
synced 2025-07-16 02:02:58 -07:00
Bump paho-mqtt from 1.6.1 to 2.0.0 (#2288)
* Bump paho-mqtt from 1.6.1 to 2.0.0 Bumps [paho-mqtt](https://github.com/eclipse/paho.mqtt.python) from 1.6.1 to 2.0.0. - [Release notes](https://github.com/eclipse/paho.mqtt.python/releases) - [Changelog](https://github.com/eclipse/paho.mqtt.python/blob/master/ChangeLog.txt) - [Commits](https://github.com/eclipse/paho.mqtt.python/compare/v1.6.1...v2.0.0) --- updated-dependencies: - dependency-name: paho-mqtt dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * Update paho-mqtt==2.0.0 --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> [skip ci]
This commit is contained in:
parent
75a1750a4e
commit
f82aecb88c
12 changed files with 2643 additions and 1443 deletions
|
@ -5,7 +5,7 @@
|
|||
# and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
#
|
||||
# The Eclipse Public License is available at
|
||||
# http://www.eclipse.org/legal/epl-v10.html
|
||||
# http://www.eclipse.org/legal/epl-v20.html
|
||||
# and the Eclipse Distribution License is available at
|
||||
# http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
#
|
||||
|
@ -18,20 +18,58 @@ of messages in a one-shot manner. In other words, they are useful for the
|
|||
situation where you have a single/multiple messages you want to publish to a
|
||||
broker, then disconnect and nothing else is required.
|
||||
"""
|
||||
from __future__ import absolute_import
|
||||
from __future__ import annotations
|
||||
|
||||
import collections
|
||||
from collections.abc import Iterable
|
||||
from typing import TYPE_CHECKING, Any, List, Tuple, Union
|
||||
|
||||
try:
|
||||
from collections.abc import Iterable
|
||||
except ImportError:
|
||||
from collections import Iterable
|
||||
from paho.mqtt.enums import CallbackAPIVersion
|
||||
from paho.mqtt.properties import Properties
|
||||
from paho.mqtt.reasoncodes import ReasonCode
|
||||
|
||||
from .. import mqtt
|
||||
from . import client as paho
|
||||
|
||||
if TYPE_CHECKING:
|
||||
try:
|
||||
from typing import NotRequired, Required, TypedDict # type: ignore
|
||||
except ImportError:
|
||||
from typing_extensions import NotRequired, Required, TypedDict
|
||||
|
||||
def _do_publish(client):
|
||||
try:
|
||||
from typing import Literal
|
||||
except ImportError:
|
||||
from typing_extensions import Literal # type: ignore
|
||||
|
||||
|
||||
|
||||
class AuthParameter(TypedDict, total=False):
|
||||
username: Required[str]
|
||||
password: NotRequired[str]
|
||||
|
||||
|
||||
class TLSParameter(TypedDict, total=False):
|
||||
ca_certs: Required[str]
|
||||
certfile: NotRequired[str]
|
||||
keyfile: NotRequired[str]
|
||||
tls_version: NotRequired[int]
|
||||
ciphers: NotRequired[str]
|
||||
insecure: NotRequired[bool]
|
||||
|
||||
|
||||
class MessageDict(TypedDict, total=False):
|
||||
topic: Required[str]
|
||||
payload: NotRequired[paho.PayloadType]
|
||||
qos: NotRequired[int]
|
||||
retain: NotRequired[bool]
|
||||
|
||||
MessageTuple = Tuple[str, paho.PayloadType, int, bool]
|
||||
|
||||
MessagesList = List[Union[MessageDict, MessageTuple]]
|
||||
|
||||
|
||||
def _do_publish(client: paho.Client):
|
||||
"""Internal function"""
|
||||
|
||||
message = client._userdata.popleft()
|
||||
|
@ -44,21 +82,18 @@ def _do_publish(client):
|
|||
raise TypeError('message must be a dict, tuple, or list')
|
||||
|
||||
|
||||
def _on_connect(client, userdata, flags, rc):
|
||||
"""Internal callback"""
|
||||
#pylint: disable=invalid-name, unused-argument
|
||||
|
||||
if rc == 0:
|
||||
def _on_connect(client: paho.Client, userdata: MessagesList, flags, reason_code, properties):
|
||||
"""Internal v5 callback"""
|
||||
if reason_code == 0:
|
||||
if len(userdata) > 0:
|
||||
_do_publish(client)
|
||||
else:
|
||||
raise mqtt.MQTTException(paho.connack_string(rc))
|
||||
raise mqtt.MQTTException(paho.connack_string(reason_code))
|
||||
|
||||
def _on_connect_v5(client, userdata, flags, rc, properties):
|
||||
"""Internal v5 callback"""
|
||||
_on_connect(client, userdata, flags, rc)
|
||||
|
||||
def _on_publish(client, userdata, mid):
|
||||
def _on_publish(
|
||||
client: paho.Client, userdata: collections.deque[MessagesList], mid: int, reason_codes: ReasonCode, properties: Properties,
|
||||
) -> None:
|
||||
"""Internal callback"""
|
||||
#pylint: disable=unused-argument
|
||||
|
||||
|
@ -68,16 +103,26 @@ def _on_publish(client, userdata, mid):
|
|||
_do_publish(client)
|
||||
|
||||
|
||||
def multiple(msgs, hostname="localhost", port=1883, client_id="", keepalive=60,
|
||||
will=None, auth=None, tls=None, protocol=paho.MQTTv311,
|
||||
transport="tcp", proxy_args=None):
|
||||
def multiple(
|
||||
msgs: MessagesList,
|
||||
hostname: str = "localhost",
|
||||
port: int = 1883,
|
||||
client_id: str = "",
|
||||
keepalive: int = 60,
|
||||
will: MessageDict | None = None,
|
||||
auth: AuthParameter | None = None,
|
||||
tls: TLSParameter | None = None,
|
||||
protocol: int = paho.MQTTv311,
|
||||
transport: Literal["tcp", "websockets"] = "tcp",
|
||||
proxy_args: Any | None = None,
|
||||
) -> None:
|
||||
"""Publish multiple messages to a broker, then disconnect cleanly.
|
||||
|
||||
This function creates an MQTT client, connects to a broker and publishes a
|
||||
list of messages. Once the messages have been delivered, it disconnects
|
||||
cleanly from the broker.
|
||||
|
||||
msgs : a list of messages to publish. Each message is either a dict or a
|
||||
:param msgs: a list of messages to publish. Each message is either a dict or a
|
||||
tuple.
|
||||
|
||||
If a dict, only the topic must be present. Default values will be
|
||||
|
@ -94,30 +139,30 @@ def multiple(msgs, hostname="localhost", port=1883, client_id="", keepalive=60,
|
|||
If a tuple, then it must be of the form:
|
||||
("<topic>", "<payload>", qos, retain)
|
||||
|
||||
hostname : a string containing the address of the broker to connect to.
|
||||
:param str hostname: the address of the broker to connect to.
|
||||
Defaults to localhost.
|
||||
|
||||
port : the port to connect to the broker on. Defaults to 1883.
|
||||
:param int port: the port to connect to the broker on. Defaults to 1883.
|
||||
|
||||
client_id : the MQTT client id to use. If "" or None, the Paho library will
|
||||
:param str client_id: the MQTT client id to use. If "" or None, the Paho library will
|
||||
generate a client id automatically.
|
||||
|
||||
keepalive : the keepalive timeout value for the client. Defaults to 60
|
||||
:param int keepalive: the keepalive timeout value for the client. Defaults to 60
|
||||
seconds.
|
||||
|
||||
will : a dict containing will parameters for the client: will = {'topic':
|
||||
:param will: a dict containing will parameters for the client: will = {'topic':
|
||||
"<topic>", 'payload':"<payload">, 'qos':<qos>, 'retain':<retain>}.
|
||||
Topic is required, all other parameters are optional and will
|
||||
default to None, 0 and False respectively.
|
||||
Defaults to None, which indicates no will should be used.
|
||||
|
||||
auth : a dict containing authentication parameters for the client:
|
||||
:param auth: a dict containing authentication parameters for the client:
|
||||
auth = {'username':"<username>", 'password':"<password>"}
|
||||
Username is required, password is optional and will default to None
|
||||
if not provided.
|
||||
Defaults to None, which indicates no authentication is to be used.
|
||||
|
||||
tls : a dict containing TLS configuration parameters for the client:
|
||||
:param tls: a dict containing TLS configuration parameters for the client:
|
||||
dict = {'ca_certs':"<ca_certs>", 'certfile':"<certfile>",
|
||||
'keyfile':"<keyfile>", 'tls_version':"<tls_version>",
|
||||
'ciphers':"<ciphers">, 'insecure':"<bool>"}
|
||||
|
@ -128,23 +173,28 @@ def multiple(msgs, hostname="localhost", port=1883, client_id="", keepalive=60,
|
|||
processed using the tls_set_context method.
|
||||
Defaults to None, which indicates that TLS should not be used.
|
||||
|
||||
transport : set to "tcp" to use the default setting of transport which is
|
||||
:param str transport: set to "tcp" to use the default setting of transport which is
|
||||
raw TCP. Set to "websockets" to use WebSockets as the transport.
|
||||
proxy_args: a dictionary that will be given to the client.
|
||||
|
||||
:param proxy_args: a dictionary that will be given to the client.
|
||||
"""
|
||||
|
||||
if not isinstance(msgs, Iterable):
|
||||
raise TypeError('msgs must be an iterable')
|
||||
if len(msgs) == 0:
|
||||
raise ValueError('msgs is empty')
|
||||
|
||||
client = paho.Client(
|
||||
CallbackAPIVersion.VERSION2,
|
||||
client_id=client_id,
|
||||
userdata=collections.deque(msgs),
|
||||
protocol=protocol,
|
||||
transport=transport,
|
||||
)
|
||||
|
||||
client = paho.Client(client_id=client_id, userdata=collections.deque(msgs),
|
||||
protocol=protocol, transport=transport)
|
||||
|
||||
client.enable_logger()
|
||||
client.on_publish = _on_publish
|
||||
if protocol == mqtt.client.MQTTv5:
|
||||
client.on_connect = _on_connect_v5
|
||||
else:
|
||||
client.on_connect = _on_connect
|
||||
client.on_connect = _on_connect # type: ignore
|
||||
|
||||
if proxy_args is not None:
|
||||
client.proxy_set(**proxy_args)
|
||||
|
@ -164,7 +214,8 @@ def multiple(msgs, hostname="localhost", port=1883, client_id="", keepalive=60,
|
|||
if tls is not None:
|
||||
if isinstance(tls, dict):
|
||||
insecure = tls.pop('insecure', False)
|
||||
client.tls_set(**tls)
|
||||
# mypy don't get that tls no longer contains the key insecure
|
||||
client.tls_set(**tls) # type: ignore[misc]
|
||||
if insecure:
|
||||
# Must be set *after* the `client.tls_set()` call since it sets
|
||||
# up the SSL context that `client.tls_insecure_set` alters.
|
||||
|
@ -177,49 +228,62 @@ def multiple(msgs, hostname="localhost", port=1883, client_id="", keepalive=60,
|
|||
client.loop_forever()
|
||||
|
||||
|
||||
def single(topic, payload=None, qos=0, retain=False, hostname="localhost",
|
||||
port=1883, client_id="", keepalive=60, will=None, auth=None,
|
||||
tls=None, protocol=paho.MQTTv311, transport="tcp", proxy_args=None):
|
||||
def single(
|
||||
topic: str,
|
||||
payload: paho.PayloadType = None,
|
||||
qos: int = 0,
|
||||
retain: bool = False,
|
||||
hostname: str = "localhost",
|
||||
port: int = 1883,
|
||||
client_id: str = "",
|
||||
keepalive: int = 60,
|
||||
will: MessageDict | None = None,
|
||||
auth: AuthParameter | None = None,
|
||||
tls: TLSParameter | None = None,
|
||||
protocol: int = paho.MQTTv311,
|
||||
transport: Literal["tcp", "websockets"] = "tcp",
|
||||
proxy_args: Any | None = None,
|
||||
) -> None:
|
||||
"""Publish a single message to a broker, then disconnect cleanly.
|
||||
|
||||
This function creates an MQTT client, connects to a broker and publishes a
|
||||
single message. Once the message has been delivered, it disconnects cleanly
|
||||
from the broker.
|
||||
|
||||
topic : the only required argument must be the topic string to which the
|
||||
:param str topic: the only required argument must be the topic string to which the
|
||||
payload will be published.
|
||||
|
||||
payload : the payload to be published. If "" or None, a zero length payload
|
||||
:param payload: the payload to be published. If "" or None, a zero length payload
|
||||
will be published.
|
||||
|
||||
qos : the qos to use when publishing, default to 0.
|
||||
:param int qos: the qos to use when publishing, default to 0.
|
||||
|
||||
retain : set the message to be retained (True) or not (False).
|
||||
:param bool retain: set the message to be retained (True) or not (False).
|
||||
|
||||
hostname : a string containing the address of the broker to connect to.
|
||||
:param str hostname: the address of the broker to connect to.
|
||||
Defaults to localhost.
|
||||
|
||||
port : the port to connect to the broker on. Defaults to 1883.
|
||||
:param int port: the port to connect to the broker on. Defaults to 1883.
|
||||
|
||||
client_id : the MQTT client id to use. If "" or None, the Paho library will
|
||||
:param str client_id: the MQTT client id to use. If "" or None, the Paho library will
|
||||
generate a client id automatically.
|
||||
|
||||
keepalive : the keepalive timeout value for the client. Defaults to 60
|
||||
:param int keepalive: the keepalive timeout value for the client. Defaults to 60
|
||||
seconds.
|
||||
|
||||
will : a dict containing will parameters for the client: will = {'topic':
|
||||
:param will: a dict containing will parameters for the client: will = {'topic':
|
||||
"<topic>", 'payload':"<payload">, 'qos':<qos>, 'retain':<retain>}.
|
||||
Topic is required, all other parameters are optional and will
|
||||
default to None, 0 and False respectively.
|
||||
Defaults to None, which indicates no will should be used.
|
||||
|
||||
auth : a dict containing authentication parameters for the client:
|
||||
auth = {'username':"<username>", 'password':"<password>"}
|
||||
:param auth: a dict containing authentication parameters for the client:
|
||||
Username is required, password is optional and will default to None
|
||||
auth = {'username':"<username>", 'password':"<password>"}
|
||||
if not provided.
|
||||
Defaults to None, which indicates no authentication is to be used.
|
||||
|
||||
tls : a dict containing TLS configuration parameters for the client:
|
||||
:param tls: a dict containing TLS configuration parameters for the client:
|
||||
dict = {'ca_certs':"<ca_certs>", 'certfile':"<certfile>",
|
||||
'keyfile':"<keyfile>", 'tls_version':"<tls_version>",
|
||||
'ciphers':"<ciphers">, 'insecure':"<bool>"}
|
||||
|
@ -230,12 +294,13 @@ def single(topic, payload=None, qos=0, retain=False, hostname="localhost",
|
|||
Alternatively, tls input can be an SSLContext object, which will be
|
||||
processed using the tls_set_context method.
|
||||
|
||||
transport : set to "tcp" to use the default setting of transport which is
|
||||
:param transport: set to "tcp" to use the default setting of transport which is
|
||||
raw TCP. Set to "websockets" to use WebSockets as the transport.
|
||||
proxy_args: a dictionary that will be given to the client.
|
||||
|
||||
:param proxy_args: a dictionary that will be given to the client.
|
||||
"""
|
||||
|
||||
msg = {'topic':topic, 'payload':payload, 'qos':qos, 'retain':retain}
|
||||
msg: MessageDict = {'topic':topic, 'payload':payload, 'qos':qos, 'retain':retain}
|
||||
|
||||
multiple([msg], hostname, port, client_id, keepalive, will, auth, tls,
|
||||
protocol, transport, proxy_args)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue