import steamvr
import os
import time


def load_init_config(serial, config_path):
    assert os.path.exists(config_path), "Path to init_steamvr does not exist"
    print("opening init config file for read")
    config = steamvr.lighthouse_console.load_steamvr_json(config_path)
    print("creating lighthouse console")
    lh = steamvr.LighthouseConsole()
    print("opening lighthouse console")
    lh.open()
    print("lighthouse console is open.")
    #device_list = lh.list_devices()
    #assert len(device_list) == 1, "Too many devices connected"
    #headset_serial = [x for x in device_list if x.startswith('LHR')][0]

    lh.select_device(serial)
    steamvr.steamvr.set_serial_number(config, serial)
    lh.upload_config(config)
    time.sleep(3)  # give the config upload time to wrap up

    conf = lh.download_config()
    assert steamvr.steamvr.get_serial_number(conf) == serial, "Failed to load init config"
    time.sleep(3)  # give the config download time to wrap up
    print("Completed")
