diff --git a/openevsehttp/__main__.py b/openevsehttp/__main__.py index ca5ae52..3a99c1a 100644 --- a/openevsehttp/__main__.py +++ b/openevsehttp/__main__.py @@ -581,6 +581,25 @@ def _version_check(self, min_version: str, max_version: str = "") -> bool: _LOGGER.debug("Non-semver firmware version detected.") return False + # Self production HTTP Posting + + async def self_production(self, grid: int, solar: int, invert: bool = True) -> None: + """Send pushed sensor data to self-prodcution.""" + if not self._version_check("4.0.0"): + _LOGGER.debug("Feature not supported for older firmware.") + raise UnsupportedFeature + + # Invert the sensor -import/+export + if invert: + grid = grid * -1 + + url = f"{self.url}status" + data = {"solar": solar, "grid": grid} + + _LOGGER.debug("Posting self-production: %s", data) + response = await self.process_request(url=url, method="post", data=data) + _LOGGER.debug("Self-production response: %s", response) + @property def hostname(self) -> str: """Return charger hostname.""" diff --git a/tests/test_main.py b/tests/test_main.py index dcb0558..59897f7 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1501,3 +1501,24 @@ async def test_get_state_raw(fixture, expected, request): await charger.update() status = charger.mqtt_connected assert status == expected + + +async def test_self_production(test_charger, test_charger_v2, mock_aioclient, caplog): + """Test self_production function.""" + await test_charger.update() + mock_aioclient.post( + TEST_URL_STATUS, + status=200, + body='{"grid_ie": 3000, "solar": 1000}', + ) + with caplog.at_level(logging.DEBUG): + await test_charger.self_production(-3000, 1000) + assert "Posting self-production: {'solar': 1000, 'grid': 3000}" in caplog.text + assert ( + "Self-production response: {'grid_ie': 3000, 'solar': 1000}" in caplog.text + ) + + with pytest.raises(UnsupportedFeature): + with caplog.at_level(logging.DEBUG): + await test_charger_v2.self_production(-3000, 1000) + assert "Feature not supported for older firmware." in caplog.text diff --git a/tox.ini b/tox.ini index 9c5b4dd..22d1d6c 100644 --- a/tox.ini +++ b/tox.ini @@ -32,3 +32,6 @@ commands = mypy openevsehttp deps = -rrequirements_lint.txt + +[flake8] +max-line-length = 88 \ No newline at end of file