Take a look at ni_apply_state_adjust_rules() to see how the power state is adjusted based on various factors.