#NEWLINE# def discover( self , timeout_secs = 0.3 , num_repeats = 3 ) : #NEWLINE# #INDENT# self . initialize_socket ( timeout_secs ) #NEWLINE# msg = GetService ( BROADCAST_MAC , self . source_id , seq_num = 0 , payload = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } , ack_requested = False , response_requested = True ) #NEWLINE# responses = [ ] #NEWLINE# addr_seen = [ ] #NEWLINE# num_devices_seen = 0 #NEWLINE# attempts = 0 #NEWLINE# while ( attempts < num_repeats ) : #NEWLINE# #INDENT# sent = False #NEWLINE# start_time = time ( ) #NEWLINE# timedout = False #NEWLINE# while ( not timedout ) : #NEWLINE# #INDENT# if ( not sent ) : #NEWLINE# #INDENT# self . sock . sendto ( msg . packed_message , ( UDP_BROADCAST_IP , UDP_BROADCAST_PORT ) ) #NEWLINE# sent = True #NEWLINE# if self . verbose : #NEWLINE# #INDENT# print ( ( 'SEND: ' + str ( msg ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# try : #NEWLINE# #INDENT# ( data , ( ip_addr , port ) ) = self . sock . recvfrom ( 1024 ) #NEWLINE# response = unpack_lifx_message ( data ) #NEWLINE# response . ip_addr = ip_addr #NEWLINE# if self . verbose : #NEWLINE# #INDENT# print ( ( 'RECV: ' + str ( response ) ) ) #NEWLINE# #UNINDENT# if ( ( type ( response ) == StateService ) and ( response . origin == 1 ) and ( response . source_id == self . source_id ) ) : #NEWLINE# #INDENT# if ( ( response . target_addr not in addr_seen ) and ( response . target_addr != BROADCAST_MAC ) ) : #NEWLINE# #INDENT# addr_seen . append ( response . target_addr ) #NEWLINE# num_devices_seen += 1 #NEWLINE# responses . append ( response ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# except timeout : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# elapsed_time = ( time ( ) - start_time ) #NEWLINE# timedout = ( True if ( elapsed_time > timeout_secs ) else False ) #NEWLINE# #UNINDENT# attempts += 1 #NEWLINE# #UNINDENT# self . close_socket ( ) #NEWLINE# return responses