Skip to content

Commit

Permalink
Replace coroutine decorator with async keyword
Browse files Browse the repository at this point in the history
Fixes: koolsb#7
  • Loading branch information
hrnciar committed Jun 30, 2022
1 parent 19dd805 commit 355aea2
Showing 1 changed file with 24 additions and 34 deletions.
58 changes: 24 additions & 34 deletions pyblackbird/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,7 @@ def lock_status(self):
return BlackbirdSync(url)


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

def locked_coro(coro):
@asyncio.coroutine
@wraps(coro)
def wrapper(*args, **kwargs):
with (yield from lock):
return (yield from coro(*args, **kwargs))
async def wrapper(*args, **kwargs):
with (await lock):
return (await coro(*args, **kwargs))
return wrapper

class BlackbirdAsync(Blackbird):
def __init__(self, blackbird_protocol):
self._protocol = blackbird_protocol

@locked_coro
@asyncio.coroutine
def zone_status(self, zone: int):
string = yield from self._protocol.send(_format_zone_status_request(zone), skip=15)
async def zone_status(self, zone: int):
string = await self._protocol.send(_format_zone_status_request(zone), skip=15)
return ZoneStatus.from_string(zone, string)

@locked_coro
@asyncio.coroutine
def set_zone_power(self, zone: int, power: bool):
yield from self._protocol.send(_format_set_zone_power(zone, power))
async def set_zone_power(self, zone: int, power: bool):
await self._protocol.send(_format_set_zone_power(zone, power))

@locked_coro
@asyncio.coroutine
def set_zone_source(self, zone: int, source: int):
yield from self._protocol.send(_format_set_zone_source(zone, source))
async def set_zone_source(self, zone: int, source: int):
await self._protocol.send(_format_set_zone_source(zone, source))

@locked_coro
@asyncio.coroutine
def set_all_zone_source(self, source: int):
yield from self._protocol.send(_format_set_all_zone_source(source))
async def set_all_zone_source(self, source: int):
await self._protocol.send(_format_set_all_zone_source(source))

@locked_coro
@asyncio.coroutine
def lock_front_buttons(self):
yield from self._protocol.send(_format_lock_front_buttons())
async def lock_front_buttons(self):
await self._protocol.send(_format_lock_front_buttons())

@locked_coro
@asyncio.coroutine
def unlock_front_buttons(self):
yield from self._protocol.send(_format_unlock_front_buttons())
async def unlock_front_buttons(self):
await self._protocol.send(_format_unlock_front_buttons())

@locked_coro
@asyncio.coroutine
def lock_status(self):
string = yield from self._protocol.send(_format_lock_status())
async def lock_status(self):
string = await self._protocol.send(_format_lock_status())
return LockStatus.from_string(string)

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

@asyncio.coroutine
def send(self, request: bytes, skip=0):
yield from self._connected.wait()
async def send(self, request: bytes, skip=0):
await self._connected.wait()
result = bytearray()
# Only one transaction at a time
with (yield from self._lock):
with (await self._lock):
self._transport.serial.reset_output_buffer()
self._transport.serial.reset_input_buffer()
while not self.q.empty():
self.q.get_nowait()
self._transport.write(request)
try:
while True:
result += yield from asyncio.wait_for(self.q.get(), TIMEOUT, loop=self._loop)
result += await asyncio.wait_for(self.q.get(), TIMEOUT, loop=self._loop)
if len(result) > skip and result[-LEN_EOL:] == EOL:
ret = bytes(result)
_LOGGER.debug('Received "%s"', ret)
Expand All @@ -361,6 +351,6 @@ def send(self, request: bytes, skip=0):
_LOGGER.error("Timeout during receiving response for command '%s', received='%s'", request, result)
raise

_, protocol = yield from create_serial_connection(loop, functools.partial(BlackbirdProtocol, loop), port_url, baudrate=9600)
_, protocol = await create_serial_connection(loop, functools.partial(BlackbirdProtocol, loop), port_url, baudrate=9600)

return BlackbirdAsync(protocol)
return BlackbirdAsync(protocol)

0 comments on commit 355aea2

Please sign in to comment.