Skip to content

Commit 6952a4f

Browse files
authored
feat: add claims functions (#320)
* feat: add claims functions * add python 3.12 to tests * update tox config * formatting * update tests * formatting * more tests * fix error message
1 parent 68bd538 commit 6952a4f

File tree

6 files changed

+138
-3
lines changed

6 files changed

+138
-3
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ jobs:
2020
python-version:
2121
- "3.10"
2222
- "3.11"
23+
- "3.12"
2324

2425
steps:
2526
- uses: actions/[email protected]

openevsehttp/__main__.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .const import (
1818
BAT_LVL,
1919
BAT_RANGE,
20+
CLIENT,
2021
GRID,
2122
MAX_AMPS,
2223
MIN_AMPS,
@@ -757,6 +758,67 @@ async def get_limit(self) -> Any:
757758
) # noqa: E501
758759
return response
759760

761+
async def make_claim(
762+
self,
763+
state: str | None = None,
764+
charge_current: int | None = None,
765+
max_current: int | None = None,
766+
auto_release: bool = True,
767+
client: int = CLIENT,
768+
) -> Any:
769+
"""Make a claim."""
770+
if not self._version_check("4.1.0"):
771+
_LOGGER.debug("Feature not supported for older firmware.")
772+
raise UnsupportedFeature
773+
774+
if state not in ["active", "disabled", None]:
775+
_LOGGER.error("Invalid claim state: %s", state)
776+
raise ValueError
777+
778+
url = f"{self.url}claims/{client}"
779+
780+
data: dict[str, Any] = {}
781+
782+
data["auto_release"] = auto_release
783+
784+
if state is not None:
785+
data["state"] = state
786+
if charge_current is not None:
787+
data["charge_current"] = charge_current
788+
if max_current is not None:
789+
data["max_current"] = max_current
790+
791+
_LOGGER.debug("Claim data: %s", data)
792+
_LOGGER.debug("Setting up claim on %s", url)
793+
response = await self.process_request(
794+
url=url, method="post", data=data
795+
) # noqa: E501
796+
return response
797+
798+
async def release_claim(self, client: int = CLIENT) -> Any:
799+
"""Delete a claim."""
800+
if not self._version_check("4.1.0"):
801+
_LOGGER.debug("Feature not supported for older firmware.")
802+
raise UnsupportedFeature
803+
804+
url = f"{self.url}claims/{client}"
805+
806+
_LOGGER.debug("Releasing claim on %s", url)
807+
response = await self.process_request(url=url, method="delete") # noqa: E501
808+
return response
809+
810+
async def list_claims(self) -> Any:
811+
"""List all claims."""
812+
if not self._version_check("4.1.0"):
813+
_LOGGER.debug("Feature not supported for older firmware.")
814+
raise UnsupportedFeature
815+
816+
url = f"{self.url}claims"
817+
818+
_LOGGER.debug("Getting claims on %s", url)
819+
response = await self.process_request(url=url, method="get") # noqa: E501
820+
return response
821+
760822
@property
761823
def hostname(self) -> str:
762824
"""Return charger hostname."""

openevsehttp/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
TYPE = "type"
1515
VALUE = "value"
1616
RELEASE = "release"
17+
CLIENT = 4

setup.py

Lines changed: 2 additions & 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.58"
9+
VERSION = "0.1.59"
1010

1111
setup(
1212
name="python-openevse-http",
@@ -31,5 +31,6 @@
3131
"Programming Language :: Python :: 3",
3232
"Programming Language :: Python :: 3.10",
3333
"Programming Language :: Python :: 3.11",
34+
"Programming Language :: Python :: 3.12",
3435
],
3536
)

