@@ -337,6 +337,7 @@ def __init__(
337
337
self ._lasthb = None
338
338
self ._sid = None
339
339
self ._program_key = None
340
+ self .websocket_task = None
340
341
341
342
if websession is None :
342
343
websession = get_new_client_session (use_https , tls_ver )
@@ -358,12 +359,15 @@ def start(self):
358
359
def stop (self ):
359
360
"""Close websocket connection."""
360
361
self .status = ES_STOP_UPDATES
361
- self .websocket_task .cancel ()
362
+ if self .websocket_task is not None and not self .websocket_task .done ():
363
+ self .websocket_task .cancel ()
362
364
363
365
async def reconnect (self , delay = RECONNECT_DELAY , retries = 0 ):
364
366
"""Reconnect to a disconnected websocket."""
365
367
if self .status == ES_CONNECTED :
366
368
return
369
+ if self .websocket_task is not None and not self .websocket_task .done ():
370
+ self .websocket_task .cancel ()
367
371
self .status = ES_RECONNECTING
368
372
_LOGGER .info ("PyISY attempting stream reconnect in %ss." , delay )
369
373
await asyncio .sleep (delay )
@@ -490,6 +494,8 @@ async def websocket(self, retries=0):
490
494
_LOGGER .error ("Websocket Client Connector Error." )
491
495
self .status = ES_LOST_STREAM_CONNECTION
492
496
await self .reconnect (RECONNECT_DELAY )
497
+ except asyncio .CancelledError :
498
+ self .status = ES_DISCONNECTED
493
499
except Exception as err :
494
500
if self .status != ES_STOP_UPDATES :
495
501
_LOGGER .exception ("Unexpected error %s" , err )
0 commit comments