I’m glad others have had success sticking things in their jacks to fix this issue—that’s worked for me a couple times but the jack still gets stuck eventually even though I never insert a headphone plug. Finally I came across a real solution at [https://forums.macrumors.com/threads/macbook-pro-optical-out-wont-turn-off.239287/page-4|this thread]. It’s a very iFixit-style fix, as you should definitely do it with at least a boardview for your computer and only if you’re confident you know what you’re doing, but if done right it’s ''actually the perfect solution.''
Since what I propose below is a permanent, hardware-level fix, also look earlier in the thread I linked to find dondavanzo’s software solution. It didn’t work for me, but if it works for you it’s easier, more elegant, and reversible, so worth a shot before going further. The VoodooHDA kext for hackintoshes can force the audio codec to ignore the presence of an optical plug and give you normal behavior switching only between headphones and speakers.
By removing a resistor (or inductor, depending on your exact model - my comment over on the macrumors thread specifies) from the circuit that detects as SPDIF optical plug, you completely disable the digital output. So, you can still use the headphone jack without worrying about the switch getting stuck, and unplugging headphones will seamlessly switch back to internal speakers. I triple and quadruple-checked to make sure I had found the proper resistor (R6805 on A1286 models with board 820-3330-B; my educated guess without having an A1502 model in front of me is either L6607 or L6608), then it took less than a minute to scrape off the tiny SMD resistor with an exacto knife. Obviously, if you ever need to use SPDIF, this won’t work; but, if you use SPDIF, you probably know what you’re doing and can figure something else out.