@@ -327,32 +327,26 @@ def _commandkey(self, key: str) -> str:
327327 key_suffix = key [4 :]
328328 return KEY_PASSWORD_PREFIX + key_action + self ._password_encoded + key_suffix
329329
330- def _writekey (self , key : str ) -> Any :
331- if self ._helper is None :
332- return
330+ def _writekey (self , key : str ) -> bool :
333331 _LOGGER .debug ("Prepare to send" )
332+ if self ._helper is None or self .getState () == "disc" :
333+ return False
334334 try :
335335 hand = self .getCharacteristics (uuid = _sb_uuid ("tx" ))[0 ]
336336 _LOGGER .debug ("Sending command, %s" , key )
337- write_result = hand .write (bytes .fromhex (key ), withResponse = False )
337+ hand .write (bytes .fromhex (key ), withResponse = False )
338338 except bluepy .btle .BTLEException :
339- _LOGGER .warning (
340- "Error while enabling notifications on Switchbot" , exc_info = True
341- )
339+ _LOGGER .warning ("Error sending command to Switchbot" , exc_info = True )
342340 raise
343- if not write_result :
344- _LOGGER .error (
345- "Sent command but didn't get a response from Switchbot confirming command was sent."
346- " Please check the Switchbot"
347- )
348341 else :
349342 _LOGGER .info ("Successfully sent command to Switchbot (MAC: %s)" , self ._mac )
350- return write_result
351343
352- def _subscribe ( self ) -> None :
353- if self . _helper is None :
354- return
344+ return True
345+
346+ def _subscribe ( self ) -> bool :
355347 _LOGGER .debug ("Subscribe to notifications" )
348+ if self ._helper is None or self .getState () == "disc" :
349+ return False
356350 enable_notify_flag = b"\x01 \x00 " # standard gatt flag to enable notification
357351 try :
358352 handle = self .getCharacteristics (uuid = _sb_uuid ("rx" ))[0 ]
@@ -366,27 +360,33 @@ def _subscribe(self) -> None:
366360 )
367361 raise
368362
363+ return True
364+
369365 def _readkey (self ) -> bytes :
366+ _LOGGER .debug ("Prepare to read notification from switchbot" )
370367 if self ._helper is None :
371- return b''
372- _LOGGER .debug ("Prepare to read" )
368+ return b"\x00 "
373369 try :
374370 receive_handle = self .getCharacteristics (uuid = _sb_uuid ("rx" ))
375- if receive_handle :
376- for char in receive_handle :
377- read_result : bytes = char .read ()
378- return read_result
379- return b"\x00 "
380371 except bluepy .btle .BTLEException :
381372 _LOGGER .warning (
382373 "Error while reading notifications from Switchbot" , exc_info = True
383374 )
384- raise
375+ else :
376+ for char in receive_handle :
377+ read_result : bytes = char .read ()
378+ return read_result
379+
380+ # Could disconnect before reading response. Assume it worked as this is executed after issueing command.
381+ if self ._helper and self .getState () == "disc" :
382+ return b"\x01 "
383+
384+ return b"\x00 "
385385
386386 def _sendcommand (self , key : str , retry : int , timeout : int | None = None ) -> bytes :
387- send_success = False
388387 command = self ._commandkey (key )
389- notify_msg = b"\x00 "
388+ send_success = False
389+ notify_msg = None
390390 _LOGGER .debug ("Sending command to switchbot %s" , command )
391391
392392 if len (self ._mac .split (":" )) != 6 :
@@ -395,25 +395,35 @@ def _sendcommand(self, key: str, retry: int, timeout: int | None = None) -> byte
395395 with CONNECT_LOCK :
396396 try :
397397 self ._connect (retry , timeout )
398- self ._subscribe ()
399- send_success = self ._writekey (command )
400- notify_msg = self ._readkey ()
398+ send_success = self ._subscribe ()
401399 except bluepy .btle .BTLEException :
402- _LOGGER .warning ("Error talking to Switchbot" , exc_info = True )
400+ _LOGGER .warning ("Error connecting to Switchbot" , exc_info = True )
401+ else :
402+ try :
403+ send_success = self ._writekey (command )
404+ except bluepy .btle .BTLEException :
405+ _LOGGER .warning (
406+ "Error sending commands to Switchbot" , exc_info = True
407+ )
408+ else :
409+ notify_msg = self ._readkey ()
403410 finally :
404411 self .disconnect ()
405- if send_success :
412+
413+ print ("notify message" , notify_msg )
414+
415+ if notify_msg and send_success :
406416 if notify_msg == b"\x07 " :
407417 _LOGGER .error ("Password required" )
408418 elif notify_msg == b"\t " :
409419 _LOGGER .error ("Password incorrect" )
410-
411420 return notify_msg
421+
412422 if retry < 1 :
413423 _LOGGER .error (
414424 "Switchbot communication failed. Stopping trying" , exc_info = True
415425 )
416- return notify_msg
426+ return b" \x00 "
417427 _LOGGER .warning ("Cannot connect to Switchbot. Retrying (remaining: %d)" , retry )
418428 time .sleep (DEFAULT_RETRY_TIMEOUT )
419429 return self ._sendcommand (key , retry - 1 )
0 commit comments