Skip to content

Commit

Permalink
Fix deadlock.
Browse files Browse the repository at this point in the history
Fix deadlock that occurs when system encounteres old fuel consumption data and improved test for this.

Resolves: denpamusic/homeassistant-plum-ecomax#45
  • Loading branch information
denpamusic committed Feb 12, 2024
1 parent d568568 commit 79970c3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
9 changes: 5 additions & 4 deletions pyplumio/devices/ecomax.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,12 @@ async def _add_burned_fuel_counter(self, fuel_consumption: float) -> None:
"Skipping outdated fuel consumption data, was %i seconds old",
time_passed_ns / 1000000000,
)
return
else:
await self.dispatch(
ATTR_FUEL_BURNED,
fuel_consumption * time_passed_ns / (3600 * 1000000000),
)

await self.dispatch(
ATTR_FUEL_BURNED, fuel_consumption * time_passed_ns / (3600 * 1000000000)
)
self._fuel_burned_timestamp_ns = current_timestamp_ns

async def _handle_mixer_parameters(
Expand Down
9 changes: 8 additions & 1 deletion tests/test_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ async def test_unknown_ecomax_parameter(ecomax: EcoMAX, caplog) -> None:

@patch(
"time.perf_counter_ns",
side_effect=(0, 10 * 1000000000, 600 * 1000000000, 610 * 1000000000),
side_effect=(0, 10 * 1000000000, 310 * 1000000000, 320 * 1000000000),
)
async def test_fuel_consumption_callbacks(mock_time, caplog) -> None:
"""Test callbacks dispatched on fuel consumption."""
Expand All @@ -279,7 +279,14 @@ async def test_fuel_consumption_callbacks(mock_time, caplog) -> None:
assert fuel_burned == 0.01
ecomax.handle_frame(Response(data={ATTR_FUEL_CONSUMPTION: 1}))
await ecomax.wait_until_done()
fuel_burned = await ecomax.get(ATTR_FUEL_BURNED)
assert "Skipping outdated fuel consumption" in caplog.text
caplog.clear()
ecomax.handle_frame(Response(data={ATTR_FUEL_CONSUMPTION: 7.2}))
await ecomax.wait_until_done()
assert "Skipping outdated fuel consumption" not in caplog.text
fuel_burned = await ecomax.get(ATTR_FUEL_BURNED)
assert fuel_burned == 0.02


async def test_regdata_callbacks(ecomax: EcoMAX) -> None:
Expand Down

0 comments on commit 79970c3

Please sign in to comment.