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
25 changes: 24 additions & 1 deletion pyprusalink/types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from enum import Enum
from typing import TypedDict
from typing import NotRequired, TypedDict

"""Types of the v1 API. Source: https://github.com/prusa3d/Prusa-Link-Web/blob/master/spec/openapi.yaml"""

Expand Down Expand Up @@ -95,10 +95,33 @@ class PrinterStatusInfo(TypedDict):
status_connect: StatusInfo | None


class StatusJob(TypedDict, total=False):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could argue using NotRequired on all the fields is a bit more consistent, but then again the API spec says all fields are optional for StatusJob, which this global flag captures nicely,

I am fine as is.

"""Job summary embedded in the status response.

All fields are optional per the OpenAPI spec.
"""

id: int
progress: float
time_printing: int
time_remaining: int


class StatusStorage(TypedDict):
"""Active storage device embedded in the status response."""

path: str
name: str
read_only: bool
Comment thread
heikkih marked this conversation as resolved.
free_space: NotRequired[int]


class PrinterStatus(TypedDict):
"""Printer status."""

printer: PrinterStatusInfo
job: NotRequired[StatusJob]
storage: NotRequired[StatusStorage]

Comment thread
heikkih marked this conversation as resolved.

class PrintFileRefs(TypedDict):
Expand Down
49 changes: 48 additions & 1 deletion tests/test_prusalink.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,60 @@ async def test_get_status(pl, respx_mock):
"speed": 100,
"fan_hotend": 1200,
"fan_print": 4500,
}
},
"job": {
"id": 42,
"progress": 55.0,
"time_printing": 1200,
"time_remaining": 980,
},
"storage": {
"path": "/usb/",
"name": "usb",
"read_only": False,
"free_space": 4202335,
},
},
)
)
result = await pl.get_status()
assert result["printer"]["state"] == "PRINTING"
assert result["printer"]["temp_nozzle"] == 214.9
assert result["job"]["id"] == 42
assert result["job"]["time_remaining"] == 980
assert result["storage"]["name"] == "usb"
assert result["storage"]["free_space"] == 4202335


async def test_get_status_idle(pl, respx_mock):
"""In IDLE state the job key is absent from the response."""
respx_mock.get(f"{HOST}/api/v1/status").mock(
return_value=httpx.Response(
200,
json={
"printer": {
"state": "IDLE",
"temp_nozzle": 27.6,
"target_nozzle": 0.0,
"temp_bed": 24.3,
"target_bed": 0.0,
"axis_z": 0.0,
"flow": 100,
"speed": 100,
"fan_hotend": 0,
"fan_print": 0,
},
"storage": {
"path": "/usb/",
"name": "usb",
"read_only": False,
},
},
)
)
result = await pl.get_status()
assert result["printer"]["state"] == "IDLE"
assert "job" not in result


async def test_get_job(pl, respx_mock):
Expand Down