Help needed for a little complex multifunctional pet macro for my lock

Just pulling Command Demon out of your spellbook and putting it on your bars will do all that with the priorities you listed. Assuming you have auto self cast enabled in the Interface > Combat options.

As for the focus version.

#showtooltip
/cast [@focus,exists,nodead] [] Command Demon

If you want mouseover support.

#showtooltip
/cast [@mouseover,exists,nodead] [] Command Demon

If you wanna go fancy