Skip to content

Commit 3b17141

Browse files
authored
feat: create async_charge_current to parse the charge_current (#391)
* feat: create async_charge_current to parse the charge_current * update tests * update tests * formatting * more test updates * adjust list claims to check claims target endpoint
1 parent baa5882 commit 3b17141

File tree

4 files changed

+113
-5
lines changed

4 files changed

+113
-5
lines changed

openevsehttp/__main__.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -815,13 +815,17 @@ async def release_claim(self, client: int = CLIENT) -> Any:
815815
response = await self.process_request(url=url, method="delete") # noqa: E501
816816
return response
817817

818-
async def list_claims(self) -> Any:
818+
async def list_claims(self, target: bool | None = None) -> Any:
819819
"""List all claims."""
820820
if not self._version_check("4.1.0"):
821821
_LOGGER.debug("Feature not supported for older firmware.")
822822
raise UnsupportedFeature
823823

824-
url = f"{self.url}claims"
824+
target_check = ""
825+
if target:
826+
target_check = "/target"
827+
828+
url = f"{self.url}claims{target_check}"
825829

826830
_LOGGER.debug("Getting claims on %s", url)
827831
response = await self.process_request(url=url, method="get") # noqa: E501
@@ -922,6 +926,20 @@ def max_current_soft(self) -> int | None:
922926
return self._config["max_current_soft"]
923927
return self._status["pilot"]
924928

929+
@property
930+
async def async_charge_current(self) -> int | None:
931+
"""Return the charge current."""
932+
try:
933+
claims = None
934+
claims = await self.list_claims(target=True)
935+
except UnsupportedFeature:
936+
pass
937+
if claims is not None and "charge_current" in claims["properties"].keys():
938+
return claims["properties"]["charge_current"]
939+
if self._config is not None and "max_current_soft" in self._config:
940+
return self._config["max_current_soft"]
941+
return self._status["pilot"]
942+
925943
@property
926944
def max_current(self) -> int | None:
927945
"""Return the max current."""

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
PROJECT_DIR = Path(__file__).parent.resolve()
88
README_FILE = PROJECT_DIR / "README.md"
9-
VERSION = "0.1.63"
9+
VERSION = "0.1.64"
1010

1111
setup(
1212
name="python-openevse-http",

tests/conftest.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
TEST_URL_STATUS = "http://openevse.test.tld/status"
1212
TEST_URL_CONFIG = "http://openevse.test.tld/config"
1313
TEST_URL_RAPI = "http://openevse.test.tld/r"
14+
TEST_URL_WS = "ws://openevse.test.tld/ws"
1415
TEST_TLD = "openevse.test.tld"
1516

1617

@@ -27,6 +28,12 @@ def test_charger_auth(mock_aioclient):
2728
status=200,
2829
body=load_fixture("v4_json/config.json"),
2930
)
31+
mock_aioclient.get(
32+
TEST_URL_WS,
33+
status=200,
34+
body=load_fixture("v4_json/status.json"),
35+
repeat=True,
36+
)
3037
return main.OpenEVSE(TEST_TLD, user="testuser", pwd="fakepassword")
3138

3239

@@ -57,6 +64,12 @@ def test_charger(mock_aioclient):
5764
status=200,
5865
body=load_fixture("v4_json/config.json"),
5966
)
67+
mock_aioclient.get(
68+
TEST_URL_WS,
69+
status=200,
70+
body=load_fixture("v4_json/status.json"),
71+
repeat=True,
72+
)
6073
return main.OpenEVSE(TEST_TLD)
6174

6275

@@ -73,6 +86,12 @@ def test_charger_dev(mock_aioclient):
7386
status=200,
7487
body=load_fixture("v4_json/config-dev.json"),
7588
)
89+
mock_aioclient.get(
90+
TEST_URL_WS,
91+
status=200,
92+
body=load_fixture("v4_json/status.json"),
93+
repeat=True,
94+
)
7695
return main.OpenEVSE(TEST_TLD)
7796

7897

@@ -89,6 +108,12 @@ def test_charger_new(mock_aioclient):
89108
status=200,
90109
body=load_fixture("v4_json/config-new.json"),
91110
)
111+
mock_aioclient.get(
112+
TEST_URL_WS,
113+
status=200,
114+
body=load_fixture("v4_json/status-new.json"),
115+
repeat=True,
116+
)
92117
return main.OpenEVSE(TEST_TLD)
93118

94119

tests/test_main.py

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
UnsupportedFeature,
2020
)
2121
from tests.common import load_fixture
22+
from openevsehttp.websocket import SIGNAL_CONNECTION_STATE, STATE_CONNECTED
2223

2324
pytestmark = pytest.mark.asyncio
2425