tests/test_main.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
TEST_URL_RESTART = "http://openevse.test.tld/restart"
3131
TEST_URL_LIMIT = "http://openevse.test.tld/limit"
3232
TEST_URL_WS = "ws://openevse.test.tld/ws"
33+
TEST_URL_CLAIMS = "http://openevse.test.tld/claims"
3334
TEST_URL_GITHUB_v4 = (
3435
"https://api.github.com/repos/OpenEVSE/ESP32_WiFi_V4.x/releases/latest"
3536
)
@@ -1682,3 +1683,71 @@ async def test_voltage(test_charger, test_charger_v2, mock_aioclient, caplog):
16821683
with caplog.at_level(logging.DEBUG):
16831684
await test_charger_v2.grid_voltage(210)
16841685
assert "Feature not supported for older firmware." in caplog.text
1686+
1687+
1688+
async def test_list_claims(test_charger, test_charger_v2, mock_aioclient, caplog):
1689+
"""Test list_claims function."""
1690+
await test_charger.update()
1691+
mock_aioclient.get(
1692+
TEST_URL_CLAIMS,
1693+
status=200,
1694+
body='[{"client":65540,"priority":10,"state":"disabled","auto_release":false}]',
1695+
repeat=True,
1696+
)
1697+
with caplog.at_level(logging.DEBUG):
1698+
await test_charger.list_claims()
1699+
assert f"Getting claims on {TEST_URL_CLAIMS}" in caplog.text
1700+
1701+
with pytest.raises(UnsupportedFeature):
1702+
with caplog.at_level(logging.DEBUG):
1703+
await test_charger_v2.list_claims()
1704+
assert "Feature not supported for older firmware." in caplog.text
1705+
1706+
1707+
async def test_release_claim(test_charger, test_charger_v2, mock_aioclient, caplog):
1708+
"""Test release_claim function."""
1709+
await test_charger.update()
1710+
mock_aioclient.delete(
1711+
f"{TEST_URL_CLAIMS}/4",
1712+
status=200,
1713+
body='[{"msg":"done"}]',
1714+
repeat=True,
1715+
)
1716+
with caplog.at_level(logging.DEBUG):
1717+
await test_charger.release_claim()
1718+
assert f"Releasing claim on {TEST_URL_CLAIMS}/4" in caplog.text
1719+
1720+
with pytest.raises(UnsupportedFeature):
1721+
with caplog.at_level(logging.DEBUG):
1722+
await test_charger_v2.release_claim()
1723+
assert "Feature not supported for older firmware." in caplog.text
1724+
1725+
1726+
async def test_make_claim(test_charger, test_charger_v2, mock_aioclient, caplog):
1727+
"""Test make_claim function."""
1728+
await test_charger.update()
1729+
mock_aioclient.post(
1730+
f"{TEST_URL_CLAIMS}/4",
1731+
status=200,
1732+
body='[{"msg":"done"}]',
1733+
repeat=True,
1734+
)
1735+
with caplog.at_level(logging.DEBUG):
1736+
await test_charger.make_claim(
1737+
state="disabled", charge_current=20, max_current=20
1738+
)
1739+
assert (
1740+
"Claim data: {'auto_release': True, 'state': 'disabled', 'charge_current': 20, 'max_current': 20}"
1741+
in caplog.text
1742+
)
1743+
assert f"Setting up claim on {TEST_URL_CLAIMS}/4" in caplog.text
1744+
1745+
with pytest.raises(ValueError):
1746+
with caplog.at_level(logging.DEBUG):
1747+
await test_charger.make_claim("invalid")
1748+
assert "Invalid claim state: invalid" in caplog.text
1749+
1750+
with pytest.raises(UnsupportedFeature):
1751+
with caplog.at_level(logging.DEBUG):
1752+
await test_charger_v2.make_claim()
1753+
assert "Feature not supported for older firmware." in caplog.text

tox.ini

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
[tox]
2-
envlist = py310, py311, lint, mypy
2+
envlist = py310, py311, py312, lint, mypy
33
skip_missing_interpreters = True
44

55
[gh-actions]
66
python =
7-
3.10: py310, lint, mypy
7+
3.10: py310
88
3.11: py311
9+
3.12: py312, lint, mypy
910

1011
[testenv]
1112
commands =

0 commit comments

Comments
 (0)