| 183 | |
| 184 | class _Subscriber: |
| 185 | def __init__(self): |
| 186 | try: |
| 187 | self.client = mqtt.Client( |
| 188 | client_id=f"ss-test-{time.time_ns()}", |
| 189 | callback_api_version=mqtt.CallbackAPIVersion.VERSION2, |
| 190 | ) |
| 191 | except (AttributeError, TypeError): |
| 192 | # Older paho-mqtt without callback_api_version arg |
| 193 | self.client = mqtt.Client(client_id=f"ss-test-{time.time_ns()}") |
| 194 | |
| 195 | def _on_message(_client, _userdata, msg): |
| 196 | received.append({"topic": msg.topic, "payload": bytes(msg.payload)}) |
| 197 | |
| 198 | self.client.on_message = _on_message |
| 199 | self.client.connect(mosquitto_broker["host"], mosquitto_broker["port"], 30) |
| 200 | self.client.loop_start() |
| 201 | self.messages = received |
| 202 | |
| 203 | def subscribe(self, topic: str, qos: int = 0) -> None: |
| 204 | self.client.subscribe(topic, qos) |