Skip to content

Commit 2aab6f0

Browse files
authored
Merge pull request #5 from microBeesTech/microBeesPy
fix mqtt class
2 parents 0da946c + 825802f commit 2aab6f0

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Used as a wrapper dependency in the Home Assistant integration.
66
Version
77
-------
88

9-
0.3.4
9+
0.3.5
1010

1111
Installation
1212
------------

microBeesPy/mqtt.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import logging
22
import paho.mqtt.client as mqtt
3+
import asyncio
34

45
_LOGGER = logging.getLogger(__name__)
56

67
class MicrobeesMqtt:
78
"""Class to handle MQTT communication with the microBees platform."""
8-
9+
910
def __init__(self, broker, port, username, password, client_id, on_message_callback=None):
1011
self.broker = broker
1112
self.port = port
@@ -21,7 +22,7 @@ def __init__(self, broker, port, username, password, client_id, on_message_callb
2122
# Use the new API for setting callbacks
2223
self.client.on_connect = self._on_connect
2324
self.client.on_disconnect = self._on_disconnect
24-
self.client.on_message = self._on_message if on_message_callback else None
25+
self.client.on_message = self._on_message
2526

2627
def _on_connect(self, client, userdata, flags, rc):
2728
"""Handle connection to the broker."""
@@ -30,15 +31,18 @@ def _on_connect(self, client, userdata, flags, rc):
3031

3132
def _on_disconnect(self, client, userdata, rc):
3233
"""Handle disconnection from the broker."""
34+
if rc != 0:
35+
_LOGGER.warning(f"Disconnected from MQTT broker with error code {rc}")
3336

3437
def _on_message(self, client, userdata, message):
35-
"""Handle incoming messages."""
36-
if self.on_message_callback:
37-
self.on_message_callback(message)
38+
loop = asyncio.new_event_loop()
39+
asyncio.set_event_loop(loop)
40+
loop.run_until_complete(self.on_message_callback(message))
3841

3942
def connect(self):
4043
"""Connect to the MQTT broker."""
4144
self.client.connect(self.broker, self.port)
45+
self.client.loop_start()
4246

4347
def disconnect(self):
4448
"""Disconnect from the MQTT broker."""
@@ -52,6 +56,6 @@ def publish(self, topic, payload, qos=0):
5256
"""Publish a message to a topic."""
5357
self.client.publish(topic, payload, qos)
5458

55-
def loop_forever(self):
56-
"""Start the network loop."""
57-
self.client.loop_forever()
59+
async def async_loop(self):
60+
"""Run the MQTT loop in an asyncio-friendly way."""
61+
self.client.loop_forever()

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
setup(
1010
name='microBeesPy',
1111
packages=find_packages(),
12-
version='0.3.4',
12+
version='0.3.5',
1313
long_description=readme,
1414
keywords='microbees',
1515
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)