I think ones I read that the instant script flow control is asynchronous, and you can not really control the order of execution.
Have you tried to set focus in onStartup event? I would try to put the script in the view or in the component itself.
Regarding the sleep action, I would use instead:
from threading import Timer
def focus():
self.focus()
Timer(0.5, focus).start()