@@ -31,6 +32,7 @@
3132
TEST_URL_LIMIT = "http://openevse.test.tld/limit"
3233
TEST_URL_WS = "ws://openevse.test.tld/ws"
3334
TEST_URL_CLAIMS = "http://openevse.test.tld/claims"
35+
TEST_URL_CLAIMS_TARGET = "http://openevse.test.tld/claims/target"
3436
TEST_URL_GITHUB_v4 = (
3537
"https://api.github.com/repos/OpenEVSE/ESP32_WiFi_V4.x/releases/latest"
3638
)
@@ -46,6 +48,20 @@ async def test_get_status_auth(test_charger_auth):
4648
assert status == "sleeping"
4749

4850

51+
async def test_ws_state(test_charger):
52+
"""Test v4 Status reply."""
53+
await test_charger.update()
54+
value = test_charger.ws_state
55+
assert value == None
56+
57+
58+
async def test_update_status(test_charger):
59+
"""Test v4 Status reply."""
60+
data = json.loads(load_fixture("v4_json/status.json"))
61+
await test_charger._update_status("data", data, None)
62+
assert test_charger._status == data
63+
64+
4965
async def test_get_status_auth_err(test_charger_auth_err):
5066
"""Test v4 Status reply."""
5167
with pytest.raises(main.AuthenticationError):
@@ -110,6 +126,13 @@ async def test_send_command_parse_err(test_charger_auth, mock_aioclient):
110126
status = await test_charger_auth.send_command("test")
111127
assert status is None
112128

129+
mock_aioclient.post(
130+
TEST_URL_RAPI, status=400, body='{"error": "Could not parse JSON"}'
131+
)
132+
with pytest.raises(main.ParseJSONError):
133+
status = await test_charger_auth.send_command("test")
134+
assert status is None
135+
113136

114137
async def test_send_command_auth_err(test_charger_auth, mock_aioclient):
115138
"""Test v4 Status reply."""
@@ -947,7 +970,7 @@ async def test_test_and_get(test_charger, test_charger_v2, mock_aioclient, caplo
947970
assert "Older firmware detected, missing serial." in caplog.text
948971

949972

950-
async def test_restart(test_charger_modified_ver, mock_aioclient, caplog):
973+
async def test_restart_wifi(test_charger_modified_ver, mock_aioclient, caplog):
951974
"""Test v4 set divert mode."""
952975
await test_charger_modified_ver.update()
953976
mock_aioclient.post(
@@ -1057,7 +1080,9 @@ async def test_firmware_check(
10571080
assert firmware is None
10581081

10591082

1060-
async def test_evse_restart(test_charger_v2, mock_aioclient, caplog):
1083+
async def test_evse_restart(
1084+
test_charger_v2, test_charger_modified_ver, mock_aioclient, caplog
1085+
):
10611086
"""Test EVSE module restart."""
10621087
await test_charger_v2.update()
10631088
value = {"cmd": "OK", "ret": "$OK^20"}
@@ -1070,6 +1095,16 @@ async def test_evse_restart(test_charger_v2, mock_aioclient, caplog):
10701095
await test_charger_v2.restart_evse()
10711096
assert "EVSE Restart response: $OK^20" in caplog.text
10721097

1098+
await test_charger_modified_ver.update()
1099+
mock_aioclient.post(
1100+
TEST_URL_RESTART,
1101+
status=200,
1102+
body='{"msg": "restart evse"}',
1103+
)
1104+
with caplog.at_level(logging.DEBUG):
1105+
await test_charger_modified_ver.restart_evse()
1106+
assert "Restarting EVSE module via HTTP" in caplog.text
1107+
10731108

10741109
@pytest.mark.parametrize(
10751110
"fixture, expected", [("test_charger", True), ("test_charger_v2", None)]
@@ -1869,3 +1904,33 @@ async def test_set_led_brightness(
18691904
with caplog.at_level(logging.DEBUG):
18701905
await test_charger_v2.set_led_brightness(255)
18711906
assert "Feature not supported for older firmware." in caplog.text
1907+
1908+
1909+
async def test_async_charge_current(
1910+
test_charger, test_charger_v2, mock_aioclient, caplog
1911+
):
1912+
"""Test async_charge_current function."""
1913+
await test_charger.update()
1914+
mock_aioclient.get(
1915+
TEST_URL_CLAIMS_TARGET,
1916+
status=200,
1917+
body='{"properties":{"state":"disabled","charge_current":28,"max_current":23,"auto_release":false},"claims":{"state":65540,"charge_current":65537,"max_current":65548}}',
1918+
repeat=False,
1919+
)
1920+
1921+
value = await test_charger.async_charge_current
1922+
assert value == 28
1923+
1924+
mock_aioclient.get(
1925+
TEST_URL_CLAIMS_TARGET,
1926+
status=200,
1927+
body='{"properties":{"state":"disabled","max_current":23,"auto_release":false},"claims":{"state":65540,"charge_current":65537,"max_current":65548}}',
1928+
repeat=False,
1929+
)
1930+
1931+
value = await test_charger.async_charge_current
1932+
assert value == 48
1933+
1934+
await test_charger_v2.update()
1935+
value = await test_charger_v2.async_charge_current
1936+
assert value == 25

0 commit comments

Comments
 (0)