How to detect when an ap_periph device is connected in a script

Is there a way to dynamically detect whether a ap_periph device is connected to the autopilot? I’d like to use a lua script to trigger certain behaviors in my autopilot when my ap_periph device is connected and go back to default behavior if it is not, but I haven’t found a very robust or elegant way of accomplishing this.