See how you can easily toggle any setting by checking its status first.
You start by doing a UI query on the toggler and check if it's on or off. Then you use that state to click it.
This way you can easily toggle a device and don't have to assume which state it is in before trying to do so.
In this example you can see how you can toggle the Adaptive Brightness setting by first learning what status it's in.