Skip to content

Commit e3fc002

Browse files
driver/resource: mqtt: use callback API v2, bump paho-mqtt>=2.0.0
labgrid's "mqtt" and "dev" extras require paho-mqtt>=1.5.1. With paho-mqtt 2.0.0 [1] released on 2024-02-10, breaking changes were introduced [2]. This was first detected by the scheduled CI jobs via pylint: ************* Module labgrid.resource.mqtt labgrid/resource/mqtt.py:20:17: E1120: No value for argument 'callback_api_version' in constructor call (no-value-for-parameter) ************* Module labgrid.driver.mqtt labgrid/driver/mqtt.py:30:23: E1120: No value for argument 'callback_api_version' in constructor call (no-value-for-parameter) When using 'labgrid-client pw get' with a TasmotaPowerPort resource, the errors looks like this: DEBUG root: Starting session with "ws://labgrid:20408/ws", realm: "realm1" Exception ignored in: <function Client.__del__ at 0x7fd594564a40> Traceback (most recent call last): File "/path/to/venv/lib/python3.11/site-packages/paho/mqtt/client.py", line 874, in __del__ self._reset_sockets() File "/path/to/venv/lib/python3.11/site-packages/paho/mqtt/client.py", line 1133, in _reset_sockets self._sock_close() File "/path/to/venv/lib/python3.11/site-packages/paho/mqtt/client.py", line 1119, in _sock_close if not self._sock: ^^^^^^^^^^ AttributeError: 'Client' object has no attribute '_sock' Traceback (most recent call last): File "/path/to/labgrid/labgrid/factory.py", line 124, in make_resource r = cls(target, name, **args) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "<attrs generated init labgrid.resource.mqtt.TasmotaPowerPort>", line 19, in __init__ self.__attrs_post_init__() File "/path/to/labgrid/labgrid/resource/mqtt.py", line 61, in __attrs_post_init__ super().__attrs_post_init__() File "/path/to/labgrid/labgrid/resource/common.py", line 157, in __attrs_post_init__ self.manager._add_resource(self) File "/path/to/labgrid/labgrid/resource/common.py", line 133, in _add_resource self.on_resource_added(resource) File "/path/to/labgrid/labgrid/resource/mqtt.py", line 29, in on_resource_added self._clients[host] = self._create_mqtt_connection(host) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/path/to/labgrid/labgrid/resource/mqtt.py", line 20, in _create_mqtt_connection client = mqtt.Client() ^^^^^^^^^^^^^ TypeError: Client.__init__() missing 1 required positional argument: 'callback_api_version' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/path/to/labgrid/labgrid/remote/client.py", line 1892, in main args.func(session) File "/path/to/labgrid/labgrid/remote/client.py", line 726, in power target = self._get_target(place) ^^^^^^^^^^^^^^^^^^^^^^^ File "/path/to/labgrid/labgrid/remote/client.py", line 687, in _get_target RemotePlace(target, name=place.name) File "<attrs generated init labgrid.resource.remote.RemotePlace>", line 11, in __init__ self.__attrs_post_init__() File "/path/to/labgrid/labgrid/resource/remote.py", line 101, in __attrs_post_init__ super().__attrs_post_init__() File "/path/to/labgrid/labgrid/resource/common.py", line 157, in __attrs_post_init__ self.manager._add_resource(self) File "/path/to/labgrid/labgrid/resource/common.py", line 133, in _add_resource self.on_resource_added(resource) File "/path/to/labgrid/labgrid/resource/remote.py", line 53, in on_resource_added new = target_factory.make_resource( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/path/to/labgrid/labgrid/factory.py", line 126, in make_resource raise InvalidConfigError( labgrid.exceptions.InvalidConfigError: failed to create TasmotaPowerPort for target 'Target(name='test', env=None)' using {'host': 'mqtt', 'status_topic': 'stat/03374/POWER', 'power_topic': 'cmnd/03374/POWER', 'avail_topic': 'tele/03374/LWT'} This is likely caused by an error in the environment configuration or invalid resource information provided by the coordinator. To fix this, use paho-mqtt's callback API v2 and migrate the on_connect() callback. Require paho-mqtt>=2.0.0 from now on. [1] https://github.com/eclipse/paho.mqtt.python/releases/tag/v2.0.0 [2] https://github.com/eclipse/paho.mqtt.python/blob/master/docs/migrations.rst#change-between-version-1x-and-20 Signed-off-by: Bastian Krause <bst@pengutronix.de>
1 parent 00f466c commit e3fc002

3 files changed

Lines changed: 5 additions & 5 deletions

File tree

labgrid/driver/mqtt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class TasmotaPowerDriver(Driver, PowerProtocol):
2727
def __attrs_post_init__(self):
2828
super().__attrs_post_init__()
2929
import paho.mqtt.client as mqtt
30-
self._client = mqtt.Client()
30+
self._client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
3131

3232
def on_activate(self):
3333
self._client.on_message = self._on_message
@@ -45,7 +45,7 @@ def _on_message(self, client, userdata, msg):
4545
status = False
4646
self._status = status
4747

48-
def _on_connect(self, client, userdata, flags, rc):
48+
def _on_connect(self, client, userdata, flags, reason_code, properties):
4949
client.subscribe(self.power.status_topic)
5050

5151
def _publish(self, topic, payload):

labgrid/resource/mqtt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class MQTTManager(ResourceManager):
1717

1818
def _create_mqtt_connection(self, host):
1919
import paho.mqtt.client as mqtt
20-
client = mqtt.Client()
20+
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
2121
client.connect(host)
2222
client.on_message = self._on_message
2323
client.loop_start()

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ graph = ["graphviz>=0.17.0"]
6060
kasa = ["python-kasa>=0.4.0"]
6161
modbus = ["pyModbusTCP>=0.2.0"]
6262
modbusrtu = ["minimalmodbus>=1.0.2"]
63-
mqtt = ["paho-mqtt>=1.5.1"]
63+
mqtt = ["paho-mqtt>=2.0.0"]
6464
onewire = ["onewire>=0.2"]
6565
pyvisa = [
6666
"pyvisa>=1.11.3",
@@ -101,7 +101,7 @@ dev = [
101101
"minimalmodbus>=1.0.2",
102102

103103
# labgrid[mqtt]
104-
"paho-mqtt>=1.5.1",
104+
"paho-mqtt>=2.0.0",
105105

106106
# labgrid[onewire]
107107
"onewire>=0.2",

0 commit comments

Comments
 (0)