Skip to content

Commit 99ec693

Browse files
Copilotgonzalocasas
andcommitted
Add client ID generation and update CI workflow versions
- Add client_id parameter to MqttTransport constructor with auto-generation - Support both explicit client_id and auto-generated UUID-based client_id - Update workflow files to use compas-dev/compas-actions.build@v4 - Update compatibility tests to match new client_id behavior - Add test for custom client_id functionality Co-authored-by: gonzalocasas <933277+gonzalocasas@users.noreply.github.com>
1 parent 545d0da commit 99ec693

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
python: ['3.9', '3.10', '3.11']
1919

2020
steps:
21-
- uses: compas-dev/compas-actions.build@v3
21+
- uses: compas-dev/compas-actions.build@v4
2222
with:
2323
python: ${{ matrix.python }}
2424
invoke_lint: true
@@ -54,7 +54,7 @@ jobs:
5454
run: |
5555
docker run -d --name nanomq -p 1883:1883 -p 8083:8083 -p 8883:8883 emqx/nanomq:latest
5656
docker ps -a
57-
- uses: compas-dev/compas-actions.build@v3
57+
- uses: compas-dev/compas-actions.build@v4
5858
with:
5959
python: '3.11'
6060
invoke_lint: false

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
python: ['3.9', '3.10', '3.11']
1515

1616
steps:
17-
- uses: compas-dev/compas-actions.build@v3
17+
- uses: compas-dev/compas-actions.build@v4
1818
with:
1919
python: ${{ matrix.python }}
2020
invoke_lint: true
@@ -31,7 +31,7 @@ jobs:
3131
run: |
3232
docker run -d --name nanomq -p 1883:1883 -p 8083:8083 -p 8883:8883 emqx/nanomq:latest
3333
docker ps -a
34-
- uses: compas-dev/compas-actions.build@v3
34+
- uses: compas-dev/compas-actions.build@v4
3535
with:
3636
python: '3.11'
3737
invoke_lint: false

src/compas_eve/mqtt/mqtt_paho.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from ..event_emitter import EventEmitterMixin
33

44
import paho.mqtt.client as mqtt
5+
import uuid
56

67
try:
78
from paho.mqtt.enums import CallbackAPIVersion
@@ -21,18 +22,25 @@ class MqttTransport(Transport, EventEmitterMixin):
2122
you are running a local broker on your machine.
2223
port : int
2324
MQTT broker port, defaults to ``1883``.
25+
client_id : str, optional
26+
Client ID for the MQTT connection. If not provided, a unique ID will be generated.
2427
"""
2528

26-
def __init__(self, host, port=1883, *args, **kwargs):
29+
def __init__(self, host, port=1883, client_id=None, *args, **kwargs):
2730
super(MqttTransport, self).__init__(*args, **kwargs)
2831
self.host = host
2932
self.port = port
3033
self._is_connected = False
3134
self._local_callbacks = {}
35+
36+
# Generate client ID if not provided
37+
if client_id is None:
38+
client_id = f"compas_eve_{uuid.uuid4().hex[:8]}"
39+
3240
if PAHO_MQTT_V2_AVAILABLE:
33-
self.client = mqtt.Client(callback_api_version=CallbackAPIVersion.VERSION1)
41+
self.client = mqtt.Client(client_id=client_id, callback_api_version=CallbackAPIVersion.VERSION1)
3442
else:
35-
self.client = mqtt.Client() # todo: generate client_id
43+
self.client = mqtt.Client(client_id=client_id)
3644
self.client.on_connect = self._on_connect
3745
self.client.connect(self.host, self.port)
3846
self.client.loop_start()

tests/unit/test_mqtt_paho_compatibility.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ def test_paho_mqtt_v1_compatibility():
1919
# This should work as if paho-mqtt 1.x is installed
2020
transport = MqttTransport("localhost")
2121

22-
# Should have called mqtt.Client() without parameters
23-
mock_client_class.assert_called_once_with()
22+
# Should have called mqtt.Client() with client_id parameter only (no callback_api_version)
23+
mock_client_class.assert_called_once()
24+
call_args = mock_client_class.call_args
25+
assert "client_id" in call_args.kwargs
26+
assert call_args.kwargs["client_id"].startswith("compas_eve_")
27+
assert "callback_api_version" not in call_args.kwargs
2428
assert transport.client == mock_client
2529

2630

@@ -38,8 +42,13 @@ def test_paho_mqtt_v2_compatibility():
3842
# This should work as if paho-mqtt 2.x is installed
3943
transport = MqttTransport("localhost")
4044

41-
# Should have called mqtt.Client() with callback_api_version parameter
42-
mock_client_class.assert_called_once_with(callback_api_version=CallbackAPIVersion.VERSION1)
45+
# Should have called mqtt.Client() with both client_id and callback_api_version parameters
46+
mock_client_class.assert_called_once()
47+
call_args = mock_client_class.call_args
48+
assert "client_id" in call_args.kwargs
49+
assert call_args.kwargs["client_id"].startswith("compas_eve_")
50+
assert "callback_api_version" in call_args.kwargs
51+
assert call_args.kwargs["callback_api_version"] == CallbackAPIVersion.VERSION1
4352
assert transport.client == mock_client
4453

4554

@@ -55,3 +64,21 @@ def test_version_detection():
5564
# If v2 is not available, import should fail
5665
with pytest.raises(ImportError):
5766
from paho.mqtt.enums import CallbackAPIVersion
67+
68+
69+
def test_custom_client_id():
70+
"""Test that custom client_id parameter works correctly."""
71+
with patch("paho.mqtt.client.Client") as mock_client_class:
72+
mock_client = Mock()
73+
mock_client_class.return_value = mock_client
74+
75+
# Test with custom client_id
76+
custom_client_id = "my_custom_client_id"
77+
transport = MqttTransport("localhost", client_id=custom_client_id)
78+
79+
# Should have called mqtt.Client() with the custom client_id
80+
mock_client_class.assert_called_once()
81+
call_args = mock_client_class.call_args
82+
assert "client_id" in call_args.kwargs
83+
assert call_args.kwargs["client_id"] == custom_client_id
84+
assert transport.client == mock_client

0 commit comments

Comments
 (0)