nickovs / unificontrol

A high-level Python interface to the Unifi controller software
Apache License 2.0
96 stars 41 forks source link

Reuse unificontrol.UnifiClient when iterating sites on same controller #33

Open smarthotspots opened 2 years ago

smarthotspots commented 2 years ago

Currently if we "login" to unifi controllers and then get a list of sites to iterate for more details , it looks like a new instance of unificontrol.UnifiClient needs to be created each time in the loop. So for example, if there are 100 sites on a controller and we iterate them and an API for each site, there are 100 "logins" to the controller API created. For example if we iterate a list of controller, extract list of sites from each one, and then attempt to device level detail of each . Can there be a way to create an instance of unificlient without if calling a login to controller as we've already been authenticated in the first "cont" loop . Just trying to reduce the amount of logins required and load on unfi controller..

controllers =[ ["myserver.acme.com","8443","admin","mysecretpassword1"],"myserver2.acme.com","8443","admin","mysecretpassword2"]]

for cont in controllers:
login = unificontrol.UnifiClient(host=cont[0],port=cont[1],username=cont[2], password=cont[3], site="default",cert=None)

sites = login.list_sites()

for site in sites:
    current_site = site.get("name")
    current_sitedesc = site.get("desc")
    client = unificontrol.UnifiClient(host=cont[0],port=cont[1],username=cont[2], password=cont[3], site=current_site,cert=None)
    devices=  client.list_devices()

    for device in devices:
        ip =  device.get("ip")
        device_name = device.get("name")
        print (current_sitedesc , " " , device_name, " " , ip)