Skip to content

Commit 6c2cf80

Browse files
committed
better loop handling
1 parent 061d28f commit 6c2cf80

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

openevsehttp/__init__.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,13 @@ def __init__(self, host: str, user: str = None, pwd: str = None) -> None:
171171
self._override = None
172172
self._ws_listening = False
173173
self.websocket: Optional[OpenEVSEWebsocket] = None
174-
self._loop = asyncio.get_event_loop()
174+
175+
try:
176+
_LOGGER.debug("Attempting to find running loop...")
177+
self._loop = asyncio.get_running_loop()
178+
except RuntimeError:
179+
self._loop = asyncio.get_event_loop()
180+
_LOGGER.debug("Using new event loop...")
175181

176182
async def send_command(self, command: str) -> tuple | None:
177183
"""Send a RAPI command to the charger and parses the response."""
@@ -231,11 +237,11 @@ async def update(self) -> None:
231237

232238
def _start_listening(self):
233239
"""Start the websocket listener."""
234-
if not self._loop.is_running():
240+
if not self._ws_listening:
235241
self._loop.create_task(self.websocket.listen())
236242
pending = asyncio.all_tasks()
237-
self._loop.run_until_complete(asyncio.gather(*pending))
238243
self._ws_listening = True
244+
self._loop.run_until_complete(asyncio.gather(*pending))
239245

240246
def _update_status(self, msgtype, data, error):
241247
"""Update data from websocket listener."""

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
PROJECT_DIR = Path(__file__).parent.resolve()
77
README_FILE = PROJECT_DIR / "README.md"
8-
VERSION = "0.1.8-14"
8+
VERSION = "0.1.8-15"
99

1010

1111
setup(

0 commit comments

Comments
 (0)