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 grid voltage http posting function #305

Merged
merged 2 commits into from
Jan 2, 2024
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
29 changes: 28 additions & 1 deletion openevsehttp/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand All @@ -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.")
Expand Down
29 changes: 27 additions & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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