Skip to content

Commit

Permalink
modify pct calulation logic to account for missing filament usage (#232)
Browse files Browse the repository at this point in the history
* modify pct calulation logic to account for missing filament usage

* updated pct logic and tests

* typo
  • Loading branch information
marcolivierarsenault authored Nov 28, 2023
1 parent 1277d04 commit fc123d0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
19 changes: 14 additions & 5 deletions custom_components/moonraker/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,13 +517,22 @@ def calculate_pct_job(data) -> float:
print_expected_duration = data["estimated_time"]
filament_used = data["status"]["print_stats"]["filament_used"]
expected_filament = data["filament_total"]
if print_expected_duration == 0 or expected_filament == 0:
return 0
divider = 0
time_pct = 0
filament_pct = 0

if print_expected_duration != 0:
time_pct = data["status"]["display_status"]["progress"]
divider += 1

time_pct = data["status"]["display_status"]["progress"]
filament_pct = 1.0 * filament_used / expected_filament
if expected_filament != 0:
filament_pct = 1.0 * filament_used / expected_filament
divider += 1

if divider == 0:
return 0

return (time_pct + filament_pct) / 2
return (time_pct + filament_pct) / divider


def calculate_eta(data):
Expand Down
8 changes: 7 additions & 1 deletion tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,17 @@ async def test_calculate_pct_job(data_for_calculate_pct):

async def test_calculate_pct_job_no_time(data_for_calculate_pct):
data_for_calculate_pct["estimated_time"] = 0
assert calculate_pct_job(data_for_calculate_pct) == 0
assert calculate_pct_job(data_for_calculate_pct) == 0.5


async def test_calculate_pct_job_no_filament(data_for_calculate_pct):
data_for_calculate_pct["filament_total"] = 0
assert calculate_pct_job(data_for_calculate_pct) == 0.6


async def test_calculate_pct_job_no_filament_no_time(data_for_calculate_pct):
data_for_calculate_pct["filament_total"] = 0
data_for_calculate_pct["estimated_time"] = 0
assert calculate_pct_job(data_for_calculate_pct) == 0


Expand Down

0 comments on commit fc123d0

Please sign in to comment.