From e63b6912878c0a6e7ba8977be2e47911e1d9b944 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 16 Mar 2022 11:09:34 -0700 Subject: [PATCH 1/6] fix: update set_current to work with older firmware --- openevsehttp/__init__.py | 44 +++++++++++++++++++----------- setup.py | 2 +- tests/fixtures/v4_json/config.json | 2 +- tests/test_init.py | 2 +- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/openevsehttp/__init__.py b/openevsehttp/__init__.py index 4afd20e..0775a14 100644 --- a/openevsehttp/__init__.py +++ b/openevsehttp/__init__.py @@ -444,24 +444,36 @@ async def clear_override(self) -> None: async def set_current(self, amps: int = 6) -> None: """Set the soft current limit.""" - url = f"{self.url}config" - - if ( - amps < self._config["min_current_hard"] - or amps > self._config["max_current_hard"] - ): - _LOGGER.error("Invalid value for max_current_soft: %s", amps) - raise ValueError + # 3.x - 4.1.0: use RAPI commands $SC + # 4.1.2: use HTTP API call - data = {"max_current_soft": amps} + cutoff = AwesomeVersion("4.1.2") + current = AwesomeVersion(self._config["version"]) - _LOGGER.debug("Setting max_current_soft to %s", amps) - response = await self.process_request( - url=url, method="post", data=data - ) # noqa: E501 - if response["msg"] != "done": - _LOGGER.error("Problem issuing command: %s", response["msg"]) - raise UnknownError + if cutoff <= current: + url = f"{self.url}config" + + if ( + amps < self._config["min_current_hard"] + or amps > self._config["max_current_hard"] + ): + _LOGGER.error("Invalid value for max_current_soft: %s", amps) + raise ValueError + data = {"max_current_soft": amps} + + _LOGGER.debug("Setting max_current_soft to %s", amps) + response = await self.process_request( + url=url, method="post", data=data + ) # noqa: E501 + if response["msg"] != "done": + _LOGGER.error("Problem issuing command: %s", response["msg"]) + raise UnknownError + else: + # RAPI commands + _LOGGER.debug("Setting current via RAPI") + command = f"$SC {amps}" + response = await self.send_command(command) + _LOGGER.debug("Set current response: %s", response[1]) @property def hostname(self) -> str: diff --git a/setup.py b/setup.py index 3e4c576..eae9ca8 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ PROJECT_DIR = Path(__file__).parent.resolve() README_FILE = PROJECT_DIR / "README.md" -VERSION = "0.1.16" +VERSION = "0.1.17" setup( name="python-openevse-http", diff --git a/tests/fixtures/v4_json/config.json b/tests/fixtures/v4_json/config.json index 44d64af..cb6eb56 100644 --- a/tests/fixtures/v4_json/config.json +++ b/tests/fixtures/v4_json/config.json @@ -3,7 +3,7 @@ "protocol": "-", "espflash": 4194304, "wifi_serial": "1234567890AB", - "version": "4.0.0", + "version": "4.1.2", "diodet": 0, "gfcit": 0, "groundt": 0, diff --git a/tests/test_init.py b/tests/test_init.py index ac06cb9..080e613 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -186,7 +186,7 @@ async def test_get_service_level(fixture, expected, request): @pytest.mark.parametrize( - "fixture, expected", [("test_charger", "4.0.0"), ("test_charger_v2", "2.9.1")] + "fixture, expected", [("test_charger", "4.1.2"), ("test_charger_v2", "2.9.1")] ) async def test_get_wifi_firmware(fixture, expected, request): """Test v4 Status reply""" From e517edbbc6d12bbebf70469d772a1bc58935bc24 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 16 Mar 2022 11:20:00 -0700 Subject: [PATCH 2/6] tests: add test for set_current on old firmware --- tests/test_init.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_init.py b/tests/test_init.py index 080e613..65e9c23 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -650,3 +650,17 @@ async def test_set_current_error(test_charger, mock_aioclient, caplog): with pytest.raises(ValueError): await test_charger.set_current(60) assert "Invalid value for max_current_soft: 60" in caplog.text + + +async def test_set_current_v2(test_charger_v2, mock_aioclient, caplog): + """Test v4 Status reply""" + await test_charger_v2.update() + value = {"cmd": "OK", "ret": "$OK^20"} + mock_aioclient.post( + TEST_URL_RAPI, + status=200, + body=json.dumps(value), + ) + with caplog.at_level(logging.DEBUG): + await test_charger_v2.set_current(12) + assert "Setting current via RAPI" in caplog.text \ No newline at end of file From 2eef9790e68691a81f5cf666d3693c498b4cb055 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 16 Mar 2022 11:25:46 -0700 Subject: [PATCH 3/6] tests: formatting --- tests/test_init.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_init.py b/tests/test_init.py index 65e9c23..d66f310 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -663,4 +663,4 @@ async def test_set_current_v2(test_charger_v2, mock_aioclient, caplog): ) with caplog.at_level(logging.DEBUG): await test_charger_v2.set_current(12) - assert "Setting current via RAPI" in caplog.text \ No newline at end of file + assert "Setting current via RAPI" in caplog.text From 2843f7e6000cdb382f65b77bcd6afe145491be70 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 16 Mar 2022 12:29:51 -0700 Subject: [PATCH 4/6] fix: convert amps to int in set_current (#41) --- openevsehttp/__init__.py | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/openevsehttp/__init__.py b/openevsehttp/__init__.py index 0775a14..a24cb9d 100644 --- a/openevsehttp/__init__.py +++ b/openevsehttp/__init__.py @@ -446,7 +446,7 @@ async def set_current(self, amps: int = 6) -> None: """Set the soft current limit.""" # 3.x - 4.1.0: use RAPI commands $SC # 4.1.2: use HTTP API call - + amps = int(amps) cutoff = AwesomeVersion("4.1.2") current = AwesomeVersion(self._config["version"]) diff --git a/setup.py b/setup.py index eae9ca8..9d03380 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ PROJECT_DIR = Path(__file__).parent.resolve() README_FILE = PROJECT_DIR / "README.md" -VERSION = "0.1.17" +VERSION = "0.1.18" setup( name="python-openevse-http", From 0de75d7c567a0b76deb388256b040bd48076f409 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 16 Mar 2022 13:32:33 -0700 Subject: [PATCH 5/6] fix: adjust post parameters --- openevsehttp/__init__.py | 6 ++++-- setup.py | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/openevsehttp/__init__.py b/openevsehttp/__init__.py index a24cb9d..612921d 100644 --- a/openevsehttp/__init__.py +++ b/openevsehttp/__init__.py @@ -197,7 +197,8 @@ async def process_request( async with aiohttp.ClientSession() as session: http_method = getattr(session, method) - async with http_method(url, data=data, auth=auth) as resp: + _LOGGER.debug("Connecting to %s with data payload of %s using method %s", url, data, method) + async with http_method(url, json=data, auth=auth) as resp: try: message = await resp.json() except TimeoutError: @@ -206,7 +207,7 @@ async def process_request( message = {"msg": resp} if resp.status == 400: - _LOGGER.error("%s", message["msg"]) + _LOGGER.error("Error 400: %s", message["msg"]) raise ParseJSONError if resp.status == 401: error = await resp.text() @@ -459,6 +460,7 @@ async def set_current(self, amps: int = 6) -> None: ): _LOGGER.error("Invalid value for max_current_soft: %s", amps) raise ValueError + data = {"max_current_soft": amps} _LOGGER.debug("Setting max_current_soft to %s", amps) diff --git a/setup.py b/setup.py index 9d03380..0ace299 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ PROJECT_DIR = Path(__file__).parent.resolve() README_FILE = PROJECT_DIR / "README.md" -VERSION = "0.1.18" +VERSION = "0.1.19" setup( name="python-openevse-http", From e7eb6077147d86313fc6c6b42ad932b5141f9122 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 16 Mar 2022 13:37:00 -0700 Subject: [PATCH 6/6] formatting --- openevsehttp/__init__.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/openevsehttp/__init__.py b/openevsehttp/__init__.py index 612921d..af64ecf 100644 --- a/openevsehttp/__init__.py +++ b/openevsehttp/__init__.py @@ -197,7 +197,12 @@ async def process_request( async with aiohttp.ClientSession() as session: http_method = getattr(session, method) - _LOGGER.debug("Connecting to %s with data payload of %s using method %s", url, data, method) + _LOGGER.debug( + "Connecting to %s with data payload of %s using method %s", + url, + data, + method, + ) async with http_method(url, json=data, auth=auth) as resp: try: message = await resp.json()