def add_switch_handler(self, switch_name, callback, state=1, ms=0,
                           return_info=False, callback_kwargs=None):
        """Register a handler to take action on a switch event.

        Args:
            switch_name: String name of the switch you're adding this handler
                for.
            callback: The method you want called when this switch handler fires.
            state: Integer of the state transition you want to callback to be
                triggered on. Default is 1 which means it's called when the
                switch goes from inactive to active, but you can also use 0
                which means your callback will be called when the switch becomes
                inactive
            ms: Integer. If you specify a 'ms' parameter, the handler won't be
                called until the witch is in that state for that many
                milliseconds (rounded up to the nearst machine timer tick).
            return_info: If True, the switch controller will pass the
                parameters of the switch handler as arguments to the callback,
                including switch_name, state, and ms. If False (default), it
                just calls the callback with no parameters.
            callback_kwargs: Additional kwargs that will be passed with the
                callback.

        You can mix & match entries for the same switch here.

        """
        if not callback_kwargs:
            callback_kwargs = dict()

        self.log.debug("Registering switch handler: %s, %s, state: %s, ms: %s"
                       ", info: %s, cb_kwargs: %s", switch_name, callback,
                       state, ms,
                       return_info, callback_kwargs)

        entry_val = {'ms': ms, 'callback': callback,
                     'return_info': return_info,
                     'callback_kwargs': callback_kwargs}
        entry_key = str(switch_name) + '-' + str(state)

        self.registered_switches[entry_key].append(entry_val)

        # If the switch handler that was just registered has a delay (i.e. ms>0,
        # then let's see if the switch is currently in the state that the
        # handler was registered for. If so, and if the switch has been in this
        # state for less time than the ms registered, then we need to add this
        # switch to our active_timed_switches list so this handler is called
        # when this switch's active time expires. (in other words, we're
        # catching delayed switches that were in progress when this handler was
        # registered.

        if ms:  # only do this for handlers that have delays
            if state == 1:
                if self.is_active(switch_name, 0) and (
                            self.ms_since_change(switch_name) < ms):
                    # figure out when this handler should fire based on the
                    # switch's original activation time.
                    key = (
                    time.time() + ((ms - self.ms_since_change(switch_name))
                                   / 1000.0))
                    value = {'switch_action': entry_key,
                             'callback': callback,
                             'switch_name': switch_name,
                             'state': state,
                             'ms': ms,
                             'removed': False,
                             'return_info': return_info,
                             'callback_kwargs': callback_kwargs}
                    self.active_timed_switches[key].append(value)
            elif state == 0:
                if self.is_inactive(switch_name, 0) and (
                            self.ms_since_change(switch_name) < ms):
                    key = (
                    time.time() + ((ms - self.ms_since_change(switch_name))
                                   / 1000.0))
                    value = {'switch_action': entry_key,
                             'callback': callback,
                             'switch_name': switch_name,
                             'state': state,
                             'ms': ms,
                             'removed': False,
                             'return_info': return_info,
                             'callback_kwargs': callback_kwargs}
                    self.active_timed_switches[key].append(value)

        # Return the args we used to setup this handler for easy removal later
        return {'switch_name': switch_name,
                'callback': callback,
                'state': state,
                'ms': ms}
dataset/ETHPy150Open missionpinball/mpf/mpf/system/switch_controller.py/SwitchController.add_switch_handler/original