From 48663d2e0d0d31ef0ebd7053aec086e4aee67a25 Mon Sep 17 00:00:00 2001 From: bazza2000 Date: Fri, 15 May 2026 08:09:26 +0100 Subject: [PATCH] Fix AttributeError for paho-mqtt < 2.0 compatibility (#321) When paho-mqtt < 2.0 is installed (e.g. in cached Docker layers), mqtt.CallbackAPIVersion does not exist, causing an immediate crash on startup with: AttributeError: module 'paho.mqtt.client' has no attribute 'CallbackAPIVersion' Add try/except fallback so the addon works with both paho-mqtt 1.x and 2.x, eliminating the crash and allowing the addon to start correctly. Co-authored-by: Barry Jarman Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/CHANGELOG.md | 6 ++++++ src/mqtt.py | 10 +++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md index 87acbc7..f58ae94 100644 --- a/src/CHANGELOG.md +++ b/src/CHANGELOG.md @@ -1,3 +1,9 @@ +## v1.13.4 + +### 🐛 Bug Fixes: + +- Fix `AttributeError: module 'paho.mqtt.client' has no attribute 'CallbackAPIVersion'` when paho-mqtt < 2.0 is installed + ## v1.13.3 ### 🐛 Bug Fixes: diff --git a/src/mqtt.py b/src/mqtt.py index 1b7d8b3..4883b2b 100644 --- a/src/mqtt.py +++ b/src/mqtt.py @@ -25,9 +25,13 @@ active_schedules = {} otp_code = None def connect(): - client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "volvoAAOS2mqtt") \ - if os.environ.get("IS_HA_ADDON") \ - else mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "volvoAAOS2mqtt_" + settings.volvoData["username"].replace("+", "")) + client_id = "volvoAAOS2mqtt" if os.environ.get("IS_HA_ADDON") \ + else "volvoAAOS2mqtt_" + settings.volvoData["username"].replace("+", "") + try: + client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, client_id) + except AttributeError: + # paho-mqtt < 2.0 does not have CallbackAPIVersion + client = mqtt.Client(client_id) if "logging" in settings["mqtt"] and settings["mqtt"]["logging"]: mqtt_logger = logging.getLogger("mqtt")