How to determine whether Kodi is in master mode from the command line

I currently have Kodi configured with several restricted sources that are only unlocked under master mode. This is to prevent my young kids from watching unsuitable films.

This generally works well, but I have to remember to switch master mode off after watching something from one of the restricted sources. Ideally this would happen automatically in the event that I forget. (A few weeks ago I forgot to exit master mode before I went to bed and when I came down in the morning my six year old son was watching Deadpool Confused)

Unfortunately this functionality isn’t built in. I’ve been trying to roll my own solution, and I settled on rebooting the machine at 4am every morning, because when Kodi starts up it’s always in “normal” mode (i.e. not in master mode.). This works, but I had to mess around with some CEC settings to stop the pi turning on my TV every morning when it rebooted, and now I’ve got weird intermittent CEC problems – e.g. my receiver sometimes turns off when switching to the pi.

I have found that I can exit master mode on the command line with this:

Code:
kodi-send --host=xxx.xxx.xxx.xxx --port=9777 --action="Mastermode"

If Kodi is in master mode, it drops back to normal mode. However, if Kodi isn’t in master mode this command brings up the keypad on the screen requesting a pin to enter master mode. This is undesirable because it’ll confuse the kids when they turn the telly on.

What I need is a way to determine whether Kodi is in master mode from the command line, then I can write a script to run the command above if (and only if) necessary. Does anyone know of a way to do this? Or an alternative way to accomplish my goal?