diff --git a/openevsehttp/__init__.py b/openevsehttp/__init__.py index 0e94674..1af82d4 100644 --- a/openevsehttp/__init__.py +++ b/openevsehttp/__init__.py @@ -16,6 +16,7 @@ AlreadyListening, AuthenticationError, MissingMethod, + MissingSerial, ParseJSONError, UnknownError, ) @@ -287,6 +288,28 @@ async def update(self) -> None: self.url, self._update_status, self._user, self._pwd ) + async def test_and_get(self) -> dict: + """Test connection. + + Return model serial number as dict + """ + url = f"{self.url}config" + data = {} + + response = await self.process_request(url, method="get") + if "wifi_serial" in response: + serial = response["wifi_serial"] + else: + _LOGGER.debug("Older firmware detected, missing serial.") + raise MissingSerial + if "buildenv" in response: + model = response["buildenv"] + else: + model = "unknown" + + data = {"serial": serial, "model": model} + return data + def ws_start(self): """Start the websocket listener.""" if self._ws_listening: diff --git a/openevsehttp/exceptions.py b/openevsehttp/exceptions.py index c384f24..6cff328 100644 --- a/openevsehttp/exceptions.py +++ b/openevsehttp/exceptions.py @@ -19,3 +19,7 @@ class MissingMethod(Exception): class AlreadyListening(Exception): """Exception for already listening websocket.""" + + +class MissingSerial(Exception): + """Exception for missing serial number.""" diff --git a/tests/test_init.py b/tests/test_init.py index bd745c6..8691772 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -9,6 +9,8 @@ from aiohttp.client_exceptions import ContentTypeError, ServerTimeoutError import openevsehttp +from tests.common import load_fixture +from openevsehttp.exceptions import MissingSerial pytestmark = pytest.mark.asyncio @@ -737,3 +739,20 @@ async def test_set_divertmode(test_charger_v2, mock_aioclient, caplog): await test_charger_v2.divert_mode("normal") assert "Setting charge mode to normal" in caplog.text assert "Non JSON response: Divert Mode changed" in caplog.text + + +async def test_test_and_get(test_charger, test_charger_v2, mock_aioclient, caplog): + """Test v4 Status reply""" + data = await test_charger.test_and_get() + mock_aioclient.get( + TEST_URL_CONFIG, + status=200, + body=load_fixture("v4_json/config.json"), + ) + assert data["serial"] == "1234567890AB" + assert data["model"] == "unknown" + + with pytest.raises(MissingSerial): + with caplog.at_level(logging.DEBUG): + data = await test_charger_v2.test_and_get() + assert "Older firmware detected, missing serial." in caplog.text diff --git a/tox.ini b/tox.ini index 3cee839..114271f 100644 --- a/tox.ini +++ b/tox.ini @@ -4,9 +4,9 @@ skip_missing_interpreters = True [gh-actions] python = - 3.8: py38, lint, mypy + 3.8: py38 3.9: py39 - 3.10: py310 + 3.10: py310, lint, mypy [testenv] commands =