Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions openevsehttp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
AlreadyListening,
AuthenticationError,
MissingMethod,
MissingSerial,
ParseJSONError,
UnknownError,
)
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions openevsehttp/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ class MissingMethod(Exception):

class AlreadyListening(Exception):
"""Exception for already listening websocket."""


class MissingSerial(Exception):
"""Exception for missing serial number."""
19 changes: 19 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down