parameters: (
          self, payload, command_type=CMD_SEND_KEY, key_press_delay: float = 0, press=False
      )
return_statement: return ret_val
docstring: Send a key to the tv and handles exceptions.
docstring_summary: Send a key to the tv and handles exceptions.
docstring_tokens: Send a key to the tv and handles exceptions .
function: def send_command(
          self, payload, command_type=CMD_SEND_KEY, key_press_delay: float = 0, press=False
      ):
          """Send a key to the tv and handles exceptions."""
          if key_press_delay < 0:
              key_press_delay = None  # means "default" provided with constructor
  
          ret_val = False
          try:
              if command_type == CMD_RUN_APP:
                  ret_val = self._ws.run_app(payload)
              elif command_type == CMD_RUN_APP_REMOTE:
                  app_cmd = payload.split(",")
                  app_id = app_cmd[0]
                  action_type = ""
                  if len(app_cmd) > 1:
                      action_type = app_cmd[1]
                  ret_val = self._ws.run_app(app_id, action_type, "", use_remote=True)
              elif command_type == CMD_RUN_APP_REST:
                  result = self._ws.rest_app_run(payload)
                  _LOGGER.debug("Rest API result launching app %s: %s", payload, result)
                  ret_val = True
              elif command_type == CMD_OPEN_BROWSER:
                  ret_val = self._ws.open_browser(payload)
              elif command_type == CMD_SEND_TEXT:
                  ret_val = self._ws.send_text(payload)
              elif command_type == CMD_SEND_KEY:
                  hold_delay = 0
                  source_keys = payload.split(",")
                  key_code = source_keys[0]
                  if len(source_keys) > 1:
  
                      def get_hold_time():
                          hold_time = source_keys[1].replace(" ", "")
                          if not hold_time:
                              return 0
                          if not hold_time.isdigit():
                              return 0
                          hold_time = int(hold_time)/1000
                          return min(hold_time, KEYHOLD_MAX_DELAY)
  
                      hold_delay = get_hold_time()
  
                  if hold_delay > 0:
                      ret_val = self._ws.hold_key(key_code, hold_delay)
                  else:
                      ret_val = self._ws.send_key(
                          key_code, key_press_delay, "Press" if press else "Click"
                      )
              else:
                  _LOGGER.debug("Send command: invalid command type -> %s", command_type)
  
          except (ConnectionResetError, AttributeError, BrokenPipeError):
              _LOGGER.debug(
                  "Error in send_command() -> ConnectionResetError/AttributeError/BrokenPipeError"
                      )
  
          except WebSocketTimeoutException:
              _LOGGER.debug(
                  "Failed sending payload %s command_type %s",
                  payload,
                  command_type,
                  exc_info=True,
              )
  
          except OSError:
              _LOGGER.debug("Error in send_command() -> OSError")
  
          return ret_val
function_tokens: def send_command ( self , payload , command_type = CMD_SEND_KEY , key_press_delay : float = 0 , press = False ) : if key_press_delay < 0 : key_press_delay = None # means "default" provided with constructor ret_val = False try : if command_type == CMD_RUN_APP : ret_val = self . _ws . run_app ( payload ) elif command_type == CMD_RUN_APP_REMOTE : app_cmd = payload . split ( "," ) app_id = app_cmd [ 0 ] action_type = "" if len ( app_cmd ) > 1 : action_type = app_cmd [ 1 ] ret_val = self . _ws . run_app ( app_id , action_type , "" , use_remote = True ) elif command_type == CMD_RUN_APP_REST : result = self . _ws . rest_app_run ( payload ) _LOGGER . debug ( "Rest API result launching app %s: %s" , payload , result ) ret_val = True elif command_type == CMD_OPEN_BROWSER : ret_val = self . _ws . open_browser ( payload ) elif command_type == CMD_SEND_TEXT : ret_val = self . _ws . send_text ( payload ) elif command_type == CMD_SEND_KEY : hold_delay = 0 source_keys = payload . split ( "," ) key_code = source_keys [ 0 ] if len ( source_keys ) > 1 : def get_hold_time ( ) : hold_time = source_keys [ 1 ] . replace ( " " , "" ) if not hold_time : return 0 if not hold_time . isdigit ( ) : return 0 hold_time = int ( hold_time ) / 1000 return min ( hold_time , KEYHOLD_MAX_DELAY ) hold_delay = get_hold_time ( ) if hold_delay > 0 : ret_val = self . _ws . hold_key ( key_code , hold_delay ) else : ret_val = self . _ws . send_key ( key_code , key_press_delay , "Press" if press else "Click" ) else : _LOGGER . debug ( "Send command: invalid command type -> %s" , command_type ) except ( ConnectionResetError , AttributeError , BrokenPipeError ) : _LOGGER . debug ( "Error in send_command() -> ConnectionResetError/AttributeError/BrokenPipeError" ) except WebSocketTimeoutException : _LOGGER . debug ( "Failed sending payload %s command_type %s" , payload , command_type , exc_info = True , ) except OSError : _LOGGER . debug ( "Error in send_command() -> OSError" ) return ret_val