@@ -231,12 +231,13 @@ def ws_start(self) -> None:
231231
232232 def _start_listening (self ):
233233 """Start the websocket listener."""
234- try :
235- _LOGGER .debug ("Attempting to find running loop..." )
236- self ._loop = asyncio .get_running_loop ()
237- except RuntimeError :
238- self ._loop = asyncio .get_event_loop ()
239- _LOGGER .debug ("Using new event loop..." )
234+ if not self ._loop :
235+ try :
236+ _LOGGER .debug ("Attempting to find running loop..." )
237+ self ._loop = asyncio .get_running_loop ()
238+ except RuntimeError :
239+ self ._loop = asyncio .get_event_loop ()
240+ _LOGGER .debug ("Using new event loop..." )
240241
241242 if not self ._ws_listening :
242243 _LOGGER .debug ("Setting up websocket ping..." )
@@ -260,6 +261,7 @@ async def _update_status(self, msgtype, data, error):
260261 "Websocket to %s disconnected, retrying" ,
261262 self .websocket .uri ,
262263 )
264+ _LOGGER .debug ("Disconnect message: %s" , error )
263265 self ._ws_listening = False
264266 self .ws_start ()
265267 # Stopped websockets without errors are expected during shutdown
@@ -291,9 +293,9 @@ async def _update_status(self, msgtype, data, error):
291293
292294 async def ws_disconnect (self ) -> None :
293295 """Disconnect the websocket listener."""
296+ self ._ws_listening = False
294297 assert self .websocket
295298 await self .websocket .close ()
296- self ._ws_listening = False
297299
298300 def is_coroutine_function (self , callback ):
299301 """Check if a callback is a coroutine function."""
0 commit comments