diff --git a/mpf/devices/drop_target.py b/mpf/devices/drop_target.py index 6ada558b5..d71d463bf 100644 --- a/mpf/devices/drop_target.py +++ b/mpf/devices/drop_target.py @@ -52,7 +52,7 @@ async def _initialize(self): # can't read the switch until the switch controller is set up self.machine.events.add_handler('init_phase_4', - self._update_state_from_switch, priority=2) + self._update_state_from_switch, priority=2, reconcile=True) self.machine.events.add_handler('init_phase_4', self._register_switch_handlers, priority=1) @@ -405,20 +405,24 @@ def reset(self, attempt=None): if self.reset_coil: coils.add(self.reset_coil) + # now pulse the coils + self.debug_log('Pulsing reset coils: %s', coils) + restore_delay_ms = 0 + for coil in coils: + wait_ms = coil.pulse(max_wait_ms=self.config['reset_coil_max_wait_ms']) + self.debug_log("Coil %s firing has a wait of %s!", coil, wait_ms) + restore_delay_ms += wait_ms + if self.config['ignore_switch_ms']: + restore_delay_ms += self.config['ignore_switch_ms'] if self.config['max_reset_attempts'] and attempt is None: attempt = 1 self._ignore_switch_hits = True - self.delay.add(ms=self.config['ignore_switch_ms'], + self.delay.add(ms=restore_delay_ms, callback=self._restore_switch_hits, name='ignore_hits', reset_attempt=attempt) - # now pulse the coils - self.debug_log('Pulsing reset coils: %s', coils) - for coil in coils: - coil.pulse(max_wait_ms=self.config['reset_coil_max_wait_ms']) - def _restore_switch_hits(self, reset_attempt=None): self._ignore_switch_hits = False self.member_target_change() diff --git a/mpf/tests/test_DropTargets.py b/mpf/tests/test_DropTargets.py index b6e09e5ed..7e84c3fc4 100644 --- a/mpf/tests/test_DropTargets.py +++ b/mpf/tests/test_DropTargets.py @@ -1,5 +1,5 @@ from mpf.tests.MpfFakeGameTestCase import MpfFakeGameTestCase -from unittest.mock import MagicMock +from unittest.mock import MagicMock, patch from mpf.tests.MpfTestCase import MpfTestCase @@ -43,7 +43,7 @@ def test_drop_target_bank(self): self.assertIn('left3', self.machine.drop_targets) self.assertIn('left_bank', self.machine.drop_target_banks) - self.machine.coils["coil1"].pulse = MagicMock() + self.machine.coils["coil1"].pulse = MagicMock(return_value=200) self.assertFalse(self.machine.drop_targets["left1"].complete) self.assertFalse(self.machine.drop_targets["left2"].complete) @@ -82,7 +82,7 @@ def test_drop_target_bank(self): self.assertFalse(self.machine.drop_target_banks["left_bank"].complete) # check that the bank does not reset if already down - self.machine.coils["coil1"].pulse = MagicMock() + self.machine.coils["coil1"].pulse = MagicMock(return_value=100) self.machine.drop_target_banks['left_bank'].reset() assert not self.machine.coils["coil1"].pulse.called @@ -132,7 +132,7 @@ def test_drop_targets_in_mode(self): self.machine.modes['mode1'].start() self.advance_time_and_run() - self.machine.coils["coil2"].pulse = MagicMock() + self.machine.coils["coil2"].pulse = MagicMock(return_value=30) self.assertFalse(self.machine.drop_targets["left4"].complete) self.assertFalse(self.machine.drop_targets["left5"].complete) @@ -335,7 +335,7 @@ def test_drop_target_ignore_ms_ball_search(self): # wait until ball search phase 1 event_future = self.machine.events.wait_for_event("ball_search_phase_1") self.machine.clock.loop.run_until_complete(event_future) - + self.advance_time_and_run(.25) self.hit_switch_and_run('switch10', .1) @@ -352,7 +352,7 @@ def test_drop_target_ignore_ms_ball_search(self): # wait until ball search phase 2 event_future = self.machine.events.wait_for_event("ball_search_phase_2") self.machine.clock.loop.run_until_complete(event_future) - + self.advance_time_and_run(.25) self.hit_switch_and_run('switch10', .1) @@ -369,7 +369,7 @@ def test_drop_target_ignore_ms_ball_search(self): # wait until ball search phase 3 event_future = self.machine.events.wait_for_event("ball_search_phase_3") self.machine.clock.loop.run_until_complete(event_future) - + self.advance_time_and_run(.25) self.hit_switch_and_run('switch10', .1) @@ -415,6 +415,20 @@ def test_drop_target_bank_ignore_ms(self): # up should have been called by now self.assertEventCalled('drop_target_bank_right_bank_up') + def test_drop_target_bank_restore_delay_ms(self): + # Set a specific ms that the pulse will wait before firing + self.machine.coils['coil5'].pulse = MagicMock(return_value=27) + with patch('mpf.core.delays.DelayManager.add') as add: + self.hit_switch_and_run('switch8', 1) + self.hit_switch_and_run('switch9', 1) + + self.post_event('reset_right_bank', 1.5) + bank = self.machine.drop_target_banks['right_bank'] + # Verify that the ignore_ms is the config value (1000) plus the wait + add.assert_called_with(ms=1027, name='ignore_hits', + callback=bank._restore_switch_hits, + reset_attempt=None) + class TestDropTargetsInGame(MpfFakeGameTestCase):