Skip to content

Commit dbe2a77

Browse files
authored
fix: update config data when config_update signaled and fix set_charge_mode (#203)
* fix: update config data when config_update signaled * fix set_charge_mode function * formatting * update test * formatting
1 parent 72fc8ec commit dbe2a77

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

openevsehttp/__main__.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ async def process_request(
113113
if resp.status in [404, 405, 500]:
114114
_LOGGER.error("%s", message)
115115

116+
if method == "post" and "config_version" in message:
117+
await self.update()
116118
return message
117119

118120
except (TimeoutError, ServerTimeoutError):
@@ -264,7 +266,7 @@ async def set_charge_mode(self, mode: str = "fast") -> None:
264266
"""Set the charge mode."""
265267
url = f"{self.url}config"
266268

267-
if mode != "fast" or mode != "eco":
269+
if mode not in ["fast", "eco"]:
268270
_LOGGER.error("Invalid value for charge_mode: %s", mode)
269271
raise ValueError
270272

@@ -274,7 +276,8 @@ async def set_charge_mode(self, mode: str = "fast") -> None:
274276
response = await self.process_request(
275277
url=url, method="post", data=data
276278
) # noqa: E501
277-
if response["msg"] != "done":
279+
result = response["msg"]
280+
if result not in ["done", "no change"]:
278281
_LOGGER.error("Problem issuing command: %s", response["msg"])
279282
raise UnknownError
280283

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.41"
8+
VERSION = "0.1.42"
99

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

tests/test_main.py

+54-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
from aiohttp.client_exceptions import ContentTypeError, ServerTimeoutError
1010

1111
import openevsehttp.__main__ as main
12+
from openevsehttp.exceptions import MissingSerial, UnknownError, UnsupportedFeature
1213
from tests.common import load_fixture
13-
from openevsehttp.exceptions import MissingSerial, UnsupportedFeature
1414

1515
pytestmark = pytest.mark.asyncio
1616

17+
TEST_URL_STATUS = "http://openevse.test.tld/status"
1718
TEST_URL_RAPI = "http://openevse.test.tld/r"
1819
TEST_URL_OVERRIDE = "http://openevse.test.tld/override"
1920
TEST_URL_CONFIG = "http://openevse.test.tld/config"
@@ -1203,3 +1204,55 @@ async def test_version_check(test_charger_new, mock_aioclient, caplog):
12031204

12041205
result = test_charger_new._version_check("4.0.0", "4.1.7")
12051206
assert not result
1207+
1208+
1209+
async def test_set_charge_mode(test_charger, mock_aioclient, caplog):
1210+
"""Test v4 Status reply."""
1211+
await test_charger.update()
1212+
value = {"msg": "done"}
1213+
mock_aioclient.post(
1214+
TEST_URL_CONFIG,
1215+
status=200,
1216+
body=json.dumps(value),
1217+
)
1218+
with caplog.at_level(logging.DEBUG):
1219+
await test_charger.set_charge_mode("eco")
1220+
1221+
mock_aioclient.get(
1222+
TEST_URL_STATUS,
1223+
status=200,
1224+
body=load_fixture("v4_json/status.json"),
1225+
)
1226+
mock_aioclient.get(
1227+
TEST_URL_CONFIG,
1228+
status=200,
1229+
body=load_fixture("v4_json/config.json"),
1230+
)
1231+
value = {"config_version": 2, "msg": "done"}
1232+
mock_aioclient.post(
1233+
TEST_URL_CONFIG,
1234+
status=200,
1235+
body=json.dumps(value),
1236+
)
1237+
with caplog.at_level(logging.DEBUG):
1238+
await test_charger.set_charge_mode("fast")
1239+
1240+
value = {"msg": "error"}
1241+
mock_aioclient.post(
1242+
TEST_URL_CONFIG,
1243+
status=200,
1244+
body=json.dumps(value),
1245+
)
1246+
with caplog.at_level(logging.DEBUG):
1247+
with pytest.raises(UnknownError):
1248+
await test_charger.set_charge_mode("fast")
1249+
assert "Problem issuing command: error" in caplog.text
1250+
1251+
value = {"msg": "done"}
1252+
mock_aioclient.post(
1253+
TEST_URL_CONFIG,
1254+
status=200,
1255+
body=json.dumps(value),
1256+
)
1257+
with pytest.raises(ValueError):
1258+
await test_charger.set_charge_mode("test")

0 commit comments

Comments
 (0)