Skip to content

Commit

Permalink
feat: return better messages from websocket handler (#405)
Browse files Browse the repository at this point in the history
* feat: return better messages from websocket handler

* formatting
  • Loading branch information
firstof9 authored Jan 12, 2025
1 parent 3fa2153 commit aaa4d28
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 10 deletions.
16 changes: 9 additions & 7 deletions openevsehttp/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,13 @@ def ws_start(self) -> None:

def _start_listening(self):
"""Start the websocket listener."""
try:
_LOGGER.debug("Attempting to find running loop...")
self._loop = asyncio.get_running_loop()
except RuntimeError:
self._loop = asyncio.get_event_loop()
_LOGGER.debug("Using new event loop...")
if not self._loop:
try:
_LOGGER.debug("Attempting to find running loop...")
self._loop = asyncio.get_running_loop()
except RuntimeError:
self._loop = asyncio.get_event_loop()
_LOGGER.debug("Using new event loop...")

if not self._ws_listening:
_LOGGER.debug("Setting up websocket ping...")
Expand All @@ -260,6 +261,7 @@ async def _update_status(self, msgtype, data, error):
"Websocket to %s disconnected, retrying",
self.websocket.uri,
)
_LOGGER.debug("Disconnect message: %s", error)
self._ws_listening = False
self.ws_start()
# Stopped websockets without errors are expected during shutdown
Expand Down Expand Up @@ -291,9 +293,9 @@ async def _update_status(self, msgtype, data, error):

async def ws_disconnect(self) -> None:
"""Disconnect the websocket listener."""
self._ws_listening = False
assert self.websocket
await self.websocket.close()
self._ws_listening = False

def is_coroutine_function(self, callback):
"""Check if a callback is a coroutine function."""
Expand Down
4 changes: 2 additions & 2 deletions openevsehttp/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ async def running(self):
self._error_reason = ERROR_AUTH_FAILURE
else:
_LOGGER.error("Unexpected response received: %s", error)
self._error_reason = ERROR_UNKNOWN
self._error_reason = error
await OpenEVSEWebsocket.state.fset(self, STATE_STOPPED)
except (aiohttp.ClientConnectionError, asyncio.TimeoutError) as error:
if self.failed_attempts >= MAX_FAILED_ATTEMPTS:
Expand All @@ -126,7 +126,7 @@ async def running(self):
except Exception as error: # pylint: disable=broad-except
if self.state != STATE_STOPPED:
_LOGGER.exception("Unexpected exception occurred: %s", error)
self._error_reason = ERROR_UNKNOWN
self._error_reason = error
await OpenEVSEWebsocket.state.fset(self, STATE_STOPPED)
else:
if self.state != STATE_STOPPED:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

PROJECT_DIR = Path(__file__).parent.resolve()
README_FILE = PROJECT_DIR / "README.md"
VERSION = "0.1.69"
VERSION = "0.1.70"

setup(
name="python_openevse_http",
Expand Down
Loading

0 comments on commit aaa4d28

Please sign in to comment.