If a cop mistakenly knocks on my door instead of my neighbors, and I have an unlocked handgun in my night stand, you better believe it should be legal. You’re inviting a whole heap of infringements otherwise.
Your judgement would obviously be impaired if you staggered to the door drunk and threw it open for a cop. (And you would probably be wise to consider professional help.)