Skip to content

Commit 1669240

Browse files
committed
Split DeviceBuildResult devices and connection specs
Makes access to the devices easier and makes it easier to build parameters
1 parent c3e9266 commit 1669240

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/dodal/device_manager.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def build(
166166
).or_raise()
167167
if connect_immediately:
168168
devices.connect(timeout=timeout or self.timeout).or_raise()
169-
device = devices.devices[self.name].device
169+
device = devices.devices[self.name]
170170
if name:
171171
device.set_name(name)
172172
return device # type: ignore - it's us, honest
@@ -286,17 +286,16 @@ def build(self, mock: bool = False, fixtures: dict[str, Any] | None = None) -> V
286286
mock=mock,
287287
).or_raise()
288288

289-
device = devices.devices[self.name].device
289+
device = devices.devices[self.name]
290290
return device # type: ignore - it's us really, promise
291291

292292
def __repr__(self) -> str:
293293
return f"<{self.name}: V1DeviceFactory({self.factory.__name__})>"
294294

295295

296296
class ConnectionSpec(NamedTuple):
297-
"""A device paired with the options used to configure it"""
297+
"""The options used to configure a device"""
298298

299-
device: OphydV2Device
300299
mock: bool
301300
timeout: float
302301

@@ -322,20 +321,22 @@ def or_raise(self) -> dict[str, Any]:
322321
class DeviceBuildResult(NamedTuple):
323322
"""Wrapper around the results of building devices"""
324323

325-
devices: dict[str, ConnectionSpec]
324+
devices: dict[str, AnyDevice]
326325
errors: dict[str, Exception]
326+
connection_specs: dict[str, ConnectionSpec]
327327

328328
def connect(self, timeout: float | None = None) -> ConnectionResult:
329329
"""Connect all devices that didn't fail to build"""
330330
connections = {}
331331
connected = {}
332332
loop: asyncio.EventLoop = get_bluesky_event_loop() # type: ignore
333-
for name, (device, mock, dev_timeout) in self.devices.items():
333+
for name, device in self.devices.items():
334334
if not isinstance(device, OphydV2Device):
335335
# TODO: Remove when ophyd v1 support is no longer required
336336
# V1 devices are connected at creation time assuming wait is not set to False
337337
connected[name] = device
338338
continue
339+
mock, dev_timeout = self.connection_specs[name]
339340
timeout = timeout or dev_timeout or DEFAULT_TIMEOUT
340341
fut = asyncio.run_coroutine_threadsafe(
341342
device.connect(mock=mock, timeout=timeout),
@@ -347,7 +348,7 @@ def connect(self, timeout: float | None = None) -> ConnectionResult:
347348
for name, connection_future in connections.items():
348349
try:
349350
connection_future.result()
350-
connected[name] = self.devices[name].device
351+
connected[name] = self.devices[name]
351352
except Exception as e:
352353
connection_errors[name] = e
353354

@@ -506,7 +507,8 @@ def build_devices(
506507
order = self._build_order(
507508
{dep: self[dep] for dep in build_list}, fixtures=fixtures
508509
)
509-
built: dict[str, ConnectionSpec] = {}
510+
built: dict[str, AnyDevice] = {}
511+
connection_specs: dict[str, ConnectionSpec] = {}
510512
errors = {}
511513
for device in order:
512514
factory = self[device]
@@ -529,15 +531,15 @@ def build_devices(
529531
# TODO: Remove when ophyd v1 support is no longer required
530532
factory = factory.mock_if_needed(mock)
531533
built_device = factory._create(**params)
532-
built[device] = ConnectionSpec(
533-
built_device,
534+
built[device] = built_device
535+
connection_specs[device] = ConnectionSpec(
534536
mock=mock or factory.mock,
535537
timeout=factory.timeout,
536538
)
537539
except Exception as e:
538540
errors[device] = e
539541

540-
return DeviceBuildResult(built, errors)
542+
return DeviceBuildResult(built, errors, connection_specs)
541543

542544
def __contains__(self, name):
543545
return name in self._factories or name in self._v1_factories

0 commit comments

Comments
 (0)