Skip to content

Commit 798cb9a

Browse files
authored
feat: set running divertmode (#430)
* feat: set running `divertmode` * formatting and linting * update tests * formatting
1 parent 08f8773 commit 798cb9a

2 files changed

Lines changed: 86 additions & 14 deletions

File tree

openevsehttp/__main__.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@
6464
255: "disabled",
6565
}
6666

67+
divert_mode = {
68+
"fast": 1,
69+
"eco": 2,
70+
}
71+
6772
ERROR_TIMEOUT = "Timeout while updating"
6873
INFO_LOOP_RUNNING = "Event loop already running, not creating new one."
6974
UPDATE_TRIGGERS = [
@@ -328,7 +333,7 @@ async def get_schedule(self) -> Union[Dict[str, str], Dict[str, Any]]:
328333
return response
329334

330335
async def set_charge_mode(self, mode: str = "fast") -> None:
331-
"""Set the charge mode."""
336+
"""Set the charge mode at startup setting."""
332337
url = f"{self.url}config"
333338

334339
if mode not in ["fast", "eco"]:
@@ -868,6 +873,26 @@ async def set_led_brightness(self, level: int) -> None:
868873
_LOGGER.debug("Setting LED brightness to %s", level)
869874
await self.process_request(url=url, method="post", data=data) # noqa: E501
870875

876+
async def set_divert_mode(self, mode: str = "fast") -> None:
877+
"""Set the divert mode."""
878+
url = f"{self.url}divertmode"
879+
880+
if mode not in ["fast", "eco"]:
881+
_LOGGER.error("Invalid value for charge_mode: %s", mode)
882+
raise ValueError
883+
884+
_LOGGER.debug("Setting divert mode to %s", mode)
885+
886+
# convert text to int
887+
new_mode = divert_mode[mode]
888+
data = {"divertmode": new_mode}
889+
response = await self.process_request(
890+
url=url, method="get", data=data
891+
) # noqa: E501
892+
if response != "Divert Mode changed":
893+
_LOGGER.error("Problem issuing command: %s", response)
894+
raise UnknownError
895+
871896
@property
872897
def led_brightness(self) -> str:
873898
"""Return charger led_brightness."""
@@ -1221,7 +1246,7 @@ def divertmode(self) -> str:
12211246
assert self._status is not None
12221247
mode = self._status["divertmode"]
12231248
if mode == 1:
1224-
return "normal"
1249+
return "fast"
12251250
return "eco"
12261251

12271252
@property

tests/test_main.py

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -641,18 +641,6 @@ async def test_get_relayt(fixture, expected, request):
641641
await charger.ws_disconnect()
642642

643643

644-
@pytest.mark.parametrize(
645-
"fixture, expected", [("test_charger", "eco"), ("test_charger_v2", "normal")]
646-
)
647-
async def test_get_divertmode(fixture, expected, request):
648-
"""Test v4 Status reply."""
649-
charger = request.getfixturevalue(fixture)
650-
await charger.update()
651-
status = charger.divertmode
652-
assert status == expected
653-
await charger.ws_disconnect()
654-
655-
656644
@pytest.mark.parametrize(
657645
"fixture, expected", [("test_charger", 0), ("test_charger_v2", 0)]
658646
)
@@ -2159,3 +2147,62 @@ async def test_get_status(test_charger_timeout, caplog):
21592147
assert "Updating data from http://openevse.test.tld/status" in caplog.text
21602148
assert "Status update:" not in caplog.text
21612149
assert "Config update:" not in caplog.text
2150+
2151+
2152+
@pytest.mark.parametrize(
2153+
"fixture, expected",
2154+
[
2155+
("test_charger", "eco"),
2156+
("test_charger_v2", "fast"),
2157+
("test_charger_broken", "eco"),
2158+
("test_charger_new", "fast"),
2159+
],
2160+
)
2161+
async def test_divertmode(fixture, expected, request):
2162+
"""Test divertmode property."""
2163+
charger = request.getfixturevalue(fixture)
2164+
await charger.update()
2165+
status = charger.divertmode
2166+
assert status == expected
2167+
await charger.ws_disconnect()
2168+
2169+
2170+
async def test_set_divert_mode(
2171+
test_charger_new, test_charger_v2, mock_aioclient, caplog
2172+
):
2173+
"""Test set_divert_mode reply."""
2174+
await test_charger_new.update()
2175+
value = "Divert Mode changed"
2176+
mock_aioclient.get(
2177+
TEST_URL_DIVERT,
2178+
status=200,
2179+
body=value,
2180+
)
2181+
with caplog.at_level(logging.DEBUG):
2182+
await test_charger_new.set_divert_mode("fast")
2183+
assert "Setting divert mode to fast" in caplog.text
2184+
2185+
mock_aioclient.get(
2186+
TEST_URL_DIVERT,
2187+
status=200,
2188+
body=value,
2189+
)
2190+
await test_charger_v2.update()
2191+
with caplog.at_level(logging.DEBUG):
2192+
await test_charger_v2.set_divert_mode("eco")
2193+
assert "Setting divert mode to eco" in caplog.text
2194+
2195+
with pytest.raises(ValueError):
2196+
with caplog.at_level(logging.DEBUG):
2197+
await test_charger_new.set_divert_mode("test")
2198+
assert "Invalid value for charge_mode: test" in caplog.text
2199+
2200+
mock_aioclient.get(
2201+
TEST_URL_DIVERT,
2202+
status=200,
2203+
body="error",
2204+
)
2205+
with pytest.raises(UnknownError):
2206+
with caplog.at_level(logging.DEBUG):
2207+
await test_charger_new.set_divert_mode("fast")
2208+
assert "Problem issuing command: error" in caplog.text

0 commit comments

Comments
 (0)