flashforge-python-api 1.6.0__py3-none-any.whl → 3.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- flashforge/__init__.py +337 -256
- flashforge/_removed.py +65 -0
- flashforge/api/__init__.py +12 -18
- flashforge/api/constants/__init__.py +11 -11
- flashforge/api/constants/commands.py +18 -15
- flashforge/api/constants/endpoints.py +11 -11
- flashforge/api/misc/__init__.py +8 -8
- flashforge/api/network/__init__.py +11 -11
- flashforge/api/network/utils.py +71 -71
- flashforge/backends/__init__.py +80 -0
- flashforge/backends/connect.py +275 -0
- flashforge/backends/creator5_backend.py +191 -0
- flashforge/backends/dual_api_backend.py +170 -0
- flashforge/backends/dual_api_models.py +145 -0
- flashforge/backends/http_core.py +431 -0
- flashforge/backends/http_printer_backend.py +254 -0
- flashforge/backends/legacy_backend.py +436 -0
- flashforge/backends/printer_backend.py +217 -0
- flashforge/backends/types.py +125 -0
- flashforge/capabilities/__init__.py +98 -0
- flashforge/capabilities/models.py +67 -0
- flashforge/capabilities/palettes/__init__.py +28 -0
- flashforge/capabilities/palettes/ad5x_palette.py +70 -0
- flashforge/capabilities/palettes/creator5_palette.py +78 -0
- flashforge/capabilities/palettes/palette.py +54 -0
- flashforge/{api/controls/creator5_palette.py → capabilities/palettes/palette_snap.py} +226 -258
- flashforge/capabilities/printer_capabilities.py +145 -0
- flashforge/capabilities/registry.py +125 -0
- flashforge/capabilities/resolve_model.py +61 -0
- flashforge/discovery/__init__.py +39 -39
- flashforge/discovery/discovery.py +730 -763
- flashforge/exceptions.py +90 -51
- flashforge/models/__init__.py +52 -55
- flashforge/models/machine_info.py +540 -543
- flashforge/{api/controls/info.py → models/machine_info_parser.py} +35 -242
- flashforge/models/responses.py +119 -254
- flashforge/status/__init__.py +43 -0
- flashforge/status/printer_snapshot.py +396 -0
- flashforge/tcp/__init__.py +55 -54
- flashforge/tcp/a3_client.py +427 -427
- flashforge/tcp/a4_client.py +304 -304
- flashforge/tcp/ff_client.py +609 -590
- flashforge/tcp/gcode/__init__.py +15 -13
- flashforge/tcp/gcode/a3_gcode_controller.py +109 -109
- flashforge/tcp/gcode/gcode_controller.py +292 -308
- flashforge/tcp/gcode/gcodes.py +81 -99
- flashforge/tcp/gcode/legacy_gcode_controller.py +56 -0
- flashforge/tcp/parsers/__init__.py +27 -27
- flashforge/tcp/parsers/endstop_status.py +248 -248
- flashforge/tcp/parsers/location_info.py +83 -83
- flashforge/tcp/parsers/print_status.py +180 -180
- flashforge/tcp/parsers/printer_info.py +146 -146
- flashforge/tcp/parsers/temp_info.py +218 -218
- flashforge/tcp/parsers/thumbnail_info.py +274 -274
- flashforge/tcp/tcp_client.py +52 -12
- flashforge/threemf/__init__.py +40 -40
- flashforge/threemf/parser.py +560 -560
- flashforge/threemf/warnings.py +58 -58
- {flashforge_python_api-1.6.0.dist-info → flashforge_python_api-3.0.0.dist-info}/METADATA +46 -47
- flashforge_python_api-3.0.0.dist-info/RECORD +69 -0
- {flashforge_python_api-1.6.0.dist-info → flashforge_python_api-3.0.0.dist-info}/WHEEL +1 -1
- {flashforge_python_api-1.6.0.dist-info → flashforge_python_api-3.0.0.dist-info}/licenses/LICENSE +21 -21
- flashforge/api/controls/__init__.py +0 -17
- flashforge/api/controls/control.py +0 -413
- flashforge/api/controls/files.py +0 -210
- flashforge/api/controls/job_control.py +0 -896
- flashforge/api/controls/temp_control.py +0 -309
- flashforge/client.py +0 -609
- flashforge_python_api-1.6.0.dist-info/RECORD +0 -52
- {flashforge_python_api-1.6.0.dist-info → flashforge_python_api-3.0.0.dist-info}/entry_points.txt +0 -0
|
@@ -1,51 +1,23 @@
|
|
|
1
1
|
"""
|
|
2
|
-
FlashForge Python API -
|
|
2
|
+
FlashForge Python API - MachineInfoParser
|
|
3
|
+
|
|
4
|
+
Transforms the printer's ``/detail`` payload into the structured :class:`FFMachineInfo`.
|
|
3
5
|
"""
|
|
4
6
|
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
5
9
|
import logging
|
|
6
|
-
import re
|
|
7
10
|
from datetime import datetime, timedelta
|
|
8
|
-
from typing import TYPE_CHECKING, Any
|
|
9
|
-
|
|
10
|
-
from ...exceptions import FlashForgeResponseError
|
|
11
|
-
from ...models.machine_info import FFMachineInfo, MachineState, Temperature
|
|
12
|
-
from ...models.responses import DetailResponse, FFPrinterDetail
|
|
13
|
-
from ..constants.endpoints import Endpoints
|
|
14
|
-
from ..misc.redaction import redact_model
|
|
15
|
-
from ..network.utils import json_from_response
|
|
16
11
|
|
|
17
|
-
|
|
18
|
-
|
|
12
|
+
from ..api.misc.redaction import redact_model
|
|
13
|
+
from ..capabilities.models import PrinterModelId
|
|
14
|
+
from ..capabilities.registry import get_model_spec
|
|
15
|
+
from ..capabilities.resolve_model import resolve_model
|
|
16
|
+
from .machine_info import FFMachineInfo, FFPrinterDetail, MachineState, Temperature
|
|
19
17
|
|
|
20
18
|
logger = logging.getLogger(__name__)
|
|
21
19
|
|
|
22
20
|
|
|
23
|
-
# Firmware-reported PIDs from FlashForge's /detail endpoint. These are stable
|
|
24
|
-
# identifiers set by firmware, unlike the user-mutable `name` field.
|
|
25
|
-
PID_5M = 35
|
|
26
|
-
PID_5M_PRO = 36
|
|
27
|
-
PID_AD5X = 38
|
|
28
|
-
PID_CREATOR5 = 40
|
|
29
|
-
PID_CREATOR5_PRO = 41
|
|
30
|
-
KNOWN_HTTP_PIDS = {
|
|
31
|
-
PID_5M,
|
|
32
|
-
PID_5M_PRO,
|
|
33
|
-
PID_AD5X,
|
|
34
|
-
PID_CREATOR5,
|
|
35
|
-
PID_CREATOR5_PRO,
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
# Immutable model display names keyed by firmware PID. Used as a fallback for
|
|
39
|
-
# `model` when the printer doesn't report the `model` field (older firmware).
|
|
40
|
-
PID_MODEL_NAMES: dict[int, str] = {
|
|
41
|
-
PID_5M: "Adventurer 5M",
|
|
42
|
-
PID_5M_PRO: "Adventurer 5M Pro",
|
|
43
|
-
PID_AD5X: "AD5X",
|
|
44
|
-
PID_CREATOR5: "Creator 5",
|
|
45
|
-
PID_CREATOR5_PRO: "Creator 5 Pro",
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
21
|
# The one state in which the firmware actually counts `estimatedTime` down.
|
|
50
22
|
# Outside it the field freezes at its last value while the wall clock keeps
|
|
51
23
|
# moving, so `now() + estimatedTime` walks forward one minute per minute instead
|
|
@@ -130,27 +102,15 @@ class MachineInfoParser:
|
|
|
130
102
|
pid = getattr(detail, "pid", None)
|
|
131
103
|
detail_model = getattr(detail, "model", None) or ""
|
|
132
104
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
#
|
|
143
|
-
# IMPORTANT: detect the Creator 5 family *first*. A user-set
|
|
144
|
-
# name like "Creator 5 Pro" contains "Pro", so a naive
|
|
145
|
-
# `"Pro" in name` check would mis-classify it as a 5M Pro.
|
|
146
|
-
is_creator5 = "Creator 5" in printer_name
|
|
147
|
-
is_creator5_pro = is_creator5 and bool(
|
|
148
|
-
re.search(r"Creator 5 Pro", detail_model or printer_name, re.IGNORECASE)
|
|
149
|
-
)
|
|
150
|
-
is_ad5x = (
|
|
151
|
-
printer_name.upper() == "AD5X" or has_material_station
|
|
152
|
-
) and not is_creator5
|
|
153
|
-
is_pro = "Pro" in printer_name and not is_ad5x and not is_creator5
|
|
105
|
+
# PID first (firmware-set), then the immutable factory model name, then
|
|
106
|
+
# the user-set name as a last resort.
|
|
107
|
+
model_id = resolve_model(
|
|
108
|
+
pid=pid,
|
|
109
|
+
model_name=detail_model or None,
|
|
110
|
+
name=printer_name or None,
|
|
111
|
+
has_material_station=has_material_station,
|
|
112
|
+
)
|
|
113
|
+
spec = get_model_spec(model_id)
|
|
154
114
|
|
|
155
115
|
# Per-tool temperatures. Creator 5 series report nozzleTemps[] /
|
|
156
116
|
# nozzleTargetTemps[]; single-nozzle models don't, so fall back to a
|
|
@@ -185,7 +145,7 @@ class MachineInfoParser:
|
|
|
185
145
|
getattr(detail, "camera_stream_url", "") or ""
|
|
186
146
|
)
|
|
187
147
|
has_lidar = getattr(detail, "lidar", None) == 1
|
|
188
|
-
has_door_sensor =
|
|
148
|
+
has_door_sensor = spec.door_sensor
|
|
189
149
|
# Not every Creator 5 has the heated chamber. FFPrinterDetail maps
|
|
190
150
|
# the firmware's "no sensor" sentinel (-108) to None, so an absent
|
|
191
151
|
# reading is the signal - never the model family, which is what left
|
|
@@ -195,7 +155,7 @@ class MachineInfoParser:
|
|
|
195
155
|
|
|
196
156
|
# Immutable model name resolution: prefer the firmware `model`
|
|
197
157
|
# field, then a PID-derived name, then the user-set name.
|
|
198
|
-
pid_model_name =
|
|
158
|
+
pid_model_name = spec.display_name if model_id != PrinterModelId.UNKNOWN else None
|
|
199
159
|
model_value = detail_model or pid_model_name or printer_name or ""
|
|
200
160
|
|
|
201
161
|
# Prefer the firmware-reported nozzle count; fall back to the parsed
|
|
@@ -239,10 +199,7 @@ class MachineInfoParser:
|
|
|
239
199
|
firmware_version=getattr(detail, "firmware_version", "") or "",
|
|
240
200
|
name=printer_name,
|
|
241
201
|
pid=pid,
|
|
242
|
-
|
|
243
|
-
is_ad5x=is_ad5x,
|
|
244
|
-
is_creator5=is_creator5,
|
|
245
|
-
is_creator5_pro=is_creator5_pro,
|
|
202
|
+
model_id=model_id,
|
|
246
203
|
model=model_value,
|
|
247
204
|
nozzle_size=getattr(detail, "nozzle_model", "") or "",
|
|
248
205
|
nozzle_count=nozzle_count,
|
|
@@ -344,11 +301,8 @@ class MachineInfoParser:
|
|
|
344
301
|
"heating": MachineState.HEATING,
|
|
345
302
|
"printing": MachineState.PRINTING,
|
|
346
303
|
"pausing": MachineState.PAUSING,
|
|
347
|
-
#
|
|
348
|
-
#
|
|
349
|
-
# firmware that reports one is not a reason to drop the other.
|
|
350
|
-
# Observed on pid 41, firmware 1.9.4, whenever the printer paused
|
|
351
|
-
# itself on a detected clog.
|
|
304
|
+
# Every model reports "pause" for a paused print. No firmware sends
|
|
305
|
+
# "paused"; it stays mapped because it costs nothing.
|
|
352
306
|
"pause": MachineState.PAUSED,
|
|
353
307
|
"paused": MachineState.PAUSED,
|
|
354
308
|
"cancel": MachineState.CANCELLED,
|
|
@@ -358,6 +312,17 @@ class MachineInfoParser:
|
|
|
358
312
|
# dedicated state would need consumers to add it to their option
|
|
359
313
|
# lists first.
|
|
360
314
|
"downloading": MachineState.BUSY,
|
|
315
|
+
# Short transitional states, mapped onto BUSY for the same reason.
|
|
316
|
+
# A cancel in progress (all models). Not CANCELLED, which consumers
|
|
317
|
+
# treat as a final state.
|
|
318
|
+
"canceling": MachineState.BUSY,
|
|
319
|
+
# Filament load or unload (Creator 5 series).
|
|
320
|
+
"loading": MachineState.BUSY,
|
|
321
|
+
# Cloud job preparation (5M / 5M Pro 5.x firmware, Creator 5 1.9.2
|
|
322
|
+
# and later).
|
|
323
|
+
"cloud_slicing": MachineState.BUSY,
|
|
324
|
+
"sending": MachineState.BUSY,
|
|
325
|
+
"unzipping": MachineState.BUSY,
|
|
361
326
|
}
|
|
362
327
|
|
|
363
328
|
if valid_status in state_mapping:
|
|
@@ -366,175 +331,3 @@ class MachineInfoParser:
|
|
|
366
331
|
if valid_status:
|
|
367
332
|
logger.warning("Unknown machine status received: %r", status)
|
|
368
333
|
return MachineState.UNKNOWN
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
class Info:
|
|
372
|
-
"""
|
|
373
|
-
Provides methods for retrieving various information and status details from the FlashForge 3D printer.
|
|
374
|
-
This includes general machine information, printing status, and raw detail responses.
|
|
375
|
-
"""
|
|
376
|
-
|
|
377
|
-
def __init__(self, client: "FlashForgeClient"):
|
|
378
|
-
"""
|
|
379
|
-
Creates an instance of the Info class.
|
|
380
|
-
|
|
381
|
-
Args:
|
|
382
|
-
client: The FlashForgeClient instance used for communication with the printer.
|
|
383
|
-
"""
|
|
384
|
-
self.client = client
|
|
385
|
-
|
|
386
|
-
async def get(self) -> FFMachineInfo | None:
|
|
387
|
-
"""
|
|
388
|
-
Retrieves comprehensive machine information, processed into the FFMachineInfo model.
|
|
389
|
-
This method fetches detailed data from the printer and transforms it.
|
|
390
|
-
|
|
391
|
-
Returns:
|
|
392
|
-
An FFMachineInfo object, or None if the printer could not be reached.
|
|
393
|
-
|
|
394
|
-
Raises:
|
|
395
|
-
FlashForgeResponseError: The printer answered but the payload could
|
|
396
|
-
not be understood. Distinct from a None return, which means the
|
|
397
|
-
request never got through.
|
|
398
|
-
"""
|
|
399
|
-
detail_response = await self.get_detail_response()
|
|
400
|
-
if detail_response is None or detail_response.detail is None:
|
|
401
|
-
return None
|
|
402
|
-
|
|
403
|
-
machine_info = MachineInfoParser.from_detail(detail_response.detail)
|
|
404
|
-
if machine_info is None:
|
|
405
|
-
# We validated the payload but could not build our own model from
|
|
406
|
-
# it - that is a bug on our side, not an unreachable printer, and
|
|
407
|
-
# reporting it as the latter is what ff-5mp-hass#18 was about.
|
|
408
|
-
raise FlashForgeResponseError(
|
|
409
|
-
"The /detail payload was read but could not be converted into machine info",
|
|
410
|
-
endpoint=Endpoints.DETAIL,
|
|
411
|
-
)
|
|
412
|
-
return machine_info
|
|
413
|
-
|
|
414
|
-
async def is_printing(self) -> bool:
|
|
415
|
-
"""
|
|
416
|
-
Checks if the printer is currently in the "printing" state.
|
|
417
|
-
|
|
418
|
-
These three convenience wrappers keep their documented "return the
|
|
419
|
-
fallback on any failure" contract, so a caller polling for a boolean is
|
|
420
|
-
not forced to handle an exception. The failure is still logged, and
|
|
421
|
-
callers that need to distinguish causes should use `get()` directly.
|
|
422
|
-
|
|
423
|
-
Returns:
|
|
424
|
-
True if the printer is printing, False otherwise or if status cannot be determined.
|
|
425
|
-
"""
|
|
426
|
-
info = await self._get_or_none()
|
|
427
|
-
return info.status == "printing" if info else False
|
|
428
|
-
|
|
429
|
-
async def get_status(self) -> str | None:
|
|
430
|
-
"""
|
|
431
|
-
Retrieves the raw status string of the printer (e.g., "ready", "printing", "error").
|
|
432
|
-
|
|
433
|
-
Returns:
|
|
434
|
-
The status string, or None if it cannot be determined.
|
|
435
|
-
"""
|
|
436
|
-
info = await self._get_or_none()
|
|
437
|
-
return info.status if info else None
|
|
438
|
-
|
|
439
|
-
async def get_machine_state(self) -> MachineState | None:
|
|
440
|
-
"""
|
|
441
|
-
Retrieves the machine state as a MachineState enum value.
|
|
442
|
-
|
|
443
|
-
Returns:
|
|
444
|
-
A MachineState enum value, or None if it cannot be determined.
|
|
445
|
-
"""
|
|
446
|
-
info = await self._get_or_none()
|
|
447
|
-
return info.machine_state if info else None
|
|
448
|
-
|
|
449
|
-
async def _get_or_none(self) -> FFMachineInfo | None:
|
|
450
|
-
"""Call `get()`, absorbing a response error into a None for the convenience wrappers."""
|
|
451
|
-
try:
|
|
452
|
-
return await self.get()
|
|
453
|
-
except FlashForgeResponseError as error:
|
|
454
|
-
logger.warning("Could not read machine info: %s", error)
|
|
455
|
-
return None
|
|
456
|
-
|
|
457
|
-
async def get_detail_raw(self) -> dict[str, Any] | None:
|
|
458
|
-
"""
|
|
459
|
-
Retrieves the /detail response as the plain decoded JSON dict, with no
|
|
460
|
-
model validation applied.
|
|
461
|
-
|
|
462
|
-
This exists so callers can inspect identity fields - above all `pid` -
|
|
463
|
-
without first having to successfully validate the ~50 unrelated fields
|
|
464
|
-
in the payload. A supported printer must never be rejected because of a
|
|
465
|
-
field that has nothing to do with whether it is supported (ff-5mp-hass#18).
|
|
466
|
-
|
|
467
|
-
Returns:
|
|
468
|
-
The decoded response body, or None if the printer could not be
|
|
469
|
-
reached or answered with a non-200 status.
|
|
470
|
-
|
|
471
|
-
Raises:
|
|
472
|
-
FlashForgeResponseError: The printer answered with a body that is
|
|
473
|
-
not decodable JSON.
|
|
474
|
-
"""
|
|
475
|
-
payload = {"serialNumber": self.client.serial_number, "checkCode": self.client.check_code}
|
|
476
|
-
|
|
477
|
-
try:
|
|
478
|
-
session = await self.client.get_http_session()
|
|
479
|
-
async with session.post(
|
|
480
|
-
self.client.get_endpoint(Endpoints.DETAIL),
|
|
481
|
-
json=payload,
|
|
482
|
-
headers={"Content-Type": "application/json"},
|
|
483
|
-
) as response:
|
|
484
|
-
if response.status != 200:
|
|
485
|
-
logger.warning("Non-200 status from the /detail endpoint: %s", response.status)
|
|
486
|
-
return None
|
|
487
|
-
data = await json_from_response(response)
|
|
488
|
-
except Exception as error:
|
|
489
|
-
logger.warning(
|
|
490
|
-
"Could not reach the printer's /detail endpoint. This is a transport "
|
|
491
|
-
"failure (unreachable printer, timeout, or refused connection), not a "
|
|
492
|
-
"payload the library failed to read. %s",
|
|
493
|
-
error,
|
|
494
|
-
)
|
|
495
|
-
return None
|
|
496
|
-
|
|
497
|
-
if not isinstance(data, dict):
|
|
498
|
-
raise FlashForgeResponseError(
|
|
499
|
-
f"The /detail endpoint returned {type(data).__name__}, expected a JSON object",
|
|
500
|
-
endpoint=Endpoints.DETAIL,
|
|
501
|
-
)
|
|
502
|
-
return data
|
|
503
|
-
|
|
504
|
-
async def get_detail_response(self) -> DetailResponse | None:
|
|
505
|
-
"""
|
|
506
|
-
Retrieves the validated detailed response from the printer's detail endpoint.
|
|
507
|
-
This contains a wealth of information about the printer's current state.
|
|
508
|
-
|
|
509
|
-
Returns:
|
|
510
|
-
A DetailResponse object containing the raw printer details, or None
|
|
511
|
-
if the printer could not be reached.
|
|
512
|
-
|
|
513
|
-
Raises:
|
|
514
|
-
FlashForgeResponseError: The printer answered, but the body did not
|
|
515
|
-
validate. Callers should surface this differently from a None
|
|
516
|
-
return: None means "we never got an answer", this means "the
|
|
517
|
-
answer was not one we could read", and only the second is
|
|
518
|
-
actionable as a bug report.
|
|
519
|
-
"""
|
|
520
|
-
data = await self.get_detail_raw()
|
|
521
|
-
if data is None:
|
|
522
|
-
return None
|
|
523
|
-
|
|
524
|
-
try:
|
|
525
|
-
return DetailResponse(**data)
|
|
526
|
-
except Exception as error:
|
|
527
|
-
logger.warning(
|
|
528
|
-
"The printer answered /detail with a payload that failed validation. This "
|
|
529
|
-
"is NOT a connectivity problem - please report it with the debug dump "
|
|
530
|
-
"below. %s",
|
|
531
|
-
error,
|
|
532
|
-
)
|
|
533
|
-
# Redacted: this dump carries the MAC, IP and cloud registration
|
|
534
|
-
# codes, and it exists to be pasted into a bug report.
|
|
535
|
-
logger.debug("Payload that failed validation: %s", redact_model(data))
|
|
536
|
-
raise FlashForgeResponseError(
|
|
537
|
-
"The printer's /detail response could not be validated",
|
|
538
|
-
endpoint=Endpoints.DETAIL,
|
|
539
|
-
cause=error,
|
|
540
|
-
) from error
|