identify device by ip in api

This commit is contained in:
2025-02-07 21:53:12 -05:00
parent 9b2a3064d4
commit 576352819e
2 changed files with 13 additions and 45 deletions

22
app.py
View File

@ -55,18 +55,20 @@ class UnifiAPI:
self.session = requests.Session() self.session = requests.Session()
self.session.verify = False self.session.verify = False
def get_device_details(self, device_id): def get_all_devices(self):
try: url = f"{self.base_url}/proxy/network/integration/v1/sites/{self.site_id}/devices"
url = f"{self.base_url}/proxy/network/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, timeout=5) return response.json()
response.raise_for_status()
return response.json() def get_device_details(self, ip_address):
except requests.exceptions.RequestException as e: devices = self.get_all_devices()
logging.error(f"Failed to get device details: {e}") for device in devices:
return None if device['ipAddress'] == ip_address:
return device
return None
def get_device_diagnostics(self, device): def get_device_diagnostics(self, device):
details = self.get_device_details(device['device_id']) details = self.get_device_details(device['ip'])
if not details: if not details:
return {'state': 'ERROR', 'error': 'Failed to fetch device details'} return {'state': 'ERROR', 'error': 'Failed to fetch device details'}

View File

@ -10,48 +10,14 @@
"ip": "10.10.10.1", "ip": "10.10.10.1",
"type": "router", "type": "router",
"connection_type": "copper", "connection_type": "copper",
"critical": true,
"device_id": "udm-pro-id"
},
{
"name": "USP PDU Pro",
"ip": "10.10.10.107",
"type": "pdu",
"connection_type": "copper",
"critical": true "critical": true
}, },
{
"name": "USW Aggregation",
"ip": "10.10.10.238",
"type": "switch",
"connection_type": "fiber",
"critical": true
},
{
"name": "USW Flex Mini",
"ip": "10.10.10.169",
"type": "switch",
"connection_type": "copper"
},
{ {
"name": "USW Pro 24 PoE", "name": "USW Pro 24 PoE",
"ip": "10.10.10.139", "ip": "10.10.10.139",
"type": "switch", "type": "switch",
"connection_type": "fiber", "connection_type": "fiber",
"critical": true, "critical": true
"device_id": "usw-24-poe-id"
},
{
"name": "U6 Lite",
"ip": "10.10.10.154",
"type": "access_point",
"connection_type": "copper"
},
{
"name": "USP Strip",
"ip": "10.10.10.243",
"type": "pdu",
"connection_type": "copper"
} }
], ],
"check_interval": 30, "check_interval": 30,