@@ -66,7 +66,7 @@ def __init__(self):
6666
6767@dataclass
6868class BambuCloud :
69-
69+
7070 def __init__ (self , region : str , email : str , username : str , auth_token : str ):
7171 self ._region = region
7272 self ._email = email
@@ -164,7 +164,7 @@ def _post(self, urlenum: BambuUrl, json: str, headers={}, return400=False):
164164 raise NotImplementedError ()
165165
166166 self ._test_response (response , return400 )
167-
167+
168168 return response
169169
170170 def _get_authentication_token (self ) -> str :
@@ -184,25 +184,27 @@ def _get_authentication_token(self) -> str:
184184 if accessToken != '' :
185185 # We were provided the accessToken directly.
186186 return accessToken
187-
187+
188188 loginType = auth_json .get ("loginType" , None )
189189 if loginType is None :
190190 LOGGER .error (f"loginType not present" )
191191 LOGGER .error (f"Response not understood: '{ response .text } '" )
192192 return ValueError (0 ) # FIXME
193193 elif loginType == 'verifyCode' :
194194 LOGGER .debug (f"Received verifyCode response" )
195- raise EmailCodeRequiredError ()
195+ # raise EmailCodeRequiredError()
196+ return loginType
196197 elif loginType == 'tfa' :
197198 # Store the tfaKey for later use
198199 LOGGER .debug (f"Received tfa response" )
199200 self ._tfaKey = auth_json .get ("tfaKey" )
200- raise TfaCodeRequiredError ()
201+ # raise TfaCodeRequiredError()
202+ return loginType
201203 else :
202204 LOGGER .debug (f"Did not understand json. loginType = '{ loginType } '" )
203205 LOGGER .error (f"Response not understood: '{ response .text } '" )
204206 return ValueError (1 ) # FIXME
205-
207+
206208 def _get_email_verification_code (self ):
207209 # Send the verification code request
208210 data = {
@@ -228,7 +230,7 @@ def _get_authentication_token_with_verification_code(self, code) -> dict:
228230 LOGGER .debug ("Authentication successful." )
229231 LOGGER .debug (f"Response = '{ response .json ()} '" )
230232 elif status_code == 400 :
231- LOGGER .debug (f"Received response: { response .json ()} " )
233+ LOGGER .debug (f"Received response: { response .json ()} " )
232234 if response .json ()['code' ] == 1 :
233235 # Code has expired. Request a new one.
234236 self ._get_email_verification_code ()
@@ -241,7 +243,7 @@ def _get_authentication_token_with_verification_code(self, code) -> dict:
241243 raise ValueError (response .json ()['code' ])
242244
243245 return response .json ()['accessToken' ]
244-
246+
245247 def _get_authentication_token_with_2fa_code (self , code : str ) -> dict :
246248 LOGGER .debug ("Attempting to connect with provided 2FA code." )
247249
@@ -261,7 +263,7 @@ def _get_authentication_token_with_2fa_code(self, code: str) -> dict:
261263 #LOGGER.debug(f"token_from_tfa: {token_from_tfa}")
262264
263265 return token_from_tfa
264-
266+
265267 def _get_username_from_authentication_token (self ) -> str :
266268 LOGGER .debug ("Trying to get username from authentication token." )
267269 # User name is in 2nd portion of the auth token (delimited with periods)
@@ -301,7 +303,7 @@ def _get_username_from_authentication_token(self) -> str:
301303 LOGGER .debug (f"Unable to decode authToken to retrieve username. AuthToken = { self ._auth_token } " )
302304
303305 return username
304-
306+
305307 # Retrieves json description of devices in the form:
306308 # {
307309 # 'message': 'success',
@@ -340,7 +342,7 @@ def _get_username_from_authentication_token(self) -> str:
340342 # }
341343 # ]
342344 # }
343-
345+
344346 def test_authentication (self , region : str , email : str , username : str , auth_token : str ) -> bool :
345347 self ._region = region
346348 self ._email = email
@@ -358,18 +360,31 @@ def login(self, region: str, email: str, password: str) -> str:
358360 self ._password = password
359361
360362 result = self ._get_authentication_token ()
361- self ._auth_token = result
362- self ._username = self ._get_username_from_authentication_token ()
363-
363+ if result is None :
364+ LOGGER .error ("Unable to authenticate." )
365+ return None
366+ elif len (result ) < 20 :
367+ return result
368+ else :
369+ self ._auth_token = result
370+ self ._username = self ._get_username_from_authentication_token ()
371+ return 'success'
372+ # self._auth_token = result
373+ # self._username = self._get_username_from_authentication_token()
374+
364375 def login_with_verification_code (self , code : str ):
365376 result = self ._get_authentication_token_with_verification_code (code )
366377 self ._auth_token = result
367378 self ._username = self ._get_username_from_authentication_token ()
379+ if self ._auth_token != "" and self ._username != "" and self ._auth_token != None and self ._username != None :
380+ return "success"
368381
369382 def login_with_2fa_code (self , code : str ):
370383 result = self ._get_authentication_token_with_2fa_code (code )
371384 self ._auth_token = result
372385 self ._username = self ._get_username_from_authentication_token ()
386+ if self ._auth_token != "" and self ._username != "" and self ._auth_token != None and self ._username != None :
387+ return "success"
373388
374389 def get_device_list (self ) -> dict :
375390 LOGGER .debug ("Getting device list from Bambu Cloud" )
@@ -454,7 +469,7 @@ def get_slicer_settings(self) -> dict:
454469 return None
455470 LOGGER .debug ("Succeeded" )
456471 return response .json ()
457-
472+
458473 # The task list is of the following form with a 'hits' array with typical 20 entries.
459474 #
460475 # "total": 531,
@@ -569,15 +584,15 @@ def download(self, url: str) -> bytearray:
569584 @property
570585 def username (self ):
571586 return self ._username
572-
587+
573588 @property
574589 def auth_token (self ):
575590 return self ._auth_token
576-
591+
577592 @property
578593 def bambu_connected (self ) -> bool :
579594 return self ._auth_token != "" and self ._auth_token != None
580-
595+
581596 @property
582597 def cloud_mqtt_host (self ):
583598 return "cn.mqtt.bambulab.com" if self ._region == "China" else "us.mqtt.bambulab.com"
0 commit comments