Skip to content

Commit bbc9e26

Browse files
Fix MQTT status parsing: handle integer CurrentStatus
MQTT printers send CurrentStatus as an integer (e.g., 1) while WebSocket printers send it as a list (e.g., [1]). Changes: - Updated ElegooMachineStatus.from_int() to wrap integer in list and call from_list() - Updated PrinterStatus.__init__() to detect format and call appropriate method - Cleaner separation: from_int wraps and delegates to from_list This fixes the TypeError: object of type 'int' has no len() error that was preventing status updates from MQTT printers.
1 parent f37625a commit bbc9e26

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

custom_components/elegoo_printer/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
"documentation": "https://github.com/danielcherubini/elegoo-homeassistant",
88
"iot_class": "local_polling",
99
"issue_tracker": "https://github.com/danielcherubini/elegoo-homeassistant/issues",
10-
"version": "2.4.0-beta.13"
10+
"version": "2.4.0-beta.14"
1111
}

custom_components/elegoo_printer/sdcp/models/enums.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,26 +80,28 @@ class ElegooMachineStatus(Enum):
8080
@classmethod
8181
def from_int(cls, status_int: int) -> "ElegooMachineStatus | None":
8282
"""
83-
Converts an integer to an ElegooMachineStatus enum member.
83+
Convert a single integer to an ElegooMachineStatus enum member.
84+
85+
MQTT printers send CurrentStatus as an integer. This method wraps
86+
it in a list and calls from_list().
8487
8588
Arguments:
86-
status_int: The integer representing the print status.
89+
status_int: An integer representing the machine status.
8790
8891
Returns:
89-
The corresponding ElegooMachineStatus enum member, or None if the
90-
integer is not a valid status value.
92+
The corresponding ElegooMachineStatus enum member, or None if
93+
the integer is not a valid status value.
9194
92-
""" # noqa: D401
93-
try:
94-
return cls(status_int) # Use cls() to create enum members
95-
except ValueError:
96-
return None
95+
"""
96+
return cls.from_list([status_int])
9797

9898
@classmethod
9999
def from_list(cls, status_list: list[int]) -> "ElegooMachineStatus | None":
100100
"""
101101
Convert a list of integers to an ElegooMachineStatus enum member.
102102
103+
WebSocket printers send CurrentStatus as a list like [1].
104+
103105
Arguments:
104106
status_list: A list of integers representing print statuses.
105107
It is expected to contain only one element.
@@ -115,7 +117,10 @@ def from_list(cls, status_list: list[int]) -> "ElegooMachineStatus | None":
115117
return None # Return None if the list is empty or has more than one element
116118

117119
status_int = status_list[0]
118-
return cls.from_int(status_int)
120+
try:
121+
return cls(status_int)
122+
except ValueError:
123+
return None
119124

120125

121126
class ElegooPrintStatus(Enum):

custom_components/elegoo_printer/sdcp/models/status.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,16 @@ def __init__(
199199
# Support both legacy Saturn (nested) and flat formats
200200
status = data.get("Status", data)
201201

202-
current_status_list = status.get("CurrentStatus", [])
203-
self.current_status: ElegooMachineStatus | None = ElegooMachineStatus.from_list(
204-
current_status_list
205-
)
202+
current_status_data = status.get("CurrentStatus", [])
203+
# MQTT printers send CurrentStatus as int, WebSocket as list
204+
if isinstance(current_status_data, int):
205+
self.current_status: ElegooMachineStatus | None = (
206+
ElegooMachineStatus.from_int(current_status_data)
207+
)
208+
else:
209+
self.current_status: ElegooMachineStatus | None = (
210+
ElegooMachineStatus.from_list(current_status_data)
211+
)
206212

207213
# Generic Status
208214
self.previous_status: int = status.get("PreviousStatus", 0)

0 commit comments

Comments
 (0)