Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for empty mounts in provisioning script #12961

Merged
merged 3 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import os
import json
from dataclasses import dataclass
from typing import Optional
from opentrons_hardware.firmware_bindings.constants import PipetteName
from opentrons_hardware.instruments.pipettes.serials import serial_val_from_parts
Expand All @@ -20,36 +19,81 @@
RIGHT_PIPETTE_ENV_VAR_NAME = "RIGHT_OT3_PIPETTE_DEFINITION"


@dataclass
class OT3PipetteEnvVar:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't you just keep this a data class with defaults?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or keep it a dataclass and use a build classmethod

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stop making so much sense. I have no idea why I did it that way. I will go back and fix it.

If I remember the reason I did it that way, and the reason makes sense, I will drop a comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remembered my reasoning for doing it that way.
My reasoning was not good.

Here is the fix
262d21e

"""OT3 Pipette Environment Variable."""

pipette_name: PipetteName
pipette_model: int
pipette_serial_code: bytes
eeprom_file_path: str
NO_PIPETTE_NAME = "EMPTY"
NO_PIPETTE_MODEL = -1
NO_PIPETTE_SERIAL_CODE = ""

def __init__(
self,
pipette_name: str,
pipette_model: int,
pipette_serial_code: str,
eeprom_file_path: str,
) -> None:
"""Initialize."""
self.pipette_name = pipette_name
self.pipette_model = pipette_model
self.pipette_serial_code = pipette_serial_code
self.eeprom_file_path = eeprom_file_path

def is_no_pipette(self) -> bool:
"""Check if pipette is no pipette."""
no_pipette = (
self.pipette_name == self.NO_PIPETTE_NAME,
self.pipette_model == self.NO_PIPETTE_MODEL,
self.pipette_serial_code == self.NO_PIPETTE_SERIAL_CODE,
)
any_no_pipette_fields_have_been_set = any(no_pipette)
all_no_pipette_fields_are_not_set = not all(no_pipette)

if any_no_pipette_fields_have_been_set and all_no_pipette_fields_are_not_set:
raise ValueError(

Check warning on line 53 in hardware/opentrons_hardware/scripts/emulation_pipette_provision.py

View check run for this annotation

Codecov / codecov/patch

hardware/opentrons_hardware/scripts/emulation_pipette_provision.py#L53

Added line #L53 was not covered by tests
"\n".join(
(
"Invalid empty pipette definition. Expecting the following:",
f"pipette_name: {self.NO_PIPETTE_NAME}",
f"pipette_model: {self.NO_PIPETTE_MODEL}",
f"pipette_serial_code: {self.NO_PIPETTE_SERIAL_CODE}",
"",
"You passed the following:",
f"pipette_name: {self.pipette_name}",
f"pipette_model: {self.pipette_model}",
f"pipette_serial_code: {self.pipette_serial_code}",
)
)
)

return all(no_pipette)

@classmethod
def from_json_string(cls, env_var_string: str) -> "OT3PipetteEnvVar":
"""Create OT3PipetteEnvVar from json string."""
env_var = json.loads(env_var_string)
return cls(
pipette_name=PipetteName[env_var["pipette_name"]],
pipette_name=env_var["pipette_name"],
pipette_model=env_var["pipette_model"],
pipette_serial_code=env_var["pipette_serial_code"].encode("utf-8"),
pipette_serial_code=env_var["pipette_serial_code"],
eeprom_file_path=env_var["eeprom_file_path"],
)

def _generate_serial_code(self) -> bytes:
"""Generate serial code."""
return serial_val_from_parts(
self.pipette_name, self.pipette_model, self.pipette_serial_code
PipetteName[self.pipette_name],
self.pipette_model,
self.pipette_serial_code.encode("utf-8"),
)

def generate_eeprom_file(self) -> None:
"""Generate eeprom file for pipette."""
with open(self.eeprom_file_path, "wb") as f:
f.write(self._generate_serial_code())
if self.is_no_pipette():
return
else:
f.write(self._generate_serial_code())


