v2 api
This commit is contained in:
36
app.py
36
app.py
@ -50,42 +50,22 @@ class UnifiAPI:
|
|||||||
self.session = requests.Session()
|
self.session = requests.Session()
|
||||||
self.session.verify = False
|
self.session.verify = False
|
||||||
self.headers = {
|
self.headers = {
|
||||||
'Authorization': f'Bearer {config["unifi"]["api_key"]}',
|
'X-API-KEY': config['unifi']['api_key'],
|
||||||
'Accept': 'application/json',
|
'Accept': 'application/json'
|
||||||
'Content-Type': 'application/json'
|
|
||||||
}
|
}
|
||||||
self.site_id = self.get_site_id()
|
self.site_id = "default" # Use default site for now
|
||||||
|
|
||||||
def get_site_id(self):
|
|
||||||
url = f"{self.base_url}/proxy/network/api/self/sites"
|
|
||||||
response = self.session.get(url, headers=self.headers)
|
|
||||||
response.raise_for_status()
|
|
||||||
sites = response.json()['data']
|
|
||||||
return sites[0]['_id']
|
|
||||||
|
|
||||||
def get_all_devices(self):
|
def get_all_devices(self):
|
||||||
url = f"{self.base_url}/proxy/network/api/s/{self.site_id}/stat/device"
|
url = f"{self.base_url}/proxy/network/v2/api/site/{self.site_id}/device"
|
||||||
response = self.session.get(url, headers=self.headers)
|
response = self.session.get(url, headers=self.headers)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
return response.json()['data']
|
return response.json()['data']
|
||||||
|
|
||||||
def get_device_details(self, device_id):
|
def get_device_details(self, device_id):
|
||||||
url = f"{self.base_url}/proxy/network/integration/v1/sites/{self.site_id}/devices/{device_id}"
|
url = f"{self.base_url}/proxy/network/v2/api/site/{self.site_id}/device/{device_id}"
|
||||||
response = self.session.get(url, headers=self.headers)
|
response = self.session.get(url, headers=self.headers)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
logger.debug(f"Device details response: {response.text}")
|
return response.json()['data']
|
||||||
data = response.json()
|
|
||||||
return {
|
|
||||||
'state': data['state'],
|
|
||||||
'firmware': {
|
|
||||||
'version': data['firmwareVersion'],
|
|
||||||
'updatable': data.get('firmwareUpdatable', False)
|
|
||||||
},
|
|
||||||
'network': {
|
|
||||||
'ip': data['ipAddress'],
|
|
||||||
'mac': data['macAddress']
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
def get_device_diagnostics(self, device):
|
def get_device_diagnostics(self, device):
|
||||||
details = self.get_device_details(device['ip'])
|
details = self.get_device_details(device['ip'])
|
||||||
|
|||||||
Reference in New Issue
Block a user