Skip to content

Commit

Permalink
refactor(api): Port ReadAbsorbanceImpl and FileStore to `StateUpd…
Browse files Browse the repository at this point in the history
…ate` (#16978)
  • Loading branch information
SyntaxColoring authored Dec 3, 2024
1 parent 1e01e63 commit 64fbf42
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
MAXIMUM_CSV_FILE_LIMIT,
)
from ...resources import FileProvider
from ...state import update_types

if TYPE_CHECKING:
from opentrons.protocol_engine.state.state import StateView
Expand Down Expand Up @@ -143,7 +144,7 @@ async def execute( # noqa: C901
# Today, the action handler for the FileStore looks for a ReadAbsorbanceResult command action, this will need to be delinked.

# Begin interfacing with the file provider if the user provided a filename
file_ids = []
file_ids: list[str] = []
if params.fileName is not None:
# Create the Plate Reader Transform
plate_read_result = PlateReaderData.construct(
Expand Down Expand Up @@ -175,7 +176,13 @@ async def execute( # noqa: C901
)

return SuccessData(
public=ReadAbsorbanceResult(data=asbsorbance_result, fileIds=file_ids),
public=ReadAbsorbanceResult(
data=asbsorbance_result,
fileIds=file_ids,
),
state_update=update_types.StateUpdate(
files_added=update_types.FilesAddedUpdate(file_ids=file_ids)
),
)


Expand Down
20 changes: 9 additions & 11 deletions api/src/opentrons/protocol_engine/state/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
from dataclasses import dataclass
from typing import List

from opentrons.protocol_engine.actions.get_state_update import get_state_updates
from opentrons.protocol_engine.state import update_types

from ._abstract_store import HasState, HandlesActions
from ..actions import Action, SucceedCommandAction
from ..commands import (
Command,
absorbance_reader,
)
from ..actions import Action


@dataclass
Expand All @@ -28,13 +27,12 @@ def __init__(self) -> None:

def handle_action(self, action: Action) -> None:
"""Modify state in reaction to an action."""
if isinstance(action, SucceedCommandAction):
self._handle_command(action.command)
for state_update in get_state_updates(action):
self._handle_state_update(state_update)

def _handle_command(self, command: Command) -> None:
if isinstance(command.result, absorbance_reader.ReadAbsorbanceResult):
if command.result.fileIds is not None:
self._state.file_ids.extend(command.result.fileIds)
def _handle_state_update(self, state_update: update_types.StateUpdate) -> None:
if state_update.files_added != update_types.NO_CHANGE:
self._state.file_ids.extend(state_update.files_added.file_ids)


class FileView(HasState[FileState]):
Expand Down
28 changes: 18 additions & 10 deletions api/src/opentrons/protocol_engine/state/update_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class LabwareLocationUpdate:
new_location: LabwareLocation
"""The labware's new location."""

offset_id: typing.Optional[str]
offset_id: str | None
"""The ID of the labware's new offset, for its new location."""


Expand All @@ -112,10 +112,10 @@ class LoadedLabwareUpdate:
new_location: LabwareLocation
"""The labware's initial location."""

offset_id: typing.Optional[str]
offset_id: str | None
"""The ID of the labware's offset."""

display_name: typing.Optional[str]
display_name: str | None

definition: LabwareDefinition

Expand All @@ -133,7 +133,7 @@ class LoadPipetteUpdate:

pipette_name: PipetteNameType
mount: MountType
liquid_presence_detection: typing.Optional[bool]
liquid_presence_detection: bool | None


@dataclasses.dataclass
Expand Down Expand Up @@ -162,7 +162,7 @@ class PipetteTipStateUpdate:
"""Update pipette tip state."""

pipette_id: str
tip_geometry: typing.Optional[TipGeometry]
tip_geometry: TipGeometry | None


@dataclasses.dataclass
Expand Down Expand Up @@ -261,6 +261,13 @@ class LiquidClassLoadedUpdate:
liquid_class_record: LiquidClassRecord


@dataclasses.dataclass
class FilesAddedUpdate:
"""An update that adds a new data file."""

file_ids: list[str]


@dataclasses.dataclass
class StateUpdate:
"""Represents an update to perform on engine state."""
Expand Down Expand Up @@ -299,6 +306,8 @@ class StateUpdate:

liquid_class_loaded: LiquidClassLoadedUpdate | NoChangeType = NO_CHANGE

files_added: FilesAddedUpdate | NoChangeType = NO_CHANGE

def append(self, other: Self) -> Self:
"""Apply another `StateUpdate` "on top of" this one.
Expand Down Expand Up @@ -378,7 +387,6 @@ def set_pipette_location( # noqa: D102
new_deck_point=new_deck_point,
)
else:

self.pipette_location = PipetteLocationUpdate(
pipette_id=pipette_id,
new_location=Well(labware_id=new_labware_id, well_name=new_well_name),
Expand Down Expand Up @@ -410,8 +418,8 @@ def set_loaded_labware(
self: Self,
definition: LabwareDefinition,
labware_id: str,
offset_id: typing.Optional[str],
display_name: typing.Optional[str],
offset_id: str | None,
display_name: str | None,
location: LabwareLocation,
) -> Self:
"""Add a new labware to state. See `LoadedLabwareUpdate`."""
Expand All @@ -429,7 +437,7 @@ def set_load_pipette(
pipette_id: str,
pipette_name: PipetteNameType,
mount: MountType,
liquid_presence_detection: typing.Optional[bool],
liquid_presence_detection: bool | None,
) -> Self:
"""Add a new pipette to state. See `LoadPipetteUpdate`."""
self.loaded_pipette = LoadPipetteUpdate(
Expand Down Expand Up @@ -462,7 +470,7 @@ def update_pipette_nozzle(
return self

def update_pipette_tip_state(
self: Self, pipette_id: str, tip_geometry: typing.Optional[TipGeometry]
self: Self, pipette_id: str, tip_geometry: TipGeometry | None
) -> Self:
"""Update a pipette's tip state. See `PipetteTipStateUpdate`."""
self.pipette_tip_state = PipetteTipStateUpdate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ async def test_move_to_well_stall_defined_error(
error_id = "error-id"
error_timestamp = datetime(year=2020, month=1, day=2)
decoy.when(
movement.move_to_well(
await movement.move_to_well(
pipette_id="abc",
labware_id="123",
well_name="A3",
Expand Down

0 comments on commit 64fbf42

Please sign in to comment.