1515 Generic ,
1616 NamedTuple ,
1717 ParamSpec ,
18+ Self ,
1819 TypeVar ,
1920 cast ,
2021)
@@ -193,7 +194,7 @@ def build(
193194 device = devices .devices [self .name ].device
194195 if name :
195196 device .set_name (name )
196- return device
197+ return device # type: ignore - it's us, honest
197198
198199 def _create (self , * args , ** kwargs ) -> V2 :
199200 return self (* args , ** kwargs )
@@ -266,6 +267,22 @@ def skip(self) -> bool:
266267 """
267268 return self ._skip () if callable (self ._skip ) else self ._skip
268269
270+ def mock_if_needed (self , mock = False ) -> Self :
271+ # TODO: Remove when Ophyd V1 support is no longer required
272+ factory = (
273+ make_fake_device (self .factory ) if (self .mock or mock ) else self .factory
274+ )
275+ return self .__class__ (
276+ factory = factory ,
277+ prefix = self .prefix ,
278+ mock = mock or self .mock ,
279+ skip = self ._skip ,
280+ wait = self .wait ,
281+ timeout = self .timeout ,
282+ init = self .post_create ,
283+ manager = self ._manager ,
284+ )
285+
269286 def __call__ (self , * args , ** kwargs ):
270287 """Call the wrapped function to make decorator transparent"""
271288 return self .post_create (* args , ** kwargs )
@@ -289,15 +306,8 @@ def build(self, mock: bool = False, fixtures: dict[str, Any] | None = None) -> V
289306 print (devices .errors )
290307 raise Exception ("??? build" )
291308 else :
292- # if connect_immediately:
293- # conn = devices.connect(timeout=timeout or self.timeout)
294- # if conn.connection_errors:
295- # # TODO: NotConnected?
296- # raise Exception("??? conn")
297309 device = devices .devices [self .name ].device
298- # if name:
299- # device.set_name(name)
300- return device
310+ return device # type: ignore - it's us really, promise
301311
302312
303313class ConnectionParameters (NamedTuple ):
@@ -306,7 +316,7 @@ class ConnectionParameters(NamedTuple):
306316
307317
308318class ConnectionSpec (NamedTuple ):
309- device : OphydV1Device | OphydV2Device
319+ device : OphydV2Device
310320 params : ConnectionParameters
311321
312322
@@ -331,16 +341,19 @@ class DeviceBuildResult(NamedTuple):
331341
332342 def connect (self , timeout : float | None = None ) -> ConnectionResult :
333343 connections = {}
344+ connected = {}
334345 loop : asyncio .EventLoop = get_bluesky_event_loop () # type: ignore
335346 for name , (device , (mock , dev_timeout )) in self .devices .items ():
347+ if not isinstance (device , OphydV2Device ):
348+ connected [name ] = device
349+ continue
336350 timeout = timeout or dev_timeout or DEFAULT_TIMEOUT
337- fut : futures . Future = asyncio .run_coroutine_threadsafe (
351+ fut = asyncio .run_coroutine_threadsafe (
338352 device .connect (mock = mock , timeout = timeout ),
339353 loop = loop ,
340354 )
341355 connections [name ] = fut
342356
343- connected = {}
344357 connection_errors = {}
345358 for name , connection_future in connections .items ():
346359 try :
@@ -509,6 +522,8 @@ def build_devices(
509522 is not _EMPTY
510523 }
511524 try :
525+ if isinstance (factory , V1DeviceFactory ):
526+ factory = factory .mock_if_needed (mock )
512527 built_device = factory ._create (** params )
513528 built [device ] = ConnectionSpec (
514529 built_device ,
0 commit comments