Skip to content

Commit 652febb

Browse files
authored
Merge pull request #8 from hrnciar/fix-missing-asyncio-coroutine-decorator
Replace coroutine decorator with async keyword
2 parents 19dd805 + 355aea2 commit 652febb

File tree

1 file changed

+24
-34
lines changed

1 file changed

+24
-34
lines changed

pyblackbird/__init__.py

+24-34
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,7 @@ def lock_status(self):
263263
return BlackbirdSync(url)
264264

265265

266-
@asyncio.coroutine
267-
def get_async_blackbird(port_url, loop):
266+
async def get_async_blackbird(port_url, loop):
268267
"""
269268
Return asynchronous version of Blackbird interface
270269
:param port_url: serial port, i.e. '/dev/ttyUSB0'
@@ -274,52 +273,44 @@ def get_async_blackbird(port_url, loop):
274273
lock = asyncio.Lock()
275274

276275
def locked_coro(coro):
277-
@asyncio.coroutine
278276
@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))
282280
return wrapper
283281

284282
class BlackbirdAsync(Blackbird):
285283
def __init__(self, blackbird_protocol):
286284
self._protocol = blackbird_protocol
287285

288286
@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)
292289
return ZoneStatus.from_string(zone, string)
293290

294291
@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))
298294

299295
@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))
303298

304299
@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))
308302

309303
@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())
313306

314307
@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())
318310

319311
@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())
323314
return LockStatus.from_string(string)
324315

325316
class BlackbirdProtocol(asyncio.Protocol):
@@ -339,20 +330,19 @@ def connection_made(self, transport):
339330
def data_received(self, data):
340331
asyncio.ensure_future(self.q.put(data), loop=self._loop)
341332

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()
345335
result = bytearray()
346336
# Only one transaction at a time
347-
with (yield from self._lock):
337+
with (await self._lock):
348338
self._transport.serial.reset_output_buffer()
349339
self._transport.serial.reset_input_buffer()
350340
while not self.q.empty():
351341
self.q.get_nowait()
352342
self._transport.write(request)
353343
try:
354344
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)
356346
if len(result) > skip and result[-LEN_EOL:] == EOL:
357347
ret = bytes(result)
358348
_LOGGER.debug('Received "%s"', ret)
@@ -361,6 +351,6 @@ def send(self, request: bytes, skip=0):
361351
_LOGGER.error("Timeout during receiving response for command '%s', received='%s'", request, result)
362352
raise
363353

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)
365355

366-
return BlackbirdAsync(protocol)
356+
return BlackbirdAsync(protocol)

0 commit comments

Comments
 (0)