Fix paho-mqtt v2.x compatibility

Handle both paho-mqtt v1.x and v2.x in MQTTPublisher:
- Try v2.x format with callback_api_version first
- Fall back to v1.x format if needed
- Ensures compatibility across different paho-mqtt versions

Fixes: ValueError when using paho-mqtt 2.0+
This commit is contained in:
2025-12-27 13:52:13 +01:00
parent f54c0a0f35
commit b2f9bff765
2 changed files with 20 additions and 7 deletions

View File

@@ -34,7 +34,7 @@ source .venv/bin/activate
### 4. Install Python Dependencies ### 4. Install Python Dependencies
```bash ```bash
pip install --upgrade pip pip install --upgrade pip
pip install bluepy paho-mqtt pip install pybluez bluepy paho-mqtt
# Or install the package in development mode # Or install the package in development mode
pip install -e . pip install -e .
@@ -43,8 +43,12 @@ pip install -e .
### 5. Set Bluetooth Capabilities ### 5. Set Bluetooth Capabilities
This allows Python to access Bluetooth without sudo: This allows Python to access Bluetooth without sudo:
```bash ```bash
# Set capabilities on the Python interpreter in the venv # Set capabilities on the actual Python binary (not the symlink)
sudo setcap 'cap_net_raw,cap_net_admin+eip' .venv/bin/python3 sudo setcap 'cap_net_raw,cap_net_admin+eip' $(readlink -f .venv/bin/python3)
# Verify it was set correctly
getcap $(readlink -f .venv/bin/python3)
# Should show: cap_net_raw,cap_net_admin+eip
``` ```
### 6. Configure the Application ### 6. Configure the Application
@@ -88,13 +92,13 @@ Press Ctrl+C to stop.
If you get permission errors: If you get permission errors:
```bash ```bash
# Check if capabilities are set # Check if capabilities are set
getcap .venv/bin/python3 getcap $(readlink -f .venv/bin/python3)
# If not set, run: # If not set, run:
sudo setcap 'cap_net_raw,cap_net_admin+eip' .venv/bin/python3 sudo setcap 'cap_net_raw,cap_net_admin+eip' $(readlink -f .venv/bin/python3)
# Verify Bluetooth device is up # Verify Bluetooth device is up
hciconfig hci0 up sudo hciconfig hci0 up
``` ```
### MQTT Connection Issues ### MQTT Connection Issues

View File

@@ -23,6 +23,15 @@ class MQTTPublisher:
def _setup_client(self): def _setup_client(self):
"""Setup MQTT client with callbacks.""" """Setup MQTT client with callbacks."""
# Handle both paho-mqtt v1.x and v2.x
try:
# Try v2.x format (with callback_api_version)
self.client = mqtt.Client(
callback_api_version=mqtt.CallbackAPIVersion.VERSION1,
client_id=config.MQTT_CLIENT_ID
)
except (TypeError, AttributeError):
# Fall back to v1.x format
self.client = mqtt.Client(config.MQTT_CLIENT_ID) self.client = mqtt.Client(config.MQTT_CLIENT_ID)
# Set credentials if provided # Set credentials if provided