@asyncio.coroutine<EOL>def get_async_monoprice(port_url, loop):
lock = asyncio.Lock()<EOL>def locked_coro(coro):<EOL><INDENT>@asyncio.coroutine<EOL>@wraps(coro)<EOL>def wrapper(*args, **kwargs):<EOL><INDENT>with (yield from lock):<EOL><INDENT>return (yield from coro(*args, **kwargs))<EOL><DEDENT><DEDENT>return wrapper<EOL><DEDENT>class MonopriceAsync(Monoprice):<EOL><INDENT>def __init__(self, monoprice_protocol):<EOL><INDENT>self._protocol = monoprice_protocol<EOL><DEDENT>@locked_coro<EOL>@asyncio.coroutine<EOL>def zone_status(self, zone: int):<EOL><INDENT>string = yield from self._protocol.send(_format_zone_status_request(zone), skip=<NUM_LIT:6>)<EOL>return ZoneStatus.from_string(string)<EOL><DEDENT>@locked_coro<EOL>@asyncio.coroutine<EOL>def set_power(self, zone: int, power: bool):<EOL><INDENT>yield from self._protocol.send(_format_set_power(zone, power))<EOL><DEDENT>@locked_coro<EOL>@asyncio.coroutine<EOL>def set_mute(self, zone: int, mute: bool):<EOL><INDENT>yield from self._protocol.send(_format_set_mute(zone, mute))<EOL><DEDENT>@locked_coro<EOL>@asyncio.coroutine<EOL>def set_volume(self, zone: int, volume: int):<EOL><INDENT>yield from self._protocol.send(_format_set_volume(zone, volume))<EOL><DEDENT>@locked_coro<EOL>@asyncio.coroutine<EOL>def set_treble(self, zone: int, treble: int):<EOL><INDENT>yield from self._protocol.send(_format_set_treble(zone, treble))<EOL><DEDENT>@locked_coro<EOL>@asyncio.coroutine<EOL>def set_bass(self, zone: int, bass: int):<EOL><INDENT>yield from self._protocol.send(_format_set_bass(zone, bass))<EOL><DEDENT>@locked_coro<EOL>@asyncio.coroutine<EOL>def set_balance(self, zone: int, balance: int):<EOL><INDENT>yield from self._protocol.send(_format_set_balance(zone, balance))<EOL><DEDENT>@locked_coro<EOL>@asyncio.coroutine<EOL>def set_source(self, zone: int, source: int):<EOL><INDENT>yield from self._protocol.send(_format_set_source(zone, source))<EOL><DEDENT>@locked_coro<EOL>@asyncio.coroutine<EOL>def restore_zone(self, status: ZoneStatus):<EOL><INDENT>yield from self._protocol.send(_format_set_power(status.zone, status.power))<EOL>yield from self._protocol.send(_format_set_mute(status.zone, status.mute))<EOL>yield from self._protocol.send(_format_set_volume(status.zone, status.volume))<EOL>yield from self._protocol.send(_format_set_treble(status.zone, status.treble))<EOL>yield from self._protocol.send(_format_set_bass(status.zone, status.bass))<EOL>yield from self._protocol.send(_format_set_balance(status.zone, status.balance))<EOL>yield from self._protocol.send(_format_set_source(status.zone, status.source))<EOL><DEDENT><DEDENT>class MonopriceProtocol(asyncio.Protocol):<EOL><INDENT>def __init__(self, loop):<EOL><INDENT>super().__init__()<EOL>self._loop = loop<EOL>self._lock = asyncio.Lock()<EOL>self._transport = None<EOL>self._connected = asyncio.Event(loop=loop)<EOL>self.q = asyncio.Queue(loop=loop)<EOL><DEDENT>def connection_made(self, transport):<EOL><INDENT>self._transport = transport<EOL>self._connected.set()<EOL>_LOGGER.debug('<STR_LIT>', self._transport)<EOL><DEDENT>def data_received(self, data):<EOL><INDENT>asyncio.ensure_future(self.q.put(data), loop=self._loop)<EOL><DEDENT>@asyncio.coroutine<EOL>def send(self, request: bytes, skip=<NUM_LIT:0>):<EOL><INDENT>yield from self._connected.wait()<EOL>result = bytearray()<EOL>with (yield from self._lock):<EOL><INDENT>self._transport.serial.reset_output_buffer()<EOL>self._transport.serial.reset_input_buffer()<EOL>while not self.q.empty():<EOL><INDENT>self.q.get_nowait()<EOL><DEDENT>self._transport.write(request)<EOL>try:<EOL><INDENT>while True:<EOL><INDENT>result += yield from asyncio.wait_for(self.q.get(), TIMEOUT, loop=self._loop)<EOL>if len(result) > skip and result[-LEN_EOL:] == EOL:<EOL><INDENT>ret = bytes(result)<EOL>_LOGGER.debug('<STR_LIT>', ret)<EOL>return ret.decode('<STR_LIT:ascii>')<EOL><DEDENT><DEDENT><DEDENT>except asyncio.TimeoutError:<EOL><INDENT>_LOGGER.error("<STR_LIT>", request, result)<EOL>raise<EOL><DEDENT><DEDENT><DEDENT><DEDENT>_, protocol = yield from create_serial_connection(loop, functools.partial(MonopriceProtocol, loop),<EOL>port_url, baudrate=<NUM_LIT>)<EOL>return MonopriceAsync(protocol)<EOL>
Return asynchronous version of Monoprice interface
:param port_url: serial port, i.e. '/dev/ttyUSB0'
:return: asynchronous implementation of Monoprice interface