From 2a1ec7e1c4604b8ca39e68666e380e96f6c15613 Mon Sep 17 00:00:00 2001 From: "firstof9@gmail.com" Date: Thu, 14 Nov 2024 13:39:09 -0700 Subject: [PATCH 1/7] feat: add support to control led brightness --- openevsehttp/__main__.py | 65 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/openevsehttp/__main__.py b/openevsehttp/__main__.py index 21438d9..921f8a1 100644 --- a/openevsehttp/__main__.py +++ b/openevsehttp/__main__.py @@ -827,6 +827,29 @@ async def list_claims(self) -> Any: response = await self.process_request(url=url, method="get") # noqa: E501 return response + async def set_led_brightness(self, level: int) -> None: + """Set LED brightness level.""" + if not self._version_check("4.1.0"): + _LOGGER.debug("Feature not supported for older firmware.") + raise UnsupportedFeature + + url = f"{self.url}config" + data: dict[str, Any] = {} + + data["led_brightness"] = level + _LOGGER.debug("Setting LED brightness to %s", level) + response = await self.process_request( + url=url, method="post", data=data + ) # noqa: E501 + return response + + @property + def led_brightness(self) -> str: + """Return charger led_brightness.""" + assert self._config is not None + return self._config["led_brightness"] + + @property def hostname(self) -> str: """Return charger hostname.""" @@ -1280,9 +1303,51 @@ def max_amps(self) -> int: return self._config["max_current_hard"] return MAX_AMPS + @property def mqtt_connected(self) -> bool: """Return the status of the mqtt connection.""" if self._status is not None and "mqtt_connected" in self._status: return self._status["mqtt_connected"] return False + + + @property + def emoncms_connected(self) -> bool: + """Return the status of the emoncms connection.""" + if self._status is not None and "emoncms_connected" in self._status: + return self._status["emoncms_connected"] + return False + + @property + def ocpp_connected(self) -> bool: + """Return the status of the ocpp connection.""" + if self._status is not None and "ocpp_connected" in self._status: + return self._status["ocpp_connected"] + return False + + @property + def uptime(self) -> int | None: + """Return the unit uptime.""" + if self._status is not None and "uptime" in self._status: + return self._status["uptime"] + return None + + @property + def freeram(self) -> int | None: + """Return the unit uptime.""" + if self._status is not None and "freeram" in self._status: + return self._status["freeram"] + return None + + # Safety counts + @property + def checks_count(self) -> dict | None: + """Return the saftey checks counts.""" + if self._status is not None and ["gfcicount", "nogndcount", "stuckcount"] in self._status: + counts = {} + counts["gfcicount"] = self._status["gfcicount"] + counts["nogndcount"] = self._status["nogndcount"] + counts["stuckcount"] = self._status["stuckcount"] + return counts + return None From bc4453ccb632fb9a002a331366f96934486c6a55 Mon Sep 17 00:00:00 2001 From: "firstof9@gmail.com" Date: Thu, 14 Nov 2024 13:41:59 -0700 Subject: [PATCH 2/7] formatting --- openevsehttp/__main__.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/openevsehttp/__main__.py b/openevsehttp/__main__.py index 921f8a1..253baf2 100644 --- a/openevsehttp/__main__.py +++ b/openevsehttp/__main__.py @@ -832,7 +832,7 @@ async def set_led_brightness(self, level: int) -> None: if not self._version_check("4.1.0"): _LOGGER.debug("Feature not supported for older firmware.") raise UnsupportedFeature - + url = f"{self.url}config" data: dict[str, Any] = {} @@ -842,13 +842,12 @@ async def set_led_brightness(self, level: int) -> None: url=url, method="post", data=data ) # noqa: E501 return response - + @property def led_brightness(self) -> str: """Return charger led_brightness.""" assert self._config is not None - return self._config["led_brightness"] - + return self._config["led_brightness"] @property def hostname(self) -> str: @@ -1303,48 +1302,49 @@ def max_amps(self) -> int: return self._config["max_current_hard"] return MAX_AMPS - @property def mqtt_connected(self) -> bool: """Return the status of the mqtt connection.""" if self._status is not None and "mqtt_connected" in self._status: return self._status["mqtt_connected"] return False - @property def emoncms_connected(self) -> bool: """Return the status of the emoncms connection.""" if self._status is not None and "emoncms_connected" in self._status: return self._status["emoncms_connected"] - return False - + return False + @property def ocpp_connected(self) -> bool: """Return the status of the ocpp connection.""" if self._status is not None and "ocpp_connected" in self._status: return self._status["ocpp_connected"] - return False - + return False + @property def uptime(self) -> int | None: """Return the unit uptime.""" if self._status is not None and "uptime" in self._status: return self._status["uptime"] - return None - + return None + @property def freeram(self) -> int | None: """Return the unit uptime.""" if self._status is not None and "freeram" in self._status: return self._status["freeram"] - return None + return None # Safety counts @property def checks_count(self) -> dict | None: """Return the saftey checks counts.""" - if self._status is not None and ["gfcicount", "nogndcount", "stuckcount"] in self._status: + if ( + self._status is not None + and ["gfcicount", "nogndcount", "stuckcount"] in self._status + ): counts = {} counts["gfcicount"] = self._status["gfcicount"] counts["nogndcount"] = self._status["nogndcount"] From c2b8eec1ce9cdcbd17efa208596bcd275f2bf5bb Mon Sep 17 00:00:00 2001 From: "firstof9@gmail.com" Date: Thu, 14 Nov 2024 13:44:12 -0700 Subject: [PATCH 3/7] remove return value --- openevsehttp/__main__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/openevsehttp/__main__.py b/openevsehttp/__main__.py index 253baf2..fe05241 100644 --- a/openevsehttp/__main__.py +++ b/openevsehttp/__main__.py @@ -841,7 +841,6 @@ async def set_led_brightness(self, level: int) -> None: response = await self.process_request( url=url, method="post", data=data ) # noqa: E501 - return response @property def led_brightness(self) -> str: From 71ef79ce8aae518438d9421b9ea8f0f06a4047c4 Mon Sep 17 00:00:00 2001 From: "firstof9@gmail.com" Date: Thu, 14 Nov 2024 14:55:13 -0700 Subject: [PATCH 4/7] linting --- openevsehttp/__main__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openevsehttp/__main__.py b/openevsehttp/__main__.py index fe05241..f64a05f 100644 --- a/openevsehttp/__main__.py +++ b/openevsehttp/__main__.py @@ -838,7 +838,7 @@ async def set_led_brightness(self, level: int) -> None: data["led_brightness"] = level _LOGGER.debug("Setting LED brightness to %s", level) - response = await self.process_request( + await self.process_request( url=url, method="post", data=data ) # noqa: E501 From 115889a50baaeb6b0799e0ae66c02a3d9954274f Mon Sep 17 00:00:00 2001 From: "firstof9@gmail.com" Date: Thu, 14 Nov 2024 14:58:54 -0700 Subject: [PATCH 5/7] formatting --- openevsehttp/__main__.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/openevsehttp/__main__.py b/openevsehttp/__main__.py index f64a05f..d318bcf 100644 --- a/openevsehttp/__main__.py +++ b/openevsehttp/__main__.py @@ -838,9 +838,7 @@ async def set_led_brightness(self, level: int) -> None: data["led_brightness"] = level _LOGGER.debug("Setting LED brightness to %s", level) - await self.process_request( - url=url, method="post", data=data - ) # noqa: E501 + await self.process_request(url=url, method="post", data=data) # noqa: E501 @property def led_brightness(self) -> str: From 8b2f82ef21db6af4898c7bc11a2a11a1f658061f Mon Sep 17 00:00:00 2001 From: "firstof9@gmail.com" Date: Thu, 14 Nov 2024 15:24:50 -0700 Subject: [PATCH 6/7] update tests --- openevsehttp/__main__.py | 3 +++ tests/test_main.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/openevsehttp/__main__.py b/openevsehttp/__main__.py index 63af4ce..6c78990 100644 --- a/openevsehttp/__main__.py +++ b/openevsehttp/__main__.py @@ -843,6 +843,9 @@ async def set_led_brightness(self, level: int) -> None: @property def led_brightness(self) -> str: """Return charger led_brightness.""" + if not self._version_check("4.1.0"): + _LOGGER.debug("Feature not supported for older firmware.") + raise UnsupportedFeature assert self._config is not None return self._config["led_brightness"] diff --git a/tests/test_main.py b/tests/test_main.py index 48e5510..7656236 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1834,3 +1834,35 @@ async def test_checks_count(fixture, expected, request): await charger.update() status = charger.checks_count assert status == expected + + +async def test_led_brightness(test_charger_new, test_charger_v2, caplog): + """Test led_brightness reply.""" + await test_charger_new.update() + status = test_charger_new.led_brightness + assert status == 125 + + await test_charger_v2.update() + with pytest.raises(UnsupportedFeature): + with caplog.at_level(logging.DEBUG): + status = await test_charger_v2.led_brightness + assert "Feature not supported for older firmware." in caplog.text + +async def test_set_led_brightness(test_charger_new, test_charger_v2, mock_aioclient, caplog): + """Test set_led_brightness reply.""" + await test_charger_new.update() + value = '{"msg": "OK"}' + mock_aioclient.post( + TEST_URL_CONFIG, + status=200, + body=value, + ) + with caplog.at_level(logging.DEBUG): + await test_charger_new.set_led_brightness(255) + assert "Setting LED brightness to 255" in caplog.text + + await test_charger_v2.update() + with pytest.raises(UnsupportedFeature): + with caplog.at_level(logging.DEBUG): + await test_charger_v2.set_led_brightness(255) + assert "Feature not supported for older firmware." in caplog.text \ No newline at end of file From ee6f799862c02c68e0a8e7d92c5b0b69f45b74e8 Mon Sep 17 00:00:00 2001 From: "firstof9@gmail.com" Date: Thu, 14 Nov 2024 15:26:42 -0700 Subject: [PATCH 7/7] formatting --- openevsehttp/__main__.py | 2 +- tests/test_main.py | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/openevsehttp/__main__.py b/openevsehttp/__main__.py index 6c78990..3ddf4c5 100644 --- a/openevsehttp/__main__.py +++ b/openevsehttp/__main__.py @@ -845,7 +845,7 @@ def led_brightness(self) -> str: """Return charger led_brightness.""" if not self._version_check("4.1.0"): _LOGGER.debug("Feature not supported for older firmware.") - raise UnsupportedFeature + raise UnsupportedFeature assert self._config is not None return self._config["led_brightness"] diff --git a/tests/test_main.py b/tests/test_main.py index 7656236..7583bd2 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1848,7 +1848,10 @@ async def test_led_brightness(test_charger_new, test_charger_v2, caplog): status = await test_charger_v2.led_brightness assert "Feature not supported for older firmware." in caplog.text -async def test_set_led_brightness(test_charger_new, test_charger_v2, mock_aioclient, caplog): + +async def test_set_led_brightness( + test_charger_new, test_charger_v2, mock_aioclient, caplog +): """Test set_led_brightness reply.""" await test_charger_new.update() value = '{"msg": "OK"}' @@ -1856,7 +1859,7 @@ async def test_set_led_brightness(test_charger_new, test_charger_v2, mock_aiocli TEST_URL_CONFIG, status=200, body=value, - ) + ) with caplog.at_level(logging.DEBUG): await test_charger_new.set_led_brightness(255) assert "Setting LED brightness to 255" in caplog.text @@ -1865,4 +1868,4 @@ async def test_set_led_brightness(test_charger_new, test_charger_v2, mock_aiocli with pytest.raises(UnsupportedFeature): with caplog.at_level(logging.DEBUG): await test_charger_v2.set_led_brightness(255) - assert "Feature not supported for older firmware." in caplog.text \ No newline at end of file + assert "Feature not supported for older firmware." in caplog.text