<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 async def update_firmware( module: AbstractModule, firmware_file: str, loop: Optional[asyncio.AbstractEventLoop]) -> AbstractModule: """ Update a module. If the update succeeds, an Module instance will be returned. Otherwise, raises an UpdateError with the reason for the failure. """

    simulating = module.is_simulated
    cls = type(module)
    old_port = module.port
    flash_port = await module.prep_for_update()
    callback = module.interrupt_callback
    del module
    after_port, results = await update.update_firmware(flash_port,
                                                       firmware_file,
                                                       loop)
    await asyncio.sleep(1.0)
    new_port = after_port or old_port
    if not results[0]:
        raise UpdateError(results[1])
    return await cls.build(
        port=new_port,
        interrupt_callback=callback,
        simulating=simulating)