Skip to content

Commit

Permalink
Ophyd-async support now also register child devices.
Browse files Browse the repository at this point in the history
  • Loading branch information
canismarko committed Sep 8, 2024
1 parent e428eee commit 9ee402f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dependencies = ["ophyd"]

[project.optional-dependencies]

dev = ["black", "isort", "pytest", "build", "twine", "flake8", "ruff", "pytest-mock", "caproto"]
dev = ["ophyd_async", "black", "isort", "pytest", "build", "twine", "flake8", "ruff", "pytest-mock", "caproto"]

[project.urls]
"Homepage" = "https://github.com/spc-group/ophyd-registry"
Expand Down
23 changes: 18 additions & 5 deletions src/ophydregistry/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import warnings
from collections import OrderedDict
from itertools import chain
from typing import Hashable, List, Mapping, Optional, Tuple, Sequence
from typing import Hashable, List, Mapping, Optional, Sequence, Tuple
from weakref import WeakSet, WeakValueDictionary

from ophyd import ophydobj
Expand Down Expand Up @@ -126,7 +126,11 @@ class Registry:
use_typhos: bool
keep_references: bool
_auto_register: bool
_valid_classes: Tuple[type] = (ophydobj.OphydObject, _AggregateSignalState, AsyncDevice)
_valid_classes: Tuple[type] = (
ophydobj.OphydObject,
_AggregateSignalState,
AsyncDevice,
)

# components: Sequence
_objects_by_name: Mapping
Expand Down Expand Up @@ -255,7 +259,9 @@ def pop_disconnected(self, timeout: float = 0.0) -> List:
timeout_reached = False
while not timeout_reached:
# Remove any connected devices for the running list
remaining = [dev for dev in remaining if not getattr(dev, "connected", True)]
remaining = [
dev for dev in remaining if not getattr(dev, "connected", True)
]
if len(remaining) == 0:
# All devices are connected, so just end early.
break
Expand Down Expand Up @@ -584,7 +590,14 @@ def register(

typhos.plugins.register_signal(component)
# Recusively register sub-components
sub_signals = getattr(component, "_signals", {})
for cpt_name, cpt in sub_signals.items():
if hasattr(component, "_signals"):
# Vanilla ophyd device
sub_signals = component._signals.items()
elif hasattr(component, "children"):
# Ophyd-async device
sub_signals = component.children()
else:
sub_signals = []
for cpt_name, cpt in sub_signals:
self.register(cpt)
return component
8 changes: 8 additions & 0 deletions src/ophydregistry/tests/test_instrument_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import pytest
from ophyd import Device, EpicsMotor, sim
from ophyd_async.epics.motor import Motor

from ophydregistry import ComponentNotFound, MultipleComponentsFound, Registry

Expand Down Expand Up @@ -143,6 +144,13 @@ def test_find_component(registry):
result = registry.find(label="ion_chamber")


def test_find_async_children(registry):
"""Check that the child components of an async device get registered."""
motor = Motor(prefix="255idcVME:m1", name="m1")
registry.register(motor)
assert registry.find(motor.user_setpoint.name) is motor.user_setpoint


def test_find_name_by_dot_notation(registry):
# Create a simulated component
cptA = sim.SynGauss(
Expand Down

0 comments on commit 9ee402f

Please sign in to comment.