Skip to content

Commit c88f88a

Browse files
committed
Prevent duplicate websockets
1 parent bd51807 commit c88f88a

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

pyisy/events.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ def __init__(
337337
self._lasthb = None
338338
self._sid = None
339339
self._program_key = None
340+
self.websocket_task = None
340341

341342
if websession is None:
342343
websession = get_new_client_session(use_https, tls_ver)
@@ -358,12 +359,15 @@ def start(self):
358359
def stop(self):
359360
"""Close websocket connection."""
360361
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()
362364

363365
async def reconnect(self, delay=RECONNECT_DELAY, retries=0):
364366
"""Reconnect to a disconnected websocket."""
365367
if self.status == ES_CONNECTED:
366368
return
369+
if self.websocket_task is not None and not self.websocket_task.done():
370+
self.websocket_task.cancel()
367371
self.status = ES_RECONNECTING
368372
_LOGGER.info("PyISY attempting stream reconnect in %ss.", delay)
369373
await asyncio.sleep(delay)
@@ -490,6 +494,8 @@ async def websocket(self, retries=0):
490494
_LOGGER.error("Websocket Client Connector Error.")
491495
self.status = ES_LOST_STREAM_CONNECTION
492496
await self.reconnect(RECONNECT_DELAY)
497+
except asyncio.CancelledError:
498+
self.status = ES_DISCONNECTED
493499
except Exception as err:
494500
if self.status != ES_STOP_UPDATES:
495501
_LOGGER.exception("Unexpected error %s", err)

pyisy/nodes/group.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""Representation of groups (scenes) from an ISY."""
2+
import asyncio
3+
24
from ..constants import ISY_VALUE_UNKNOWN, PROTO_GROUP
35
from ..helpers import now
46
from .nodebase import NodeBase
@@ -46,7 +48,7 @@ def __init__(
4648
]
4749

4850
# get and update the status
49-
self.isy.loop.create_task(self.update())
51+
asyncio.create_task(self.update())
5052

5153
def __del__(self):
5254
"""Cleanup event handlers before deleting."""
@@ -105,4 +107,4 @@ async def update(self, event=None, wait_time=0, hint=None, xmldoc=None):
105107

106108
def update_callback(self, event=None):
107109
"""Handle synchronous callbacks for subscriber events."""
108-
self.isy.loop.create_task(self.update(event))
110+
asyncio.create_task(self.update(event))

0 commit comments

Comments
 (0)