@@ -263,8 +263,7 @@ def lock_status(self):
263
263
return BlackbirdSync (url )
264
264
265
265
266
- @asyncio .coroutine
267
- def get_async_blackbird (port_url , loop ):
266
+ async def get_async_blackbird (port_url , loop ):
268
267
"""
269
268
Return asynchronous version of Blackbird interface
270
269
:param port_url: serial port, i.e. '/dev/ttyUSB0'
@@ -274,52 +273,44 @@ def get_async_blackbird(port_url, loop):
274
273
lock = asyncio .Lock ()
275
274
276
275
def locked_coro (coro ):
277
- @asyncio .coroutine
278
276
@wraps (coro )
279
- def wrapper (* args , ** kwargs ):
280
- with (yield from lock ):
281
- return (yield from coro (* args , ** kwargs ))
277
+ async def wrapper (* args , ** kwargs ):
278
+ with (await lock ):
279
+ return (await coro (* args , ** kwargs ))
282
280
return wrapper
283
281
284
282
class BlackbirdAsync (Blackbird ):
285
283
def __init__ (self , blackbird_protocol ):
286
284
self ._protocol = blackbird_protocol
287
285
288
286
@locked_coro
289
- @asyncio .coroutine
290
- def zone_status (self , zone : int ):
291
- string = yield from self ._protocol .send (_format_zone_status_request (zone ), skip = 15 )
287
+ async def zone_status (self , zone : int ):
288
+ string = await self ._protocol .send (_format_zone_status_request (zone ), skip = 15 )
292
289
return ZoneStatus .from_string (zone , string )
293
290
294
291
@locked_coro
295
- @asyncio .coroutine
296
- def set_zone_power (self , zone : int , power : bool ):
297
- yield from self ._protocol .send (_format_set_zone_power (zone , power ))
292
+ async def set_zone_power (self , zone : int , power : bool ):
293
+ await self ._protocol .send (_format_set_zone_power (zone , power ))
298
294
299
295
@locked_coro
300
- @asyncio .coroutine
301
- def set_zone_source (self , zone : int , source : int ):
302
- yield from self ._protocol .send (_format_set_zone_source (zone , source ))
296
+ async def set_zone_source (self , zone : int , source : int ):
297
+ await self ._protocol .send (_format_set_zone_source (zone , source ))
303
298
304
299
@locked_coro
305
- @asyncio .coroutine
306
- def set_all_zone_source (self , source : int ):
307
- yield from self ._protocol .send (_format_set_all_zone_source (source ))
300
+ async def set_all_zone_source (self , source : int ):
301
+ await self ._protocol .send (_format_set_all_zone_source (source ))
308
302
309
303
@locked_coro
310
- @asyncio .coroutine
311
- def lock_front_buttons (self ):
312
- yield from self ._protocol .send (_format_lock_front_buttons ())
304
+ async def lock_front_buttons (self ):
305
+ await self ._protocol .send (_format_lock_front_buttons ())
313
306
314
307
@locked_coro
315
- @asyncio .coroutine
316
- def unlock_front_buttons (self ):
317
- yield from self ._protocol .send (_format_unlock_front_buttons ())
308
+ async def unlock_front_buttons (self ):
309
+ await self ._protocol .send (_format_unlock_front_buttons ())
318
310
319
311
@locked_coro
320
- @asyncio .coroutine
321
- def lock_status (self ):
322
- string = yield from self ._protocol .send (_format_lock_status ())
312
+ async def lock_status (self ):
313
+ string = await self ._protocol .send (_format_lock_status ())
323
314
return LockStatus .from_string (string )
324
315
325
316
class BlackbirdProtocol (asyncio .Protocol ):
@@ -339,20 +330,19 @@ def connection_made(self, transport):
339
330
def data_received (self , data ):
340
331
asyncio .ensure_future (self .q .put (data ), loop = self ._loop )
341
332
342
- @asyncio .coroutine
343
- def send (self , request : bytes , skip = 0 ):
344
- yield from self ._connected .wait ()
333
+ async def send (self , request : bytes , skip = 0 ):
334
+ await self ._connected .wait ()
345
335
result = bytearray ()
346
336
# Only one transaction at a time
347
- with (yield from self ._lock ):
337
+ with (await self ._lock ):
348
338
self ._transport .serial .reset_output_buffer ()
349
339
self ._transport .serial .reset_input_buffer ()
350
340
while not self .q .empty ():
351
341
self .q .get_nowait ()
352
342
self ._transport .write (request )
353
343
try :
354
344
while True :
355
- result += yield from asyncio .wait_for (self .q .get (), TIMEOUT , loop = self ._loop )
345
+ result += await asyncio .wait_for (self .q .get (), TIMEOUT , loop = self ._loop )
356
346
if len (result ) > skip and result [- LEN_EOL :] == EOL :
357
347
ret = bytes (result )
358
348
_LOGGER .debug ('Received "%s"' , ret )
@@ -361,6 +351,6 @@ def send(self, request: bytes, skip=0):
361
351
_LOGGER .error ("Timeout during receiving response for command '%s', received='%s'" , request , result )
362
352
raise
363
353
364
- _ , protocol = yield from create_serial_connection (loop , functools .partial (BlackbirdProtocol , loop ), port_url , baudrate = 9600 )
354
+ _ , protocol = await create_serial_connection (loop , functools .partial (BlackbirdProtocol , loop ), port_url , baudrate = 9600 )
365
355
366
- return BlackbirdAsync (protocol )
356
+ return BlackbirdAsync (protocol )
0 commit comments