async def test_connect_disconnect(): conf = fresh_config() conf["clean"] = False mqc = MQTTClient(conf) mqc._MQTTProto = FakeProto assert mqc._state == 0 assert mqc is not None await mqc.connect() assert mqc._proto is not None assert mqc._state == 1 await asyncio.sleep_ms(5*RTT) # let new tasks settle await finish_test(mqc, conns=1)
async def connect_subscribe(topic, qos, clean=True, fake=True): conf = fresh_config() conf["debug"] = 3 conf["clean"] = clean mqc = MQTTClient(conf) if fake: mqc._MQTTProto = FakeProto reset_cb() # await mqc.connect() assert wifi_status == True await asyncio.sleep_ms(10) # give created tasks a chance to run assert conn_started == True await mqc.subscribe(topic, qos) return (mqc, conf)
def start(cls, config): from mqtt_async import MQTTClient config["subs_cb"] = cls._msg_handler config["wifi_coro"] = cls._mqtt_handler config["connect_coro"] = cls._init_handler fail_led(True) cls.client = MQTTClient(config) cls.client.start()
def test_init_errors(): conf = fresh_config() conf["will"] = "bye" with pytest.raises(ValueError, match=r'.*MQTTMessage.*'): mqc = MQTTClient(conf) # conf = fresh_config() conf["will"] = MQTTMessage("foo", "bar") conf["keepalive"] = 1000000 with pytest.raises(ValueError, match='invalid keepalive'): mqc = MQTTClient(conf) # conf = fresh_config() conf["will"] = MQTTMessage("foo", "bar") conf["response_time"] = 10 conf["keepalive"] = 10 with pytest.raises(ValueError, match=r'keepalive.*time'): mqc = MQTTClient(conf) # conf = config.copy() with pytest.raises(ValueError, match='no server'): mqc = MQTTClient(conf) # conf = fresh_config() conf["port"] = 0 mqc = MQTTClient(conf) assert mqc._c["port"] == 1883 # conf = fresh_config() conf["port"] = 0 conf["ssl_params"] = 1 mqc = MQTTClient(conf) assert mqc._c["port"] == 8883
TOPIC = 'test/mqtt_async' config.server = '192.168.0.14' # can also be a hostname config.ssid = 'wifi-ssid' config.wifi_pw = 'wifi-password' def callback(topic, msg, retained, qos): print(topic, msg, retained, qos) async def conn_callback(client): await client.subscribe(TOPIC, 1) async def main(client): await client.connect() n = 0 while True: print('publish', n) await client.publish(TOPIC, 'Hello World #{}!'.format(n), qos=1) await asyncio.sleep(5) n += 1 config.subs_cb = callback config.connect_coro = conn_callback client = MQTTClient(config) loop = asyncio.get_event_loop() loop.run_until_complete(main(client))
def test_dns_lookup(): conf = fresh_config() mqc = MQTTClient(conf) mqc._MQTTProto = FakeProto mqc._dns_lookup() assert mqc._addr == broker
def test_instantiate(): conf = fresh_config() mqc = MQTTClient(conf) mqc._MQTTProto = FakeProto assert mqc is not None