Skip to content

Commit bd70903

Browse files
authored
feat: add self_production function (#254)
* feat: add self_production function * add test * fix test * more test
1 parent f7c9d03 commit bd70903

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

openevsehttp/__main__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,25 @@ def _version_check(self, min_version: str, max_version: str = "") -> bool:
581581
_LOGGER.debug("Non-semver firmware version detected.")
582582
return False
583583

584+
# Self production HTTP Posting
585+
586+
async def self_production(self, grid: int, solar: int, invert: bool = True) -> None:
587+
"""Send pushed sensor data to self-prodcution."""
588+
if not self._version_check("4.0.0"):
589+
_LOGGER.debug("Feature not supported for older firmware.")
590+
raise UnsupportedFeature
591+
592+
# Invert the sensor -import/+export
593+
if invert:
594+
grid = grid * -1
595+
596+
url = f"{self.url}status"
597+
data = {"solar": solar, "grid": grid}
598+
599+
_LOGGER.debug("Posting self-production: %s", data)
600+
response = await self.process_request(url=url, method="post", data=data)
601+
_LOGGER.debug("Self-production response: %s", response)
602+
584603
@property
585604
def hostname(self) -> str:
586605
"""Return charger hostname."""

tests/test_main.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,3 +1501,24 @@ async def test_get_state_raw(fixture, expected, request):
15011501
await charger.update()
15021502
status = charger.mqtt_connected
15031503
assert status == expected
1504+
1505+
1506+
async def test_self_production(test_charger, test_charger_v2, mock_aioclient, caplog):
1507+
"""Test self_production function."""
1508+
await test_charger.update()
1509+
mock_aioclient.post(
1510+
TEST_URL_STATUS,
1511+
status=200,
1512+
body='{"grid_ie": 3000, "solar": 1000}',
1513+
)
1514+
with caplog.at_level(logging.DEBUG):
1515+
await test_charger.self_production(-3000, 1000)
1516+
assert "Posting self-production: {'solar': 1000, 'grid': 3000}" in caplog.text
1517+
assert (
1518+
"Self-production response: {'grid_ie': 3000, 'solar': 1000}" in caplog.text
1519+
)
1520+
1521+
with pytest.raises(UnsupportedFeature):
1522+
with caplog.at_level(logging.DEBUG):
1523+
await test_charger_v2.self_production(-3000, 1000)
1524+
assert "Feature not supported for older firmware." in caplog.text

tox.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@ commands =
3232
mypy openevsehttp
3333
deps =
3434
-rrequirements_lint.txt
35+
36+
[flake8]
37+
max-line-length = 88

0 commit comments

Comments
 (0)