Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 19, 2025

This PR adds backward-compatible support for paho-mqtt 2.0 versioned callbacks while maintaining full compatibility with paho-mqtt 1.x installations, includes client ID generation functionality, and modernizes dependency management using optional dependencies.

Problem

The paho-mqtt library version 2.0 introduced breaking changes to the Client() constructor, requiring a callback_api_version parameter. Without this parameter, creating an MQTT client in paho-mqtt 2.0 fails with an error. Additionally, the original implementation lacked proper client ID management, which can cause connection issues with MQTT brokers. The project also used outdated dependency management with separate requirements-dev.txt instead of modern optional dependencies.

Solution

This implementation automatically detects the available paho-mqtt version and uses the appropriate client creation method with proper client ID handling, while modernizing the dependency management approach:

# Generate client ID if not provided
if client_id is None:
    client_id = f"compas_eve_{uuid.uuid4().hex[:8]}"

# For paho-mqtt 2.0+
self.client = mqtt.Client(client_id=client_id, callback_api_version=CallbackAPIVersion.VERSION1)

# For paho-mqtt 1.x (fallback)
self.client = mqtt.Client(client_id=client_id)

Key Features

  • Automatic version detection: Uses try/except import to detect paho-mqtt 2.x availability
  • Graceful fallback: Maintains compatibility with existing paho-mqtt 1.x installations
  • Client ID generation: Automatically generates unique UUID-based client IDs when none provided
  • Custom client ID support: Allows overriding the client ID via constructor parameter
  • Modern dependency management: Uses [dev] optional dependencies instead of requirements-dev.txt
  • No breaking changes: Existing code continues to work unchanged
  • Updated dependency constraint: Now allows paho-mqtt >=1, <3
  • CI improvements: Updated GitHub Actions workflows to v4 and removed Python 3.9 support

Usage

# Auto-generated client ID
transport = MqttTransport("broker.example.com")

# Custom client ID
transport = MqttTransport("broker.example.com", client_id="my_client")

Development Setup

Development dependencies are now managed as optional dependencies:

# Install with development dependencies
pip install -e .[dev]

Files Changed

  • src/compas_eve/mqtt/mqtt_paho.py: Core implementation with version detection and client ID support
  • requirements.txt: Updated paho-mqtt constraint to allow 2.x
  • tests/unit/test_mqtt_paho_compatibility.py: Updated compatibility tests with client ID verification
  • examples/mqtt_compatibility_demo.py: User-facing demonstration
  • CHANGELOG.md: Documentation of changes
  • MANIFEST.in: Include examples in distribution
  • setup.py: Added [dev] optional dependencies for modern dependency management
  • CONTRIBUTING.md: Updated development setup instructions
  • .github/workflows/build.yml & .github/workflows/release.yml: Updated to v4 actions, [dev] dependencies, and Python 3.10+ support

The implementation is minimal and surgical, changing only the necessary lines to support both versions while adding essential client ID functionality and modern dependency management practices, preserving all existing behavior.

Fixes #8.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copy link
Contributor Author

Copilot AI commented Aug 19, 2025

@gonzalocasas 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

Copilot AI changed the title [WIP] Add support for MQTT-PAHO 2.0 versioned callbacks Add support for MQTT-PAHO 2.0 versioned callbacks Aug 19, 2025
Copilot AI requested a review from gonzalocasas August 19, 2025 12:46
Comment on lines 32 to 35
if PAHO_MQTT_V2_AVAILABLE:
self.client = mqtt.Client(callback_api_version=CallbackAPIVersion.VERSION1)
else:
self.client = mqtt.Client() # todo: generate client_id
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add client id generation to this, also allow to override or define the client id passing that in the constructor of the transport class

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added client ID generation functionality. The MqttTransport constructor now accepts an optional client_id parameter and automatically generates a unique UUID-based client ID when none is provided. Both paho-mqtt 1.x and 2.x client creation now include the client_id parameter. See commit 99ec693.

