Skip to content

Commit ba08654

Browse files
authored
fix: better websocket reconnection (#470)
1 parent 2a27f3b commit ba08654

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

openevsehttp/__main__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,11 @@ async def test_and_get(self) -> dict:
231231

232232
def ws_start(self) -> None:
233233
"""Start the websocket listener."""
234-
if self._ws_listening:
235-
raise AlreadyListening
234+
if self.websocket:
235+
if self._ws_listening and self.websocket.state == "connected":
236+
raise AlreadyListening
237+
if self._ws_listening and self.websocket.state != "connected":
238+
self._ws_listening = False
236239
self._start_listening()
237240

238241
def _start_listening(self):

openevsehttp/websocket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __init__(
3838
self._user = user
3939
self._password = password
4040
self.callback = callback
41-
self._state = None
41+
self._state = STATE_DISCONNECTED
4242
self.failed_attempts = 0
4343
self._error_reason = None
4444
self._client = None

tests/test_main.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
UnsupportedFeature,
2020
)
2121
from tests.common import load_fixture
22-
from openevsehttp.websocket import SIGNAL_CONNECTION_STATE, STATE_CONNECTED
22+
from openevsehttp.websocket import (
23+
SIGNAL_CONNECTION_STATE,
24+
STATE_CONNECTED,
25+
STATE_DISCONNECTED,
26+
)
2327

2428
pytestmark = pytest.mark.asyncio
2529

@@ -53,7 +57,7 @@ async def test_ws_state(test_charger):
5357
"""Test v4 Status reply."""
5458
await test_charger.update()
5559
value = test_charger.ws_state
56-
assert value == None
60+
assert value == STATE_DISCONNECTED
5761
await test_charger.ws_disconnect()
5862

5963

0 commit comments

Comments
 (0)