From 1d14c27557cc0bef5b72dee2ec4588f63ff2250b Mon Sep 17 00:00:00 2001 From: Ryan howard Date: Mon, 23 Sep 2024 11:19:01 -0400 Subject: [PATCH] switch to context manager --- .../protocol_api/instrument_context.py | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/api/src/opentrons/protocol_api/instrument_context.py b/api/src/opentrons/protocol_api/instrument_context.py index 98bdc8c1fc6..a3ebf294b91 100644 --- a/api/src/opentrons/protocol_api/instrument_context.py +++ b/api/src/opentrons/protocol_api/instrument_context.py @@ -545,16 +545,12 @@ def mix( ), ): self.aspirate(volume, location, rate) - if self.api_version >= APIVersion(2, 20): - auto_presence = self.liquid_presence_detection - self.liquid_presence_detection = False - while repetitions - 1 > 0: - self.dispense(volume, rate=rate, **dispense_kwargs) - self.aspirate(volume, rate=rate) - repetitions -= 1 - self.dispense(volume, rate=rate) - if self.api_version >= APIVersion(2, 20): - self.liquid_presence_detection = auto_presence + with AutoProbeDisable(self): + while repetitions - 1 > 0: + self.dispense(volume, rate=rate, **dispense_kwargs) + self.aspirate(volume, rate=rate) + repetitions -= 1 + self.dispense(volume, rate=rate) return self @requires_version(2, 0) @@ -2201,6 +2197,22 @@ def _raise_if_configuration_not_supported_by_pipette( # SINGLE, QUADRANT and ALL are supported by all pipettes +class AutoProbeDisable: + """Use this class to temporarily disable automatic liquid presence detection.""" + + def __init__(self, instrument: InstrumentContext): + self.instrument = instrument + + def __enter__(self) -> None: + if self.instrument.api_version >= APIVersion(2, 20): + self.auto_presence = self.instrument.liquid_presence_detection + self.instrument.liquid_presence_detection = False + + def __exit__(self, *args: Any, **kwargs: Any) -> None: + if self.instrument.api_version >= APIVersion(2, 20): + self.instrument.liquid_presence_detection = self.auto_presence + + def _raise_if_has_end_or_front_right_or_back_left( style: NozzleLayout, end: Optional[str],