Skip to content

Commit 3bf5c47

Browse files
authored
feat: add test_and_get function (#87)
* feat: add test_and_get function * linting * more test coverage
1 parent 5570acd commit 3bf5c47

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

openevsehttp/__init__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
AlreadyListening,
1717
AuthenticationError,
1818
MissingMethod,
19+
MissingSerial,
1920
ParseJSONError,
2021
UnknownError,
2122
)
@@ -287,6 +288,28 @@ async def update(self) -> None:
287288
self.url, self._update_status, self._user, self._pwd
288289
)
289290

291+
async def test_and_get(self) -> dict:
292+
"""Test connection.
293+
294+
Return model serial number as dict
295+
"""
296+
url = f"{self.url}config"
297+
data = {}
298+
299+
response = await self.process_request(url, method="get")
300+
if "wifi_serial" in response:
301+
serial = response["wifi_serial"]
302+
else:
303+
_LOGGER.debug("Older firmware detected, missing serial.")
304+
raise MissingSerial
305+
if "buildenv" in response:
306+
model = response["buildenv"]
307+
else:
308+
model = "unknown"
309+
310+
data = {"serial": serial, "model": model}
311+
return data
312+
290313
def ws_start(self):
291314
"""Start the websocket listener."""
292315
if self._ws_listening:

openevsehttp/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ class MissingMethod(Exception):
1919

2020
class AlreadyListening(Exception):
2121
"""Exception for already listening websocket."""
22+
23+
24+
class MissingSerial(Exception):
25+
"""Exception for missing serial number."""

tests/test_init.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from aiohttp.client_exceptions import ContentTypeError, ServerTimeoutError
1010

1111
import openevsehttp
12+
from tests.common import load_fixture
13+
from openevsehttp.exceptions import MissingSerial
1214

1315
pytestmark = pytest.mark.asyncio
1416

@@ -737,3 +739,20 @@ async def test_set_divertmode(test_charger_v2, mock_aioclient, caplog):
737739
await test_charger_v2.divert_mode("normal")
738740
assert "Setting charge mode to normal" in caplog.text
739741
assert "Non JSON response: Divert Mode changed" in caplog.text
742+
743+
744+
async def test_test_and_get(test_charger, test_charger_v2, mock_aioclient, caplog):
745+
"""Test v4 Status reply"""
746+
data = await test_charger.test_and_get()
747+
mock_aioclient.get(
748+
TEST_URL_CONFIG,
749+
status=200,
750+
body=load_fixture("v4_json/config.json"),
751+
)
752+
assert data["serial"] == "1234567890AB"
753+
assert data["model"] == "unknown"
754+
755+
with pytest.raises(MissingSerial):
756+
with caplog.at_level(logging.DEBUG):
757+
data = await test_charger_v2.test_and_get()
758+
assert "Older firmware detected, missing serial." in caplog.text

tox.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ skip_missing_interpreters = True
44

55
[gh-actions]
66
python =
7-
3.8: py38, lint, mypy
7+
3.8: py38
88
3.9: py39
9-
3.10: py310
9+
3.10: py310, lint, mypy
1010

1111
[testenv]
1212
commands =

0 commit comments

Comments
 (0)