# Buggy code
async def _autodiscover_device(args, loop):
    apple_tv = await _scan_for_device(
        args, args.scan_timeout, loop, protocol=args.protocol
    )
    if not apple_tv:
        return None

    def _set_credentials(protocol, field):
        service = apple_tv.get_service(protocol)
        if service:
            value = service.credentials or getattr(args, field)
            service.credentials = value

    _set_credentials(Protocol.DMAP, "dmap_credentials")
    _set_credentials(Protocol.MRP, "mrp_credentials")
    _set_credentials(Protocol.AirPlay, "airplay_credentials")

    logging.info("Auto-discovered %s at %s", args.name, args.address)

    return apple_tv


# Traceback (asyncio.TimeoutError)
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyatv/__main__.py", line 565, in _run_application
return await cli_handler(loop)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyatv/__main__.py", line 388, in cli_handler
return await _handle_commands(args, config, loop)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyatv/__main__.py", line 480, in _handle_commands
ret = await _handle_device_command(args, cmd, atv, loop)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyatv/__main__.py", line 517, in _handle_device_command
return (await _exec_command(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyatv/__main__.py", line 531, in _exec_command
value = await tmp(*args)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyatv/airplay/__init__.py", line 71, in play_url
player = await self._player(session)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyatv/airplay/__init__.py", line 51, in _player
await verifier.verify_authed()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyatv/airplay/auth.py", line 88, in verify_authed
resp = await self._send(self.srp.verify1(), 'verify1')
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyatv/airplay/auth.py", line 100, in _send
resp, code = await self.http.post_data(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyatv/net.py", line 76, in post_data
raise ex
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyatv/net.py", line 64, in post_data
resp = await self._session.post(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/aiohttp/client.py", line 504, in _request
await resp.start(conn)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/aiohttp/client_reqrep.py", line 860, in start
self._continue = None
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/aiohttp/helpers.py", line 596, in __exit__
raise asyncio.TimeoutError from None
asyncio.exceptions.TimeoutError

# Fixed code
async def _autodiscover_device(args, loop):
    apple_tv = await _scan_for_device(
        args, args.scan_timeout, loop, protocol=args.protocol
    )
    if not apple_tv:
        return None

    def _set_credentials(protocol, field):
        service = apple_tv.get_service(protocol)
        if service:
            value = service.credentials or getattr(args, field)
            service.credentials = value

    _set_credentials(Protocol.DMAP, "dmap_credentials")
    _set_credentials(Protocol.MRP, "mrp_credentials")
    _set_credentials(Protocol.AirPlay, "airplay_credentials")

    logging.info("Auto-discovered %s at %s", apple_tv.name, apple_tv.address)

    return apple_tv