Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into signal-typing
Browse files Browse the repository at this point in the history
  • Loading branch information
coretl committed Oct 29, 2024
2 parents f22f409 + c91d283 commit 057a78f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/ophyd_async/core/_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import Any, TypeVar

from bluesky.protocols import HasName
from bluesky.run_engine import call_in_bluesky_event_loop
from bluesky.run_engine import call_in_bluesky_event_loop, in_bluesky_event_loop

from ._protocol import Connectable
from ._utils import DEFAULT_TIMEOUT, NotConnected, wait_for_connection
Expand Down Expand Up @@ -286,6 +286,11 @@ async def __aexit__(self, type, value, traceback):
await self._on_exit()

def __exit__(self, type_, value, traceback):
if in_bluesky_event_loop():
raise RuntimeError(
"Cannot use DeviceConnector inside a plan, instead use "
"`yield from ophyd_async.plan_stubs.ensure_connected(device)`"
)
self._objects_on_exit = self._caller_locals()
try:
fut = call_in_bluesky_event_loop(self._on_exit())
Expand Down
10 changes: 10 additions & 0 deletions tests/core/test_device_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ def test_sync_device_connector_run_engine_created_connects(RE):
assert working_device.connected


def test_connecting_in_plan_raises(RE):
def bad_plan():
yield from bps.null()
with DeviceCollector():
working_device = WorkingDevice("somename") # noqa: F841

with pytest.raises(RuntimeError, match="Cannot use DeviceConnector inside a plan"):
RE(bad_plan())


def test_async_device_connector_run_engine_same_event_loop():
async def set_up_device():
async with DeviceCollector(mock=True):
Expand Down
21 changes: 21 additions & 0 deletions tests/epics/signal/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
from typing import Any, Literal
from unittest.mock import ANY

import bluesky.plan_stubs as bps
import numpy as np
import pytest
from aioca import purge_channel_caches
from bluesky.protocols import Reading
from bluesky.run_engine import RunEngine
from event_model import DataKey, Limits, LimitsRange
from ophyd.signal import EpicsSignal

from ophyd_async.core import (
Array1D,
Expand Down Expand Up @@ -897,3 +900,21 @@ async def test_signals_created_for_not_prec_0_float_cannot_use_int(ioc: IOC):
".* cannot be coerced to int",
):
await sig.connect()


async def test_can_read_using_ophyd_async_then_ophyd(ioc: IOC):
oa_read = f"{ioc.protocol}://{PV_PREFIX}:{ioc.protocol}:float_prec_1"
ophyd_read = f"{PV_PREFIX}:{ioc.protocol}:float_prec_0"

ophyd_async_sig = epics_signal_rw(float, oa_read)
await ophyd_async_sig.connect()
ophyd_signal = EpicsSignal(ophyd_read)
ophyd_signal.wait_for_connection(timeout=5)

RE = RunEngine()

def my_plan():
yield from bps.rd(ophyd_async_sig)
yield from bps.rd(ophyd_signal)

RE(my_plan())

0 comments on commit 057a78f

Please sign in to comment.