Skip to content

Commit 2c08d0f

Browse files
authored
fix: throw exceptions if url unreachable (#416)
* fix: throw exceptions if url unreachable * formatting
1 parent c7f638b commit 2c08d0f

File tree

4 files changed

+37
-12
lines changed

4 files changed

+37
-12
lines changed

openevsehttp/__main__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,12 @@ async def process_request(
153153
await self.update()
154154
return message
155155

156-
except (TimeoutError, ServerTimeoutError):
156+
except (TimeoutError, ServerTimeoutError) as err:
157157
_LOGGER.error("%s: %s", ERROR_TIMEOUT, url)
158-
message = {"msg": ERROR_TIMEOUT}
158+
raise err
159159
except ContentTypeError as err:
160-
_LOGGER.error("%s", err)
161-
message = {"msg": err}
160+
_LOGGER.error("Content error: %s", err.message)
161+
raise err
162162

163163
await session.close()
164164
return message
@@ -263,7 +263,7 @@ async def _update_status(self, msgtype, data, error):
263263
)
264264
_LOGGER.debug("Disconnect message: %s", error)
265265
self._ws_listening = False
266-
self.ws_start()
266+
267267
# Stopped websockets without errors are expected during shutdown
268268
# and ignored
269269
elif data == STATE_STOPPED and error:
@@ -273,7 +273,6 @@ async def _update_status(self, msgtype, data, error):
273273
error,
274274
)
275275
self._ws_listening = False
276-
await self.ws_disconnect()
277276

278277
elif msgtype == "data":
279278
_LOGGER.debug("Websocket data: %s", data)
@@ -428,6 +427,7 @@ async def toggle_override(self) -> None:
428427
# 3.x: use RAPI commands $FE (enable) and $FS (sleep)
429428
# 4.x: use HTTP API call
430429
lower = "4.0.0"
430+
msg = ""
431431
if self._version_check(lower):
432432
url = f"{self.url}override"
433433

openevsehttp/websocket.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ async def running(self):
110110
self._error_reason = error
111111
await OpenEVSEWebsocket.state.fset(self, STATE_STOPPED)
112112
except (aiohttp.ClientConnectionError, asyncio.TimeoutError) as error:
113-
if self.failed_attempts >= MAX_FAILED_ATTEMPTS:
113+
if self.failed_attempts > MAX_FAILED_ATTEMPTS:
114114
self._error_reason = ERROR_TOO_MANY_RETRIES
115115
await OpenEVSEWebsocket.state.fset(self, STATE_STOPPED)
116116
elif self.state != STATE_STOPPED:
@@ -150,8 +150,8 @@ async def keepalive(self):
150150
if time_delta < 0:
151151
# Negitive time should indicate no pong reply so consider the
152152
# websocket disconnected.
153-
await OpenEVSEWebsocket.state.fset(self, STATE_DISCONNECTED)
154153
self._error_reason = ERROR_PING_TIMEOUT
154+
await OpenEVSEWebsocket.state.fset(self, STATE_DISCONNECTED)
155155

156156
data = json.dumps({"ping": 1})
157157
_LOGGER.debug("Sending message: %s to websocket.", data)

tests/conftest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ def test_charger(mock_aioclient):
7373
return main.OpenEVSE(TEST_TLD)
7474

7575

76+
@pytest.fixture(name="test_charger_timeout")
77+
def test_charger_timeout(mock_aioclient):
78+
"""Load the charger data."""
79+
mock_aioclient.get(
80+
TEST_URL_STATUS,
81+
exception=TimeoutError,
82+
)
83+
return main.OpenEVSE(TEST_TLD)
84+
85+
7686
@pytest.fixture(name="test_charger_dev")
7787
def test_charger_dev(mock_aioclient):
7888
"""Load the charger data."""

tests/test_main.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ async def test_send_command_async_timeout(test_charger_auth, mock_aioclient, cap
154154
exception=TimeoutError,
155155
)
156156
with caplog.at_level(logging.DEBUG):
157-
await test_charger_auth.send_command("test")
157+
with pytest.raises(TimeoutError):
158+
await test_charger_auth.send_command("test")
158159
assert main.ERROR_TIMEOUT in caplog.text
159160

160161

@@ -165,7 +166,8 @@ async def test_send_command_server_timeout(test_charger_auth, mock_aioclient, ca
165166
exception=ServerTimeoutError,
166167
)
167168
with caplog.at_level(logging.DEBUG):
168-
await test_charger_auth.send_command("test")
169+
with pytest.raises(main.ServerTimeoutError):
170+
await test_charger_auth.send_command("test")
169171
assert f"{main.ERROR_TIMEOUT}: {TEST_URL_RAPI}" in caplog.text
170172

171173

@@ -838,9 +840,10 @@ async def test_toggle_override_v2_err(test_charger_v2, mock_aioclient, caplog):
838840
),
839841
)
840842
with caplog.at_level(logging.DEBUG):
841-
await test_charger_v2.toggle_override()
843+
with pytest.raises(main.ContentTypeError):
844+
await test_charger_v2.toggle_override()
842845
assert (
843-
"Toggle response: 0, message='Attempt to decode JSON with unexpected mimetype: text/html', url='http://openevse.test.tld/r'"
846+
"Content error: Attempt to decode JSON with unexpected mimetype: text/html"
844847
in caplog.text
845848
)
846849

@@ -2144,3 +2147,15 @@ async def test_get_shaper_updated(fixture, expected, request):
21442147
status = charger.shaper_updated
21452148
assert status == expected
21462149
await charger.ws_disconnect()
2150+
2151+
2152+
async def test_get_status(test_charger_timeout, caplog):
2153+
"""Test v4 Status reply."""
2154+
with caplog.at_level(logging.DEBUG):
2155+
with pytest.raises(TimeoutError):
2156+
await test_charger_timeout.update()
2157+
assert test_charger_timeout.websocket is None
2158+
assert not test_charger_timeout._ws_listening
2159+
assert "Updating data from http://openevse.test.tld/status" in caplog.text
2160+
assert "Status update:" not in caplog.text
2161+
assert "Config update:" not in caplog.text

0 commit comments

Comments
 (0)