Skip to content

Commit

Permalink
add protection for dividing by zero
Browse files Browse the repository at this point in the history
  • Loading branch information
marcolivierarsenault committed Dec 5, 2023
1 parent 37a5265 commit f5a330f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions custom_components/moonraker/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,9 @@ def calculate_current_layer(data):
):
return data["status"]["print_stats"]["info"]["current_layer"]

if "layer_height" not in data or data["layer_height"] <= 0:
return 0

# layer = (current_z - first_layer_height) / layer_height + 1
return (
int(
Expand Down
29 changes: 29 additions & 0 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,35 @@ async def test_current_layer_calculated():
assert calculate_current_layer(data) == 42


async def test_current_layer_calculated_layer_height_0():
data = {
"status": {
"print_stats": {
"state": PRINTSTATES.PRINTING.value,
"filename": "TheUniverse.gcode",
},
"toolhead": {"position": [0, 0, 8.4]},
},
"first_layer_height": 0.2,
"layer_height": 0,
}
assert calculate_current_layer(data) == 0


async def test_current_layer_calculate_missing_layer_height():
data = {
"status": {
"print_stats": {
"state": PRINTSTATES.PRINTING.value,
"filename": "TheUniverse.gcode",
},
"toolhead": {"position": [0, 0, 8.4]},
},
"first_layer_height": 0.2,
}
assert calculate_current_layer(data) == 0


async def test_current_layer_calculated_partial_info():
data = {
"status": {
Expand Down

0 comments on commit f5a330f

Please sign in to comment.