Example #1
0
    def _are_valid_credentials(self, username, password):
        """Checks whether credentials are valid"""
        def _on_connect(client, userdata, flags, rc):
            """A callback that matches the MQTT client signature, that triggers when connection was established"""
            userdata.set_return_code(rc)
            client.disconnect()

            client.loop_stop()
            client.should_stop = True

        con_result = ConnectionResult()
        client = Client(userdata=con_result)

        client.username_pw_set(username, password)
        client.on_connect = _on_connect

        start_time = time.time()
        client.should_stop = False
        client.connect_async(self.host, self.port)
        client.loop_start()

        while not client.should_stop and time.time(
        ) - start_time < self.timeout:
            time.sleep(0.001)

        return con_result.did_succeed