Skip to content

Commit

Permalink
fix: use existing override and limits when setting new ones (#406)
Browse files Browse the repository at this point in the history
* fix: use existing override and limits when setting new ones

* version bump
  • Loading branch information
firstof9 authored Feb 3, 2025
1 parent 7ce6777 commit c2ba639
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 10 deletions.
6 changes: 2 additions & 4 deletions openevsehttp/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ async def set_override(
raise UnsupportedFeature
url = f"{self.url}override"

data: dict[str, Any] = {}
data: dict[str, Any] = await self.get_override()

if state not in ["active", "disabled", None]:
_LOGGER.error("Invalid override state: %s", state)
Expand Down Expand Up @@ -644,7 +644,6 @@ def _version_check(self, min_version: str, max_version: str = "") -> bool:
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"):
Expand All @@ -665,7 +664,6 @@ async def grid_voltage(self, voltage: int | None = None) -> None:
_LOGGER.debug("Voltage posting response: %s", response)

# Self production HTTP Posting

async def self_production(
self,
grid: int | None = None,
Expand Down Expand Up @@ -743,7 +741,7 @@ async def set_limit(
raise UnsupportedFeature

url = f"{self.url}limit"
data: Dict[str, Any] = {}
data: Dict[str, Any] = await self.get_limit()
valid_types = ["time", "energy", "soc", "range"]

if limit_type not in valid_types:
Expand Down
76 changes: 70 additions & 6 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,19 @@ async def test_wifi_serial(fixture, expected, request):
async def test_set_current(test_charger, mock_aioclient, caplog):
"""Test v4 Status reply."""
await test_charger.update()
value = {
"state": "active",
"charge_current": 0,
"max_current": 0,
"energy_limit": 0,
"time_limit": 0,
"auto_release": True,
}
mock_aioclient.get(
TEST_URL_OVERRIDE,
status=200,
body=json.dumps(value),
)
mock_aioclient.post(
TEST_URL_OVERRIDE,
status=200,
Expand All @@ -875,6 +888,20 @@ async def test_set_current_error(
):
"""Test v4 Status reply."""
await test_charger.update()
value = {
"state": "active",
"charge_current": 0,
"max_current": 0,
"energy_limit": 0,
"time_limit": 0,
"auto_release": True,
}
mock_aioclient.get(
TEST_URL_OVERRIDE,
status=200,
body=json.dumps(value),
repeat=True,
)
mock_aioclient.post(
TEST_URL_OVERRIDE,
status=200,
Expand Down Expand Up @@ -913,6 +940,19 @@ async def test_set_current_v2(

await test_charger_dev.update()
value = {"msg": "OK"}
value = {
"state": "active",
"charge_current": 0,
"max_current": 0,
"energy_limit": 0,
"time_limit": 0,
"auto_release": True,
}
mock_aioclient.get(
TEST_URL_OVERRIDE,
status=200,
body=json.dumps(value),
)
mock_aioclient.post(
TEST_URL_OVERRIDE,
status=200,
Expand Down Expand Up @@ -1257,6 +1297,20 @@ async def test_set_override(
):
"""Test set override function."""
await test_charger.update()
value = {
"state": "active",
"charge_current": 0,
"max_current": 0,
"energy_limit": 0,
"time_limit": 0,
"auto_release": True,
}
mock_aioclient.get(
TEST_URL_OVERRIDE,
status=200,
body=json.dumps(value),
repeat=True,
)
mock_aioclient.post(
TEST_URL_OVERRIDE,
status=200,
Expand All @@ -1265,7 +1319,10 @@ async def test_set_override(
with caplog.at_level(logging.DEBUG):
status = await test_charger.set_override("active")
assert status == {"msg": "OK"}
assert "Override data: {'auto_release': True, 'state': 'active'}" in caplog.text
assert (
"Override data: {'state': 'active', 'charge_current': 0, 'max_current': 0, 'energy_limit': 0, 'time_limit': 0, 'auto_release': True}"
in caplog.text
)

mock_aioclient.post(
TEST_URL_OVERRIDE,
Expand All @@ -1274,7 +1331,7 @@ async def test_set_override(
)
status = await test_charger.set_override("active", 30)
assert (
"Override data: {'auto_release': True, 'state': 'active', 'charge_current': 30}"
"Override data: {'state': 'active', 'charge_current': 30, 'max_current': 0, 'energy_limit': 0, 'time_limit': 0, 'auto_release': True}"
in caplog.text
)
mock_aioclient.post(
Expand All @@ -1284,7 +1341,8 @@ async def test_set_override(
)
status = await test_charger.set_override(charge_current=30)
assert (
"Override data: {'auto_release': True, 'charge_current': 30}" in caplog.text
"Override data: {'state': 'active', 'charge_current': 30, 'max_current': 0, 'energy_limit': 0, 'time_limit': 0, 'auto_release': True}"
in caplog.text
)
mock_aioclient.post(
TEST_URL_OVERRIDE,
Expand All @@ -1293,7 +1351,7 @@ async def test_set_override(
)
status = await test_charger.set_override("active", 30, 32)
assert (
"Override data: {'auto_release': True, 'state': 'active', 'charge_current': 30, 'max_current': 32}"
"Override data: {'state': 'active', 'charge_current': 30, 'max_current': 32, 'energy_limit': 0, 'time_limit': 0, 'auto_release': True}"
in caplog.text
)
mock_aioclient.post(
Expand All @@ -1303,7 +1361,7 @@ async def test_set_override(
)
status = await test_charger.set_override("active", 30, 32, 2000)
assert (
"Override data: {'auto_release': True, 'state': 'active', 'charge_current': 30, 'max_current': 32, 'energy_limit': 2000}"
"Override data: {'state': 'active', 'charge_current': 30, 'max_current': 32, 'energy_limit': 2000, 'time_limit': 0, 'auto_release': True}"
in caplog.text
)
mock_aioclient.post(
Expand All @@ -1313,7 +1371,7 @@ async def test_set_override(
)
status = await test_charger.set_override("active", 30, 32, 2000, 5000)
assert (
"Override data: {'auto_release': True, 'state': 'active', 'charge_current': 30, 'max_current': 32, 'energy_limit': 2000, 'time_limit': 5000}"
"Override data: {'state': 'active', 'charge_current': 30, 'max_current': 32, 'energy_limit': 2000, 'time_limit': 5000, 'auto_release': True}"
in caplog.text
)

Expand Down Expand Up @@ -1697,6 +1755,12 @@ async def test_set_limit(
):
"""Test set limit."""
await test_charger_modified_ver.update()
mock_aioclient.get(
TEST_URL_LIMIT,
status=200,
body='{"type": "energy", "value": 10}',
repeat=True,
)
mock_aioclient.post(
TEST_URL_LIMIT,
status=200,
Expand Down

0 comments on commit c2ba639

Please sign in to comment.