diff --git a/examples/mqtt-all.py b/examples/mqtt-all.py index b4fd19ef..e519c73b 100755 --- a/examples/mqtt-all.py +++ b/examples/mqtt-all.py @@ -46,14 +46,14 @@ # mqtt callbacks -def on_connect(client, userdata, flags, rc): - if rc == 0: +def on_connect(client, userdata, flags, reason_code, properties): + if reason_code == 0: print("connected OK") else: - print("Bad connection Returned code=", rc) + print("Bad connection Returned code=", reason_code) -def on_publish(client, userdata, mid): +def on_publish(client, userdata, mid, reason_code, properties): print("mid: " + str(mid)) @@ -211,7 +211,7 @@ def main(): """ ) - mqtt_client = mqtt.Client(client_id=device_id) + mqtt_client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id=device_id) if args.username and args.password: mqtt_client.username_pw_set(args.username, args.password) mqtt_client.on_connect = on_connect diff --git a/requirements-examples.txt b/requirements-examples.txt index 96def717..b982a3d0 100644 --- a/requirements-examples.txt +++ b/requirements-examples.txt @@ -3,5 +3,5 @@ font-roboto astral pytz sounddevice -paho-mqtt +paho-mqtt>=2.0 pillow diff --git a/tests/test_mqtt_example.py b/tests/test_mqtt_example.py new file mode 100644 index 00000000..5361b694 --- /dev/null +++ b/tests/test_mqtt_example.py @@ -0,0 +1,112 @@ +"""Check example MQTT callbacks using a real Paho client and local peer.""" +import importlib.util +import json +import socket +import sys +import threading +import warnings +from pathlib import Path +from unittest.mock import Mock + +import paho.mqtt.client as mqtt +import pytest + + +def test_mqtt_example(monkeypatch, capsys): + for name in ("st7735", "bme280", "pms5003", "enviroplus", "ltr559", + "fonts", "fonts.ttf", "PIL", "PIL.Image", "PIL.ImageDraw", + "PIL.ImageFont", "smbus2"): + monkeypatch.setitem(sys.modules, name, Mock()) + spec = importlib.util.spec_from_file_location("mqtt_all", Path(__file__).parents[1] / "examples/mqtt-all.py") + example = importlib.util.module_from_spec(spec) + spec.loader.exec_module(example) + connected = threading.Event() + received = threading.Event() + clients = [] + errors = [] + packets = [] + real_client = mqtt.Client + + def client(*args, **kwargs): + instance = real_client(*args, **kwargs) + clients.append(instance) + return instance + + def packet(conn): + header = conn.recv(1) + length = 0 + shift = 0 + while True: + digit = conn.recv(1)[0] + length |= (digit & 127) << shift + if digit < 128: + break + shift += 7 + body = b"" + while len(body) < length: + chunk = conn.recv(length - len(body)) + if not chunk: + raise EOFError("short MQTT packet") + body += chunk + return header[0], body + + with socket.socket() as listener: + listener.bind(("127.0.0.1", 0)) + listener.listen(1) + listener.settimeout(5) + + def peer(): + try: + with listener.accept()[0] as conn: + conn.settimeout(5) + assert packet(conn)[0] == 0x10 + conn.sendall(b"\x20\x02\x00\x00") + connected.set() + packets.append(packet(conn)) + received.set() + except (OSError, EOFError, IndexError, AssertionError) as error: + errors.append(error) + connected.set() + received.set() + + thread = threading.Thread(target=peer, daemon=True) + thread.start() + monkeypatch.setattr(mqtt, "Client", client) + monkeypatch.setattr(sys, "argv", ["mqtt-all.py", "--broker", "127.0.0.1", "--port", str(listener.getsockname()[1]), "--interval", "0"]) + monkeypatch.setattr(example, "get_serial_number", lambda: "test-pi") + monkeypatch.setattr(example, "check_wifi", lambda: True) + monkeypatch.setattr(example, "display_status", lambda *_: None) + example.SerialTimeoutError = TimeoutError + example.PMS5003.side_effect = TimeoutError + count = 0 + + def readings(_): + nonlocal count + count += 1 + if count > 1: + assert received.wait(5) + raise KeyboardInterrupt + assert connected.wait(5) + return {"temperature": 21} + + monkeypatch.setattr(example, "read_bme280", readings) + try: + with warnings.catch_warnings(): + warnings.simplefilter("error", DeprecationWarning) + with pytest.raises(KeyboardInterrupt): + example.main() + finally: + for instance in clients: + instance.disconnect() + instance.loop_stop() + thread.join(6) + assert not errors + assert not thread.is_alive() + assert packets[0][0] == 0x31 # retained QoS 0 publish + body = packets[0][1] + topic_length = int.from_bytes(body[:2], "big") + assert body[2:2 + topic_length] == b"enviroplus" + assert json.loads(body[2 + topic_length:]) == {"temperature": 21, "serial": "test-pi"} + output = capsys.readouterr().out + assert "connected OK" in output + assert "mid: 1" in output diff --git a/tox.ini b/tox.ini index 2b6d87b8..bf38c1d7 100644 --- a/tox.ini +++ b/tox.ini @@ -12,6 +12,7 @@ deps = mock pytest>=3.1 pytest-cov + paho-mqtt>=2.0 build [testenv:qa]