@gonzalocasas
Copy link
Member

@copilot update to compas-dev/compas-actions.build@v4 to try to fix the CI fail

- 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 <[email protected]>
Copy link
Contributor Author

Copilot AI commented Aug 19, 2025

@copilot update to compas-dev/compas-actions.build@v4 to try to fix the CI fail

Updated both build.yml and release.yml workflows to use compas-dev/compas-actions.build@v4 to address CI failures. See commit 99ec693.

Copilot AI changed the title Add support for MQTT-PAHO 2.0 versioned callbacks Add support for MQTT-PAHO 2.0 versioned callbacks with client ID generation Aug 19, 2025
Copilot AI requested a review from gonzalocasas August 19, 2025 13:22
@gonzalocasas
Copy link
Member

@copilot please update to install requirements-dev.txt as [dev] optional dependencies

- Move dev dependencies from requirements-dev.txt to setup.py optional dependencies
- Update CI workflows to install with [dev] instead of requirements-dev.txt
- Update CONTRIBUTING.md to reflect new installation method
- Maintain requirements-dev.txt for backward compatibility in setup.py

Co-authored-by: gonzalocasas <[email protected]>
Copy link
Contributor Author

Copilot AI commented Aug 19, 2025

@copilot please update to install requirements-dev.txt as [dev] optional dependencies

Updated dependency management to use [dev] optional dependencies instead of requirements-dev.txt. Development dependencies can now be installed with pip install -e .[dev]. Updated all CI workflows and documentation accordingly. See commit 7e0b7b6.

Copilot AI changed the title Add support for MQTT-PAHO 2.0 versioned callbacks with client ID generation Add support for MQTT-PAHO 2.0 versioned callbacks with client ID generation and modern dependency management Aug 19, 2025
@gonzalocasas
Copy link
Member

@copilot I think you should remove the calls to python -m pip install -e .[dev] in CI, because those are handled by the actions.build

Copy link
Contributor Author

Copilot AI commented Aug 19, 2025

@copilot I think you should remove the calls to python -m pip install -e .[dev] in CI, because those are handled by the actions.build

Removed the redundant python -m pip install -e .[dev] calls from both build.yml and release.yml workflows since they are handled by compas-actions.build@v4. See commit 13a2fa5.

@gonzalocasas
Copy link
Member

@copilot remove 3.9 from the python matrix

Copy link
Contributor Author

Copilot AI commented Aug 19, 2025

@copilot remove 3.9 from the python matrix

Removed Python 3.9 from the CI workflow matrix in both build.yml and release.yml. The workflows now test only Python 3.10 and 3.11. See commit 7865223.

@gonzalocasas
Copy link
Member

@copilot fix linting issues (e.g. src/compas_eve/mqtt/mqtt_paho.py:35:1: W293 blank line contains whitespace
src/compas_eve/mqtt/mqtt_paho.py:39:1: W293 blank line contains whitespace)

Copy link
Contributor Author

Copilot AI commented Aug 19, 2025

@copilot fix linting issues (e.g. src/compas_eve/mqtt/mqtt_paho.py:35:1: W293 blank line contains whitespace
src/compas_eve/mqtt/mqtt_paho.py:39:1: W293 blank line contains whitespace)

Fixed the linting issues by removing whitespace from blank lines 35 and 39 in mqtt_paho.py. The file now passes flake8 checks. See commit aa996fb.

@gonzalocasas gonzalocasas marked this pull request as ready for review August 20, 2025 15:01
Copy link
Member

@gonzalocasas gonzalocasas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Member

@chenkasirer chenkasirer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@gonzalocasas gonzalocasas merged commit c8d09c4 into main Aug 20, 2025
10 checks passed
@gonzalocasas gonzalocasas deleted the copilot/fix-8 branch August 20, 2025 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for MQTT-PAHO 2.0 versioned callbacks

3 participants