From 8be3ef15078a971c4026b1de8e8fd45248a23fb2 Mon Sep 17 00:00:00 2001
From: Ben Winslow <github@t.themuffin.net>
Date: Thu, 2 May 2024 14:24:58 -0400
Subject: [PATCH] Add HA script compat for paho-mqtt 2.0.0 via legacy callback
 API (#2916)

---
 examples/mqtt_rtl_433_test_client.py | 5 ++++-
 examples/rtl_433_mqtt_hass.py        | 5 ++++-
 examples/rtl_433_mqtt_relay.py       | 5 ++++-
 3 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/examples/mqtt_rtl_433_test_client.py b/examples/mqtt_rtl_433_test_client.py
index 58230fc6..5c50feaa 100755
--- a/examples/mqtt_rtl_433_test_client.py
+++ b/examples/mqtt_rtl_433_test_client.py
@@ -30,7 +30,10 @@ TIMEOUT_STALE_SENSOR = 600  # Seconds before showing a timeout indicator
 
 # log = logging.getLogger()  # Single process logger
 log = mp.log_to_stderr()  # Multiprocessing capable logger
-mqtt_client = mqtt.Client("RTL_433_Test")
+if hasattr(mqtt, 'CallbackAPIVersion'):  # paho >= 2.0.0
+    mqtt_client = mqtt.Client(callback_api_version=mqtt.CallbackAPIVersion.VERSION1, client_id="RTL_433_Test")
+else:
+    mqtt_client = mqtt.Client(client_id="RTL_433_Test")
 
 sensor_state = dict()  # Dictionary containing accumulated sensor state
 
diff --git a/examples/rtl_433_mqtt_hass.py b/examples/rtl_433_mqtt_hass.py
index f6f251ee..908b08ab 100755
--- a/examples/rtl_433_mqtt_hass.py
+++ b/examples/rtl_433_mqtt_hass.py
@@ -984,7 +984,10 @@ def bridge_event_to_hass(mqttc, topic_prefix, data):
 def rtl_433_bridge():
     """Run a MQTT Home Assistant auto discovery bridge for rtl_433."""
 
-    mqttc = mqtt.Client()
+    if hasattr(mqtt, 'CallbackAPIVersion'):  # paho >= 2.0.0
+        mqttc = mqtt.Client(callback_api_version=mqtt.CallbackAPIVersion.VERSION1)
+    else:
+        mqttc = mqtt.Client()
 
     if args.debug:
         mqttc.enable_logger()
diff --git a/examples/rtl_433_mqtt_relay.py b/examples/rtl_433_mqtt_relay.py
index 7ad0571e..0327fee3 100755
--- a/examples/rtl_433_mqtt_relay.py
+++ b/examples/rtl_433_mqtt_relay.py
@@ -111,7 +111,10 @@ def rtl_433_probe():
     """Run a rtl_433 UDP listener."""
 
     ## Connect to MQTT
-    mqttc = mqtt.Client()
+    if hasattr(mqtt, 'CallbackAPIVersion'):  # paho >= 2.0.0
+        mqttc = mqtt.Client(callback_api_version=mqtt.CallbackAPIVersion.VERSION1)
+    else:
+        mqttc = mqtt.Client()
     mqttc.on_connect = mqtt_connect
     mqttc.on_disconnect = mqtt_disconnect
     if MQTT_USERNAME != None: