From 01e65c8f4fa37081bf0256dd8fa321df7fb58629 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 2 Jan 2024 08:46:37 -0700 Subject: [PATCH] feat: add grid voltage http posting function (#305) * feat: add grid voltage http posting function * linting --- openevsehttp/__main__.py | 29 ++++++++++++++++++++++++++++- tests/test_main.py | 29 +++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/openevsehttp/__main__.py b/openevsehttp/__main__.py index 58f6b4c..bdcb85e 100644 --- a/openevsehttp/__main__.py +++ b/openevsehttp/__main__.py @@ -608,10 +608,35 @@ def _version_check(self, min_version: str, max_version: str = "") -> bool: _LOGGER.debug("Non-semver firmware version detected.") return False + # HTTP Posting of grid voltage + + async def grid_voltage(self, voltage: int | None = None) -> None: + """Send pushed sensor data to grid voltage.""" + if not self._version_check("4.0.0"): + _LOGGER.debug("Feature not supported for older firmware.") + raise UnsupportedFeature + + url = f"{self.url}status" + data = {} + + if voltage is not None: + data[VOLTAGE] = voltage + + if not data: + _LOGGER.info("No sensor data to send to device.") + else: + _LOGGER.debug("Posting voltage: %s", data) + response = await self.process_request(url=url, method="post", data=data) + _LOGGER.debug("Voltage posting response: %s", response) + # Self production HTTP Posting async def self_production( - self, grid: int | None = None, solar: int | None = None, invert: bool = True + self, + grid: int | None = None, + solar: int | None = None, + invert: bool = True, + voltage: int | None = None, ) -> None: """Send pushed sensor data to self-prodcution.""" if not self._version_check("4.0.0"): @@ -630,6 +655,8 @@ async def self_production( data[GRID] = grid elif solar is not None: data[SOLAR] = solar + if voltage is not None: + data[VOLTAGE] = voltage if not data: _LOGGER.info("No sensor data to send to device.") diff --git a/tests/test_main.py b/tests/test_main.py index 29c2903..fa4b413 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1521,8 +1521,10 @@ async def test_self_production(test_charger, test_charger_v2, mock_aioclient, ca repeat=True, ) with caplog.at_level(logging.DEBUG): - await test_charger.self_production(-3000, 1000) - assert "Posting self-production: {'grid_ie': 3000}" in caplog.text + await test_charger.self_production(-3000, 1000, True, 210) + assert ( + "Posting self-production: {'grid_ie': 3000, 'voltage': 210}" in caplog.text + ) assert ( "Self-production response: {'grid_ie': 3000, 'solar': 1000}" in caplog.text ) @@ -1657,3 +1659,26 @@ async def test_clear_limit( with caplog.at_level(logging.DEBUG): await test_charger.clear_limit() assert "Feature not supported for older firmware." in caplog.text + + +async def test_voltage(test_charger, test_charger_v2, mock_aioclient, caplog): + """Test voltage function.""" + await test_charger.update() + mock_aioclient.post( + TEST_URL_STATUS, + status=200, + body='{"voltage": 210}', + repeat=True, + ) + with caplog.at_level(logging.DEBUG): + await test_charger.grid_voltage(210) + assert "Posting voltage: {'voltage': 210}" in caplog.text + assert "Voltage posting response: {'voltage': 210}" in caplog.text + + await test_charger.grid_voltage(None) + assert "No sensor data to send to device." in caplog.text + + with pytest.raises(UnsupportedFeature): + with caplog.at_level(logging.DEBUG): + await test_charger_v2.grid_voltage(210) + assert "Feature not supported for older firmware." in caplog.text