Skip to content
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
4 changes: 3 additions & 1 deletion custom_components/goecharger_api2/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ async def set_pv_data(self, call: ServiceCall):
}
_LOGGER.debug(f"Service-set_pv_data(): set PV data: {payload}")
try:
resp = await coordinator.async_write_key(Tag.IDS.key, payload)
resp = await coordinator.async_write_key(Tag.IDS.key, payload, require_ack=call.return_response)
if call.return_response:
if not isinstance(resp, dict) or resp.get(Tag.IDS.key) != payload:
return {"error": "The charger did not acknowledge PV data", "date": str(datetime.datetime.now().time())}
return {
"success": "true",
"date": str(datetime.datetime.now().time()),
Expand Down
15 changes: 12 additions & 3 deletions tests/integration/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def svc(hass):
service_module._COORDINATOR_PER_CONFIG_ENTRY_ID_MAP.clear()
coordinator = SimpleNamespace(
intg_type="charger", mode="lan", name="test charger", _serial="123456", data={},
async_write_key=AsyncMock(return_value={Tag.IDS.key: True}))
async_write_key=AsyncMock(side_effect=lambda key, value, **kwargs: {key: value}))
service = GoeChargerApiV2Service(hass, SimpleNamespace(entry_id="entry"), coordinator)
yield service, coordinator
service_module._COORDINATOR_PER_CONFIG_ENTRY_ID_MAP.clear()
Expand All @@ -30,7 +30,7 @@ async def test_valid_values_are_written(svc):
result = await service.set_pv_data(_call(pgrid=-500, ppv=1500, pakku=200))
assert result["success"] == "true"
coordinator.async_write_key.assert_awaited_once_with(
Tag.IDS.key, {"pGrid": -500.0, "pPv": 1500.0, "pAkku": 200.0})
Tag.IDS.key, {"pGrid": -500.0, "pPv": 1500.0, "pAkku": 200.0}, require_ack=True)


@pytest.mark.parametrize("bad", [math.nan, math.inf, -math.inf])
Expand All @@ -46,4 +46,13 @@ async def test_non_finite_pv_and_battery_value_become_zero(svc, bad):
service, coordinator = svc
await service.set_pv_data(_call(pgrid=100, ppv=bad, pakku=bad))
coordinator.async_write_key.assert_awaited_once_with(
Tag.IDS.key, {"pGrid": 100.0, "pPv": 0.0, "pAkku": 0.0})
Tag.IDS.key, {"pGrid": 100.0, "pPv": 0.0, "pAkku": 0.0}, require_ack=True)


@pytest.mark.parametrize("reply", [{}, None, {"err": "rejected"}, {"ids": True}, {"ids": {"pGrid": 1}}])
async def test_pv_response_requires_matching_acknowledgement(svc, reply):
service, coordinator = svc
coordinator.async_write_key = AsyncMock(return_value=reply)
result = await service.set_pv_data(_call(pgrid=-500, ppv=1500, pakku=200))
assert "error" in result and "success" not in result
assert coordinator.async_write_key.call_args.kwargs == {"require_ack": True}
4 changes: 2 additions & 2 deletions tests/integration/test_services_and_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def _call(return_response=True, **data):

def _service(hass, mode="lan", car=2, **kwargs):
coordinator = make_coordinator(hass, data={Tag.CAR_CONNECTED.key: car}, mode=mode, **kwargs)
coordinator.async_write_key = AsyncMock(return_value={Tag.IDS.key: True})
coordinator.async_write_key = AsyncMock(return_value={Tag.IDS.key: {"pGrid": -500.0, "pPv": 0.0, "pAkku": 0.0}})
return GoeChargerApiV2Service(hass, coordinator._config_entry, coordinator), coordinator


Expand Down Expand Up @@ -100,7 +100,7 @@ async def test_missing_grid_power_without_wanted_answer_returns_nothing(hass):
async def test_successful_pv_data_returns_nothing_when_no_answer_is_wanted(hass):
service, coordinator = _service(hass)
assert await service.set_pv_data(_call(return_response=False, pgrid=-1, ppv=2, pakku=3)) is None
coordinator.async_write_key.assert_awaited_once_with(Tag.IDS.key, {"pGrid": -1.0, "pPv": 2.0, "pAkku": 3.0})
coordinator.async_write_key.assert_awaited_once_with(Tag.IDS.key, {"pGrid": -1.0, "pPv": 2.0, "pAkku": 3.0}, require_ack=False)


@pytest.mark.parametrize("error", [ValueError("down"), TypeError("odd")])
Expand Down
Loading