There's another topic about it, but the solution is pretty far down. Credit to AsanteRevell for posting a universal solution that worked for me and should work for everything. Note that this does actually make companions immune to death in any form, including death blows from you. They still go down on one knee to recover, and they will still leave your service if you keep attacking them.
Get the first name of your companion. For this example, I'm gonna use my companion Aranea Ienith. Simply type the following into the console:
help Aranea 4
Replace Aranea with the name of your companion, like Lydia for instance (help Lydia 4). Once you've typed that, there should be a bunch of stuff that pops up, or a little, depending on the NPC name. The relevant bit is where it says "NPC_: (00028AD0) 'Aranea Ienith'. Once you have this code , simply type
setessential 00028AD0 1
^Do not forget the '1' at the end, and do not forget to use the code for your specific companion instead of 00028AD0. This method has worked for me, tested at least 4 times spamming dual cast spells at her in god mode. Without it, she goes down to one knee, then dies as long as I deal enough damage to finish her off. With this, she simply goes down to one knee, and can't recover until I stop hitting her. She will leave my service, start crawling away, impaled several times over with shards of ice, but she will not die.
Note: Some, if not many, companion NPCs will have multiple codes that pop up when you type in their help code. For instance, a friend of mine wanted the solution to do this with his companion Cicero, but that brought up 3 different NPC codes. As I don't have this companion, I don't know which one works, but my guess is that one of the 3 codes that pop up for him will work. Just make a save and try a code, then surprise him by trying to kill him 3 times or until he refuses to die.
K have fun. If anyone knows how to simply turn off friendly fire on companions, that'd be great too. I assume it's possible due to the fact that the "cloak" line of spells (i.e. flame, frost, and lightning cloak) don't deal damage to friendlies in general. --- "When life gives you lemons, shoot them back at life at high velocity." - Asura Advice Column
Maybe put an abbreviated version in the codes section or a wiki? --- My FC -> 4212 3513 8759 Sareph: The gray fox was wrong this whole time. Why cover your face with a mask when you can cover their face with a bucket?