First Occurrence of the Term
Several slang dictionaries date the use of pansy as a slang term for homosexual to the 1920s. From Eric Partridge, A Dictionary of Slang & Unconventional English, eighth edition (1984):
pansy, n. A very effeminate youth; a homosexual: from ca. 1925. Cf. Nancy (boy). Also pansy-boy: from ca. 1930; New Statesman and Nation, 15 Sep. 1934, concerning the fascist meeting in Hyde Park on 9 Sep., notes that there were, from the crowd, 'shouts about 'pansy-boys'".
With regard to Nancy, Partridge has this:
Nancy, Miss Nancy, Nancy boy. A catamite: (low) coll. C.19–20. Also as adj.: rare before C.20. Hugh Walpole, Vanessa, 1933, 'But he isn't one of those, you know. Not a bit nancy.'—2. Also, an effeminate man: C.19–20. Cf. molly [which goes back at least to 1879, with the same meaning].—3. (Only as nancy, N-.) The breech, esp. in ask my Nancy: low (perhaps orig. c[ant]): ca. 1810–1910. (Vaux.) Cf. ask mine arse!
John Ayto & John Simpson, The Oxford Dictionary of Modern Slang (1994) has these relevant entries:
pansy mainly derog. noun 1 An effeminate man; a male homosexual. Also pansy-boy. 1929–. [Citation from 1960 omitted.] adjective 2 Of a man: effeminate, homosexual. 1929–. [Citation from 1951 omitted.]
Robert Chapman & Barbara Kipfer, Dictionary of American Slang, third edition (1995) similarly gives a first occurrence date of 1929 for pansy. And Tony Thorne, The Dictionary of Contemporary Slang (1990) has this entry:
pansy n a male homosexual or an effeminate, effete, or weak male. A word first used in this context in the 1920s and well-established until the late 1960s. It survives mainly in the speech of the middle-aged and elderly.
The earliest Google Books match for pansy in the effeminate sense is from the Class of '09 notes in The Princeton Alumni Weekly (March 28, 1930), where it appears as an adjective:
They have installed a ping-pong table in the new addition of the Princeton Club of Ne York, and ardent devotees thus far are Larry Waterman, John Nutting, and your correspondent. We old codgers are right spry, considering our creaking joints and dimming eyesight. Incidentally, any of you birds who regard ping-pong as a pansy game are hereby challenged. We three valiants guarantee to put you in your place and alter your opinion.
Claude McKay, Home to Harlem (1928) uses the term pansies to refer to the female companions of dandies:
Brown girls rouged and painted like dark pansies. Brown flesh draped in colorful soft clothes. Brown lips full and pouted for sweet kissing. Brown breasts throbbing with love.
...
All round the den, luxuriating under the little colored lights, the dark dandies were loving up their pansies. Feet tickling feet under tables, tantalizing liquor-rich giggling, hands busy above.
...
She stopped more than usual at Jake's table. He gave her a half dollar. She danced a jagging jig before him that made the giggles rise like a wave in the room. The pansies stared and tightened their grip on their dandies. The dandies tightened their hold on themselves. They looked the favored Jake up and down. All those perfect struts for him. Yet he didn't seem aroused at all.
None of the slang dictionaries I consulted cite this usage of pansy, so it may have been McKay's own invention and not an instance of found slang. Still, it occurs at just about the time when the "homosexual or effeminate" sense of pansy was emerging.
-----------------------
Derivation of the Term
None of the reference works I've checked offers any discussion of how pansy came to be applied to effeminate or homosexual men beyond Merriam-Webster's listing it as a later definition drawn from the flower sense of pansy. The simplest explanation is essentially that a pansy is a delicate flower, and so (in the 1920s, at any rate) to call a man a delicate flower was to call him effeminate. To mix things up a bit, I suggest four additional theories, some more interesting than others, below.
Theory #1: Derived from the pansy as a 'love philtre'
T. F. Thiselton-Dyer The Folk-Lore of Plants (1889), has this brief comment on the pansy:
In very early times flowers were much in request as love-philtres, various allusions to which occur in the literature of most ages. Thus, in "A Midsummer Night's Dream," Oberon tells Puck to place a pansy on the eyes of Titania, in order that, on awaking, she may fall in love with the first object she encounters.
In A Midsummer Night's Dream, the flower is identified as "a little western flower" that maidens call "love-in-idleness," and Oberon instructs Puck,
Fetch me that flower--the herb I showed thee once;/The juice of it on sleeping eyelids laid/Will make a man, or woman, madly dote/Upon the next live creature that it sees.
Another term for the pansy was "heart's-ease." And in Hamlet, Ophelia (gone mad) says, "and there is pansies, that's for thoughts"—thereby identifying the source of the flower's name in the French pensée.
This theory suggests that a person intoxicated by pansy nectar is incapable of any discrimination in his or her amorous feeling, being compelled to love the "next live creature that it sees," whether a man with the head of an ass (in Titania's case) or someone of the lover's own gender. The strongest arguments against this theory are (1) Shakespeare doesn't explicitly connect his "little western flower" to the name pansy; and (2) there is no evidentiary link between the love-philtre sense here and the use of pansy as an epithet for homosexual.
*Theory #2: Derived from the older slang term 'Nancy' by rhyming slang or similarity
There isn't much to say about this theory except that Nancy is an older slang term with much the same meaning, and with a variant Nancy-boy that corresponds to pansy-boy. The negative here is that (as far as I know) no one has suggested such a derivation or recorded it in books of such terms.
Theory #3: Derived from the Pansy Series of books for girls*
In the late 1800s, a series of at least 50 books marketed by the publisher as being for both boys and girls but focusing on girls. An advertisement for "Pansy" books at the front of One Commonplace Day (1886) by Pansy [Isabella Alden] prefaces a list of the available volumes with this introduction:
"PANSY" BOOKS
Probably no living author has exerted an influence upon the American people at large, at all comparable with Pansy's. Thousands upon thousands of families read her books every week, and the effect in the direction of right feeling, right thinking, and right living is incalculable.
Edmund Lester Pearson, The Secret Book (1914) suggests an older boy's reaction to the Pansy books:
Its benefit [that is, the benefit of a subscription volume purchased by one's father "to help out some worthy person"] on one human boy, aged, say, about ten years, was impossible to discover. Of a certainty, that boy got his full share of books of a more interesting type at Christmas, but as these were invariably devoured from cover to cover, not later than the evening of Dec. 28th, of the same year, and as there had not been enough Christmases in his experience for him to accumulate a vast number of books, there were still reasons why he had to draw upon the common collection of the town. Some of his own private stock, moreover, had been given him five or six years earlier, and he now turned from them in loathing. They were distressingly juvenile. Some of them were the “Pansy” books—I forget exactly what that implies, but I can recall the scornful tone in which the title came to be pronounced.
The theory here would be that any boy of ten or older who continued to enjoy the wholesome girl-centric stories of the "Pansy" series must be an effeminate weakling—notwithstanding (and perhaps, in part, because of) the books' "effect in the direction of right feeling, right thinking, and right living." A child who was ten years old in the heyday of the Pansy books (1880–1900) would have been thirty-five to fifty-five years old in 1925, and might recall (as Pearson did in 1914) "the scornful tone in which ["Pansy books"] came to be pronounced."
Theory #4: Derived from 'pansy,' a popular shade of purple in the mid-1920s
The Clothier and Furnisher, volume 106 (1926) [combined snippets] notes a movement in the industry to encourage men to buy purple ties:
The flare for new colors on the part of the men, in the opinion of A. N. Lincoln, secretary of the division, is but a reflection of the color habits of women, for whom sewing silk manufacturers have issued 300 standard colors and sixty seasonal shades of which the newest is pansy. Manufacturers look for an improvement in the sale of golf suits and knickers in the Fall.
...
There have been reports of the flurry in the necktie market which may as well be nipped in the bud here and now as at any time. That is the report, occurring fairly frequently of late, that pansy should be a good color in neckwear this year. Simply because the streets have been full of women wearing this favored shade of purple is no reason to believe it will be a popular color for men, and there are a number of reasons why it should not be.
It is not, in the first place, a man's color, or one that can be expected to appeal to a man for his own wear. It is of the peculiar flamboyant, audacious type that is only seized upon by women ; moreover it has never been a really fashionable color among them. Best-dressed women of the country have never worn pansy, and they certainly will not begin at this late date; and they have excellent reasons for not joining the rush after this color. Their reasons are precisely those which will prevent it from being popular among men.
Color a Bit Too Prominent
The chief point against the color is its too great prominence. The women who set the style foresaw that what has happened would happen, and refused to wear it. What they predicted has happened: the brilliance of the color won it immediate popularity among many classes, and the streets were full of pansy dresses. Because of its prominence, it immediately made itself known, and was an old story before it was a new one. It can't be disguised because of its brilliance, and many once-proud possessors of pansy gowns are wishing now that they hadn't hadn't bought a color that dates itself so obviously. It is so prominent that it marks itself as behind the times just as plainly, and almost as quickly, as it once marked itself ahead.
Now in men's ties, the same thing would happen ; only, since pansy has already played out its welcome among the ladies, it is now an old story. Pansy ties produced right now, at the time of writing, might sell to a few not over-particular dressers ; but by the time the mills could turn the color out in quantity, perhaps by the time these words see print, pansy will be as dead as a door-nail.
But if not pansy, then what? Colors will still be bright ; but they will not be as brilliant as formerly. The light, bright red that has practically held fort alone lately will have to share the honors with several other colors. Red will be more popular in a little darker shade. Blues will also be good. Browns will follow, and some purples, but not pansy purples, will be seen.
This theory rests on the idea that when clothiers attempted to sell "pansy purple" neckwear to men, the only buyers were men who were attracted to a color of a "peculiar flamboyant, audacious type that is only seized upon by women." The timing of this sartorial episode is in line with the emergence of the slang usage of pansy.
Conclusion
I have no idea whether any of the suggested derivations is the actual one—or for that matter, whether more than one of them factored into the usage. Theories 3 and 4 have the advantage of being linked to largely forgotten historical influences, which makes them more appealing (to me), but that doesn't make either of them more likely than the others to be the correct explanation.