async def test_connection_lost(mocker, event_loop): device = "/dev/ttyACM0" serial_interface = mock.Mock() def dummy_serial_conn(loop, protocol_factory, url, *args, **kwargs): fut = loop.create_future() assert url == device protocol = protocol_factory() # Our event loop doesn't really do anything event_loop.add_writer = lambda *args, **kwargs: None event_loop.add_reader = lambda *args, **kwargs: None event_loop.remove_writer = lambda *args, **kwargs: None event_loop.remove_reader = lambda *args, **kwargs: None transport = SerialTransport(event_loop, protocol, serial_interface) protocol.connection_made(transport) fut.set_result((transport, protocol)) return fut mocker.patch("serial_asyncio.create_serial_connection", new=dummy_serial_conn) api = mock.Mock() conn_lost_fut = event_loop.create_future() api.connection_lost = conn_lost_fut.set_result protocol = await znp_uart.connect( conf.SCHEMA_DEVICE({conf.CONF_DEVICE_PATH: device}), api=api ) exception = RuntimeError("Uh oh, something broke") protocol.connection_lost(exception) # Losing a connection propagates up to the api assert (await conn_lost_fut) == exception api.reset_mock() conn_closed_fut = event_loop.create_future() api.connection_lost = conn_closed_fut.set_result protocol = await znp_uart.connect( conf.SCHEMA_DEVICE({conf.CONF_DEVICE_PATH: device}), api=api ) protocol.close() # Closing a connection does as well assert (await conn_closed_fut) is None
async def test_connect_auto(mocker): device = "/dev/ttyACM0" def dummy_serial_conn(loop, protocol_factory, url, *args, **kwargs): fut = loop.create_future() assert url == device transport = mock.Mock() protocol = protocol_factory() protocol.connection_made(transport) fut.set_result((transport, protocol)) return fut mocker.patch("zigpy_znp.uart.guess_port", return_value=device) mocker.patch("serial_asyncio.create_serial_connection", new=dummy_serial_conn) api = mock.Mock() await znp_uart.connect(conf.SCHEMA_DEVICE({conf.CONF_DEVICE_PATH: "auto"}), api=api)
async def test_probe(pingable_serial_port): # noqa: F811 assert not (await ControllerApplication.probe( conf.SCHEMA_DEVICE({conf.CONF_DEVICE_PATH: "/dev/null"}))) assert await ControllerApplication.probe( conf.SCHEMA_DEVICE({conf.CONF_DEVICE_PATH: pingable_serial_port}))