Skip to content

Commit

Permalink
Merge branch 'async' into PyISY_beta
Browse files Browse the repository at this point in the history
  • Loading branch information
shbatm committed May 23, 2020
2 parents ce3ab8f + c88f88a commit f9a75e7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 7 additions & 1 deletion pyisy/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ def __init__(
self._lasthb = None
self._sid = None
self._program_key = None
self.websocket_task = None

if websession is None:
websession = get_new_client_session(use_https, tls_ver)
Expand All @@ -358,12 +359,15 @@ def start(self):
def stop(self):
"""Close websocket connection."""
self.status = ES_STOP_UPDATES
self.websocket_task.cancel()
if self.websocket_task is not None and not self.websocket_task.done():
self.websocket_task.cancel()

async def reconnect(self, delay=RECONNECT_DELAY, retries=0):
"""Reconnect to a disconnected websocket."""
if self.status == ES_CONNECTED:
return
if self.websocket_task is not None and not self.websocket_task.done():
self.websocket_task.cancel()
self.status = ES_RECONNECTING
_LOGGER.info("PyISY attempting stream reconnect in %ss.", delay)
await asyncio.sleep(delay)
Expand Down Expand Up @@ -490,6 +494,8 @@ async def websocket(self, retries=0):
_LOGGER.error("Websocket Client Connector Error.")
self.status = ES_LOST_STREAM_CONNECTION
await self.reconnect(RECONNECT_DELAY)
except asyncio.CancelledError:
self.status = ES_DISCONNECTED
except Exception as err:
if self.status != ES_STOP_UPDATES:
_LOGGER.exception("Unexpected error %s", err)
Expand Down
6 changes: 4 additions & 2 deletions pyisy/nodes/group.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Representation of groups (scenes) from an ISY."""
import asyncio

from ..constants import ISY_VALUE_UNKNOWN, PROTO_GROUP
from ..helpers import now
from .nodebase import NodeBase
Expand Down Expand Up @@ -46,7 +48,7 @@ def __init__(
]

# get and update the status
self.isy.loop.create_task(self.update())
asyncio.create_task(self.update())

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

def update_callback(self, event=None):
"""Handle synchronous callbacks for subscriber events."""
self.isy.loop.create_task(self.update(event))
asyncio.create_task(self.update(event))

0 comments on commit f9a75e7

Please sign in to comment.