Skip to content

Commit

Permalink
Migrate to Paho MQTT 2.0 (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
koenvervloesem authored Feb 11, 2024
1 parent 5264ef4 commit 426fa9f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
28 changes: 19 additions & 9 deletions TheengsGateway/ble_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,11 @@ def on_connect(
client, # noqa: ANN001
userdata, # noqa: ANN001,ARG001
flags, # noqa: ANN001,ARG001
return_code, # noqa: ANN001
reason_code, # noqa: ANN001
properties, # noqa: ANN001,ARG001
) -> None:
if return_code == 0:
logger.info("Connected to MQTT Broker!")
if reason_code == 0:
logger.info("Connected to MQTT broker")
client.publish(
self.configuration["lwt_topic"],
"online",
Expand All @@ -139,10 +140,10 @@ def on_connect(
self.subscribe(self.configuration["subscribe_topic"])
else:
logger.error(
"Failed to connect to MQTT broker %s:%d return code: %d",
"Failed to connect to MQTT broker %s:%d reason code: %d",
self.configuration["host"],
self.configuration["port"],
return_code,
reason_code,
)
self.client.connect(
self.configuration["host"],
Expand All @@ -152,14 +153,23 @@ def on_connect(
def on_disconnect(
client, # noqa: ANN001,ARG001
userdata, # noqa: ANN001,ARG001
return_code=0, # noqa: ANN001
flags, # noqa: ANN001,ARG001
reason_code, # noqa: ANN001
properties, # noqa: ANN001,ARG001
) -> None:
logger.error("Disconnected with return code = %d", return_code)
if reason_code == 0:
logger.info("Disconnected from MQTT broker")
else:
logger.error(
"Disconnected from MQTT broker with reason code = %d", reason_code
)

if self.configuration["enable_websocket"]:
self.client = mqtt_client.Client(transport="websockets")
self.client = mqtt_client.Client(
mqtt_client.CallbackAPIVersion.VERSION2, transport="websockets"
)
else:
self.client = mqtt_client.Client()
self.client = mqtt_client.Client(mqtt_client.CallbackAPIVersion.VERSION2)

if self.configuration["enable_tls"]:
self.client.tls_set(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"bluetooth-clocks<1.0",
"bluetooth-numbers>=1.0,<2.0",
"importlib-metadata",
"paho-mqtt>=1.6.1",
"paho-mqtt>=2.0.0",
"pycryptodomex>=3.18.0",
"TheengsDecoder>=1.6.4",
],
Expand Down

0 comments on commit 426fa9f

Please sign in to comment.