mqtt CallbackAPIVersion fix for paho.mqtt >= 2.0.0 #386

Merged
blacklight merged 2 commits from revil-O/platypush:master into master 2024-04-08 23:04:39 +02:00
Contributor

fix to work with paho.mqtt >= 2.0.0

fix to work with paho.mqtt >= 2.0.0
revil-O added 1 commit 2024-04-08 16:09:06 +02:00
Owner

Good catch! I missed this breaking change in paho.mqtt 2.0.0.

Let me quickly test it with paho.mqtt < 2.0.0 to make sure that it still works as expected.

Good catch! I missed this breaking change in paho.mqtt 2.0.0. Let me quickly test it with paho.mqtt < 2.0.0 to make sure that it still works as expected.
Owner

I'm afraid it won't work on paho.mqtt < 2.0.0 because of different constructor signature + non-existing CallbackAPIVersion enum:

2024-04-08 22:37:37,478|WARNING|utils|Could not initialize plugin zwave.mqtt
2024-04-08 22:37:37,478|ERROR|utils|module 'paho.mqtt.client' has no attribute 'CallbackAPIVersion'
Traceback (most recent call last):
  File "/home/blacklight/git_tree/platypush/platypush/utils/__init__.py", line 557, in get_enabled_plugins
    plugin = get_plugin(name)
             ^^^^^^^^^^^^^^^^
  File "/home/blacklight/git_tree/platypush/platypush/context/__init__.py", line 187, in get_plugin
    _ctx.plugins[name] = plugin_class(**plugin_conf)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/blacklight/git_tree/platypush/platypush/plugins/zwave/mqtt/__init__.py", line 173, in __init__
    super().__init__(
  File "/home/blacklight/git_tree/platypush/platypush/plugins/mqtt/__init__.py", line 137, in __init__
    self._get_client(
  File "/home/blacklight/git_tree/platypush/platypush/plugins/mqtt/__init__.py", line 343, in _get_client
    ] = MqttClient(  # pylint: disable=E1125
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/blacklight/git_tree/platypush/platypush/plugins/mqtt/_client.py", line 40, in __init__
    mqtt.Client.__init__(self, mqtt.CallbackAPIVersion.VERSION1, *args, client_id=self.client_id, **kwargs)
                               ^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'paho.mqtt.client' has no attribute 'CallbackAPIVersion'

I wish that callback_api_version had been an optional parameter with VERSION1 default rather than implemented as such a hard breaking change.

Since Platypush still supports older distros and versions of Python, and since probably it'll take a while for paho-mqtt 2.x to reach upstream in most of the distros, I'd propose to add a conditional logic that either checks paho.mqtt.__version__ or if paho.mqtt.CallbackAPIVersion exists, so everything should work with both versions.

I'm afraid it won't work on paho.mqtt < 2.0.0 because of different constructor signature + non-existing `CallbackAPIVersion` enum: ``` 2024-04-08 22:37:37,478|WARNING|utils|Could not initialize plugin zwave.mqtt 2024-04-08 22:37:37,478|ERROR|utils|module 'paho.mqtt.client' has no attribute 'CallbackAPIVersion' Traceback (most recent call last): File "/home/blacklight/git_tree/platypush/platypush/utils/__init__.py", line 557, in get_enabled_plugins plugin = get_plugin(name) ^^^^^^^^^^^^^^^^ File "/home/blacklight/git_tree/platypush/platypush/context/__init__.py", line 187, in get_plugin _ctx.plugins[name] = plugin_class(**plugin_conf) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/blacklight/git_tree/platypush/platypush/plugins/zwave/mqtt/__init__.py", line 173, in __init__ super().__init__( File "/home/blacklight/git_tree/platypush/platypush/plugins/mqtt/__init__.py", line 137, in __init__ self._get_client( File "/home/blacklight/git_tree/platypush/platypush/plugins/mqtt/__init__.py", line 343, in _get_client ] = MqttClient( # pylint: disable=E1125 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/blacklight/git_tree/platypush/platypush/plugins/mqtt/_client.py", line 40, in __init__ mqtt.Client.__init__(self, mqtt.CallbackAPIVersion.VERSION1, *args, client_id=self.client_id, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: module 'paho.mqtt.client' has no attribute 'CallbackAPIVersion' ``` I wish that `callback_api_version` had been an optional parameter with `VERSION1` default rather than implemented as such a hard breaking change. Since Platypush still supports older distros and versions of Python, and since probably it'll take a while for paho-mqtt 2.x to reach upstream in most of the distros, I'd propose to add a conditional logic that either checks `paho.mqtt.__version__` or if `paho.mqtt.CallbackAPIVersion` exists, so everything should work with both versions.
blacklight added 1 commit 2024-04-08 23:02:04 +02:00
Owner

@revil-O I've added a commit that introduces a compatibility layer for all versions of paho.mqtt - basically it looks if paho.mqtt.CallbackAPIVersion exists and initializes the constructor accordingly.

Thanks for your contribution!

@revil-O I've added a commit that introduces a compatibility layer for all versions of paho.mqtt - basically it looks if `paho.mqtt.CallbackAPIVersion` exists and initializes the constructor accordingly. Thanks for your contribution!
blacklight merged commit b1911e75a7 into master 2024-04-08 23:04:39 +02:00
Author
Contributor

Hi, sorry for incomplete pull-request; i forgot to test paho-mqtt < 2.0.0.
thx to blacklight for complete test and implementation !

greets

Hi, sorry for incomplete pull-request; i forgot to test paho-mqtt < 2.0.0. thx to blacklight for complete test and implementation ! greets
Sign in to join this conversation.
No description provided.