@@ -212,14 +212,6 @@ def __init__(self, host, port=None, cast_type=CAST_TYPE_CHROMECAST,
212212
213213 self .receiver_controller .register_status_listener (self )
214214
215- try :
216- self .initialize_connection ()
217- except ChromecastConnectionError :
218- self ._report_connection_status (
219- ConnectionStatus (CONNECTION_STATUS_DISCONNECTED ,
220- NetworkAddress (self .host , self .port )))
221- raise
222-
223215 def initialize_connection (self ): # noqa: E501 pylint:disable=too-many-statements, too-many-branches
224216 """Initialize a socket to a Chromecast, retrying as necessary."""
225217 tries = self .tries
@@ -361,6 +353,19 @@ def mdns_backoff(service, retry):
361353 self .fn or self .host , self .port )
362354 raise ChromecastConnectionError ("Failed to connect" )
363355
356+ def connect (self ):
357+ """ Connect socket connection to Chromecast device.
358+
359+ Must only be called if the worker thread will not be started.
360+ """
361+ try :
362+ self .initialize_connection ()
363+ except ChromecastConnectionError :
364+ self ._report_connection_status (
365+ ConnectionStatus (CONNECTION_STATUS_DISCONNECTED ,
366+ NetworkAddress (self .host , self .port )))
367+ return
368+
364369 def disconnect (self ):
365370 """ Disconnect socket connection to Chromecast device """
366371 self .stop .set ()
@@ -413,7 +418,15 @@ def is_stopped(self):
413418 return self .stop .is_set ()
414419
415420 def run (self ):
416- """ Start polling the socket. """
421+ """ Connect to the cast and start polling the socket. """
422+ try :
423+ self .initialize_connection ()
424+ except ChromecastConnectionError :
425+ self ._report_connection_status (
426+ ConnectionStatus (CONNECTION_STATUS_DISCONNECTED ,
427+ NetworkAddress (self .host , self .port )))
428+ return
429+
417430 self .heartbeat_controller .reset ()
418431 self ._force_recon = False
419432 logging .debug ("Thread started..." )
@@ -698,8 +711,8 @@ def send_message(self, destination_id, namespace, data,
698711 self .logger .info ('[%s:%s] Error writing to socket.' ,
699712 self .fn or self .host , self .port )
700713 else :
701- raise NotConnected ("Chromecast " + self .host + ":" + self . port +
702- " is connecting..." )
714+ raise NotConnected ("Chromecast " + self .host + ":" +
715+ str ( self . port ) + " is connecting..." )
703716
704717 def send_platform_message (self , namespace , message , inc_session_id = False ,
705718 callback_function_param = False ):
0 commit comments