Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add self_production function #254

Merged
merged 4 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions openevsehttp/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
21 changes: 21 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ commands =
mypy openevsehttp
deps =
-rrequirements_lint.txt

[flake8]
max-line-length = 88