def _get_env_var(env_var_name: str) -> str:
Expand Down
134 changes: 134 additions & 0 deletions hardware/tests/test_scripts/test_emulation_pipette_provision.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,74 @@ def set_env_vars(
monkeypatch.delenv("RIGHT_OT3_PIPETTE_DEFINITION")


@pytest.fixture
def set_no_left_pipette_env_vars(
tmp_eeprom_file_paths: Generator[Tuple[str, str], None, None], monkeypatch: Any
) -> Generator[None, None, None]:
"""Set environment variables."""
left, right = tmp_eeprom_file_paths
monkeypatch.setenv(
"LEFT_OT3_PIPETTE_DEFINITION",
json.dumps(
{
"pipette_name": "EMPTY",
"pipette_model": -1,
"pipette_serial_code": "",
"eeprom_file_path": left,
}
),
)

monkeypatch.setenv(
"RIGHT_OT3_PIPETTE_DEFINITION",
json.dumps(
{
"pipette_name": "p50_multi",
"pipette_model": 34,
"pipette_serial_code": "20230609",
"eeprom_file_path": right,
}
),
)
yield
monkeypatch.delenv("LEFT_OT3_PIPETTE_DEFINITION")
monkeypatch.delenv("RIGHT_OT3_PIPETTE_DEFINITION")


@pytest.fixture
def set_no_right_pipette_env_vars(
tmp_eeprom_file_paths: Generator[Tuple[str, str], None, None], monkeypatch: Any
) -> Generator[None, None, None]:
"""Set environment variables."""
left, right = tmp_eeprom_file_paths
monkeypatch.setenv(
"LEFT_OT3_PIPETTE_DEFINITION",
json.dumps(
{
"pipette_name": "p1000_multi",
"pipette_model": 34,
"pipette_serial_code": "20230609",
"eeprom_file_path": left,
}
),
)

monkeypatch.setenv(
"RIGHT_OT3_PIPETTE_DEFINITION",
json.dumps(
{
"pipette_name": "EMPTY",
"pipette_model": -1,
"pipette_serial_code": "",
"eeprom_file_path": right,
}
),
)
yield
monkeypatch.delenv("LEFT_OT3_PIPETTE_DEFINITION")
monkeypatch.delenv("RIGHT_OT3_PIPETTE_DEFINITION")


@pytest.fixture
def expected_values() -> Tuple[bytes, bytes]:
"""Expected values."""
Expand All @@ -64,6 +132,26 @@ def expected_values() -> Tuple[bytes, bytes]:
)


@pytest.fixture
def expected_no_left_pipette_values() -> Tuple[bytes, bytes]:
"""Expected values."""
return (
b"",
serial_val_from_parts(PipetteName["p50_multi"], 34, "20230609".encode("utf-8")),
)


@pytest.fixture
def expected_no_right_pipette_values() -> Tuple[bytes, bytes]:
"""Expected values."""
return (
serial_val_from_parts(
PipetteName["p1000_multi"], 34, "20230609".encode("utf-8")
),
b"",
)


def test_main(
set_env_vars: Generator[None, None, None],
tmp_eeprom_file_paths: Tuple[str, str],
Expand All @@ -85,3 +173,49 @@ def test_main(

assert left_eeprom == expected_left
assert right_eeprom == expected_right


def test_main_no_left_pipette(
set_no_left_pipette_env_vars: Generator[None, None, None],
tmp_eeprom_file_paths: Tuple[str, str],
expected_no_left_pipette_values: Tuple[bytes, bytes],
) -> None:
"""Test provision with no left pipette."""
left_eeprom_path, right_eeprom_path = tmp_eeprom_file_paths
expected_left, expected_right = expected_no_left_pipette_values
assert not os.path.exists(left_eeprom_path)
assert not os.path.exists(right_eeprom_path)
emulation_pipette_provision.main()
assert os.path.exists(left_eeprom_path)
assert os.path.exists(right_eeprom_path)

with open(left_eeprom_path, "rb") as f:
left_eeprom = f.read()
with open(right_eeprom_path, "rb") as f:
right_eeprom = f.read()

assert left_eeprom == expected_left
assert right_eeprom == expected_right


def test_main_no_right_pipette(
set_no_right_pipette_env_vars: Generator[None, None, None],
tmp_eeprom_file_paths: Tuple[str, str],
expected_no_right_pipette_values: Tuple[bytes, bytes],
) -> None:
"""Test provisioning with no right pipette."""
left_eeprom_path, right_eeprom_path = tmp_eeprom_file_paths
expected_left, expected_right = expected_no_right_pipette_values
assert not os.path.exists(left_eeprom_path)
assert not os.path.exists(right_eeprom_path)
emulation_pipette_provision.main()
assert os.path.exists(left_eeprom_path)
assert os.path.exists(right_eeprom_path)

with open(left_eeprom_path, "rb") as f:
left_eeprom = f.read()
with open(right_eeprom_path, "rb") as f:
right_eeprom = f.read()

assert left_eeprom == expected_left
assert right_eeprom == expected_right