A (read only) Python library for interacting with the Unifi Controller API
Project description
unifi-controller-api
A Python client library for interacting with Ubiquiti UniFi Network Controllers.
Core Features
- Read-Only Access: Designed for insights and reporting, so no changes can or will be made.
- Structured Data Models: Parses API responses into typed Python objects (e.g.,
UnifiDevice,UnifiSite,UnifiClient,LLDPEntry,UnifiWlanConf). - Automatic Model Mapping: Translates model codes (e.g.,
U7PG2) to friendly names ("UniFi® AC Pro AP") via themodel_nameattribute. - Convenience Methods: Includes helpers for data export (
export_csv,export_json). - Minimal Dependencies: Requires only
requests. - Python >= 3.8
Installation
pip install unifi-controller-api
Quick Start
from unifi_controller_api import UnifiController
# 1. Initialize & Authenticate
controller = UnifiController(
controller_url="https://<CONTROLLER_IP_OR_HOSTNAME>", # Use :8443 for dedicated controller
username="<LOCAL_ADMIN_USER>",
password="<PASSWORD>",
is_udm_pro=True, # Set True for UniFi OS devices, False for legacy
verify_ssl=False # Or path to CA bundle
)
# 2. Fetch Data (Example: Devices for the 'default' site)
site_name = "default" # Use the internal site name
try:
devices = controller.get_unifi_site_device(site_name=site_name, detailed=True)
# 3. Use the Typed Data
for device in devices:
print(f"- {device.name} ({device.model_name}): {device.ip} / {device.mac}")
if device.lldp_info:
print(f" LLDP: {len(device.lldp_info)} neighbors")
except Exception as e:
print(f"Error fetching devices for site '{site_name}': {e}")
# Other available methods:
# sites = controller.get_unifi_site()
# clients = controller.get_clients(site_name)
# wlans = controller.get_wlan_conf(site_name)
# alarms = controller.get_alarms(site_name)
# events = controller.get_events(site_name)
# rogue_aps = controller.get_rogue_aps(site_name)
# networks = controller.get_network_conf(site_name)
# report = controller.devices_report(site_names=['site1', 'site2'])
# Exporting data:
# from unifi_controller_api.export import export_csv, export_json
# if devices:
# export_csv(devices, "devices.csv")
# export_json(devices, "devices.json")
Connectivity Notes
- Requires direct network access to the controller.
- Uses HTTPS port 443 for UniFi OS devices (UDM, Cloud Key 2.x+, etc.).
- Uses HTTPS port 8443 for legacy controllers (Software, Cloud Key Gen1/Gen2 pre-2.x).
- Requires a local controller account, not a UniFi Cloud/SSO account.
- Set
is_udm_pro=Truefor UniFi OS,Falsefor dedicated controller. - Use
verify_ssl=Falseor provide a CA bundle path for self-signed certificates.
Data Models
The library automatically maps JSON API responses to Python data classes located in unifi_controller_api.models. Key models include:
UnifiSite: Represents a controller site.UnifiDevice: Represents a network device (AP, Switch, Gateway).LLDPEntry: Holds LLDP neighbor details (nested withinUnifiDevice).UnifiClient: Represents a connected client (wired or wireless).UnifiWlanConf: Represents a Wireless LAN configuration.UnifiNetworkConf: Represents a Network configuration.UnifiAlarm: Represents a controller alarm.UnifiEvent: Represents a controller event.UnifiRogueAp: Represents a detected rogue access point.
Use standard object attribute access (e.g., device.ip, site.desc) to work with the data.
Logging
Uses standard Python logging. Configure the unifi_controller_api logger:
import logging
logging.getLogger("unifi_controller_api").setLevel(logging.DEBUG)
# Ensure you have a handler configured (e.g., via logging.basicConfig)
logging.basicConfig(level=logging.INFO) # Example: Show INFO level and above
Disclaimer
This is an unofficial library using APIs that may change without notice. Not affiliated with Ubiquiti Inc. Use at your own risk.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file unifi_controller_api-0.2.1.tar.gz.
File metadata
- Download URL: unifi_controller_api-0.2.1.tar.gz
- Upload date:
- Size: 34.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f0b88fddd8aabdefd3663b17ab010a896aae0a1a2c3fb96d5c95c9a6f5d2046
|
|
| MD5 |
2e4025b5af37d2b89e366c2e02ebd8e0
|
|
| BLAKE2b-256 |
54be1108f244b716b40d275a3893c929b0b223299dc7858c11b9ce58f671a251
|
File details
Details for the file unifi_controller_api-0.2.1-py3-none-any.whl.
File metadata
- Download URL: unifi_controller_api-0.2.1-py3-none-any.whl
- Upload date:
- Size: 38.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b9fd00272dc3421e720b8081016f713cc30b3ef49d4c00f2ee0e70997fb1e5f
|
|
| MD5 |
9f8a733b8c4ddb33bc24d53c3562ef00
|
|
| BLAKE2b-256 |
e0dd73ee4ae3dc55920b38a85363a026674df0849ee9d6b8d05f3cda7577e17b
|