device and site id
This commit is contained in:
39
app.py
39
app.py
@ -47,44 +47,33 @@ def send_webhook(device, status, diagnostics):
|
|||||||
class UnifiAPI:
|
class UnifiAPI:
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
self.base_url = config['unifi']['controller']
|
self.base_url = config['unifi']['controller']
|
||||||
|
self.site_id = "1647955405" # Fixed site ID
|
||||||
self.session = requests.Session()
|
self.session = requests.Session()
|
||||||
self.session.verify = False
|
self.session.verify = False
|
||||||
self.headers = {
|
self.headers = {
|
||||||
'X-API-KEY': config['unifi']['api_key'],
|
'X-API-KEY': config['unifi']['api_key'],
|
||||||
'Accept': 'application/json'
|
'Accept': 'application/json'
|
||||||
}
|
}
|
||||||
self.site_id = self._get_site_id()
|
|
||||||
|
|
||||||
def _get_site_id(self):
|
|
||||||
url = f"{self.base_url}/proxy/network/integration/v1/sites"
|
|
||||||
response = self.session.get(url, headers=self.headers)
|
|
||||||
response.raise_for_status()
|
|
||||||
sites = response.json()['data']
|
|
||||||
return sites[0]['id'] # Get first site ID
|
|
||||||
|
|
||||||
def get_all_devices(self):
|
def get_all_devices(self):
|
||||||
url = f"{self.base_url}/proxy/network/integration/v1/sites/{self.site_id}/devices"
|
url = f"{self.base_url}/integration/v1/sites/{self.site_id}/devices"
|
||||||
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']
|
devices = response.json()['data']
|
||||||
|
# Map IP addresses to device IDs
|
||||||
|
self.device_map = {device['ipAddress']: device['id'] for device in devices}
|
||||||
|
return devices
|
||||||
|
|
||||||
def get_device_details(self, device_id):
|
def get_device_details(self, ip_address):
|
||||||
url = f"{self.base_url}/proxy/network/integration/v1/sites/{self.site_id}/devices/{device_id}"
|
device_id = self.device_map.get(ip_address)
|
||||||
|
if not device_id:
|
||||||
|
self.get_all_devices() # Refresh device map
|
||||||
|
device_id = self.device_map.get(ip_address)
|
||||||
|
|
||||||
|
url = f"{self.base_url}/integration/v1/sites/{self.site_id}/devices/{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()
|
||||||
data = response.json()
|
return response.json()
|
||||||
return {
|
|
||||||
'state': data['state'],
|
|
||||||
'firmware': {
|
|
||||||
'version': data['firmwareVersion'],
|
|
||||||
'updatable': data.get('firmwareUpdatable', False)
|
|
||||||
},
|
|
||||||
'network': {
|
|
||||||
'ip': data['ipAddress'],
|
|
||||||
'mac': data['macAddress']
|
|
||||||
},
|
|
||||||
'interfaces': self._parse_interfaces(data.get('interfaces', {}))
|
|
||||||
}
|
|
||||||
|
|
||||||
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