Skip to content

Commit 6212349

Browse files
authored
fix: update set_current to work with older firmware (#40)
* fix: update set_current to work with older firmware * tests: add test for set_current on old firmware * tests: formatting
1 parent 48de91e commit 6212349

File tree

4 files changed

+45
-19
lines changed

4 files changed

+45
-19
lines changed

openevsehttp/__init__.py

+28-16
Original file line numberDiff line numberDiff line change
@@ -444,24 +444,36 @@ async def clear_override(self) -> None:
444444

445445
async def set_current(self, amps: int = 6) -> None:
446446
"""Set the soft current limit."""
447-
url = f"{self.url}config"
448-
449-
if (
450-
amps < self._config["min_current_hard"]
451-
or amps > self._config["max_current_hard"]
452-
):
453-
_LOGGER.error("Invalid value for max_current_soft: %s", amps)
454-
raise ValueError
447+
# 3.x - 4.1.0: use RAPI commands $SC <amps>
448+
# 4.1.2: use HTTP API call
455449

456-
data = {"max_current_soft": amps}
450+
cutoff = AwesomeVersion("4.1.2")
451+
current = AwesomeVersion(self._config["version"])
457452

458-
_LOGGER.debug("Setting max_current_soft to %s", amps)
459-
response = await self.process_request(
460-
url=url, method="post", data=data
461-
) # noqa: E501
462-
if response["msg"] != "done":
463-
_LOGGER.error("Problem issuing command: %s", response["msg"])
464-
raise UnknownError
453+
if cutoff <= current:
454+
url = f"{self.url}config"
455+
456+
if (
457+
amps < self._config["min_current_hard"]
458+
or amps > self._config["max_current_hard"]
459+
):
460+
_LOGGER.error("Invalid value for max_current_soft: %s", amps)
461+
raise ValueError
462+
data = {"max_current_soft": amps}
463+
464+
_LOGGER.debug("Setting max_current_soft to %s", amps)
465+
response = await self.process_request(
466+
url=url, method="post", data=data
467+
) # noqa: E501
468+
if response["msg"] != "done":
469+
_LOGGER.error("Problem issuing command: %s", response["msg"])
470+
raise UnknownError
471+
else:
472+
# RAPI commands
473+
_LOGGER.debug("Setting current via RAPI")
474+
command = f"$SC {amps}"
475+
response = await self.send_command(command)
476+
_LOGGER.debug("Set current response: %s", response[1])
465477

466478
@property
467479
def hostname(self) -> str:

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
PROJECT_DIR = Path(__file__).parent.resolve()
77
README_FILE = PROJECT_DIR / "README.md"
8-
VERSION = "0.1.16"
8+
VERSION = "0.1.17"
99

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

tests/fixtures/v4_json/config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"protocol": "-",
44
"espflash": 4194304,
55
"wifi_serial": "1234567890AB",
6-
"version": "4.0.0",
6+
"version": "4.1.2",
77
"diodet": 0,
88
"gfcit": 0,
99
"groundt": 0,

tests/test_init.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ async def test_get_service_level(fixture, expected, request):
186186

187187

188188
@pytest.mark.parametrize(
189-
"fixture, expected", [("test_charger", "4.0.0"), ("test_charger_v2", "2.9.1")]
189+
"fixture, expected", [("test_charger", "4.1.2"), ("test_charger_v2", "2.9.1")]
190190
)
191191
async def test_get_wifi_firmware(fixture, expected, request):
192192
"""Test v4 Status reply"""
@@ -650,3 +650,17 @@ async def test_set_current_error(test_charger, mock_aioclient, caplog):
650650
with pytest.raises(ValueError):
651651
await test_charger.set_current(60)
652652
assert "Invalid value for max_current_soft: 60" in caplog.text
653+
654+
655+
async def test_set_current_v2(test_charger_v2, mock_aioclient, caplog):
656+
"""Test v4 Status reply"""
657+
await test_charger_v2.update()
658+
value = {"cmd": "OK", "ret": "$OK^20"}
659+
mock_aioclient.post(
660+
TEST_URL_RAPI,
661+
status=200,
662+
body=json.dumps(value),
663+
)
664+
with caplog.at_level(logging.DEBUG):
665+
await test_charger_v2.set_current(12)
666+
assert "Setting current via RAPI" in caplog.text

0 commit comments

Comments
 (0)