unifi-controller-api 0.3.2__tar.gz → 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- unifi_controller_api-0.4.0/MANIFEST.in +1 -0
- unifi_controller_api-0.4.0/PKG-INFO +244 -0
- unifi_controller_api-0.4.0/README.md +217 -0
- {unifi_controller_api-0.3.2 → unifi_controller_api-0.4.0}/pyproject.toml +17 -10
- unifi_controller_api-0.4.0/skills/unifi-controller-api/SKILL.md +145 -0
- unifi_controller_api-0.4.0/skills/unifi-controller-api/references/change-safety.md +74 -0
- unifi_controller_api-0.4.0/skills/unifi-controller-api/references/response-handling.md +125 -0
- unifi_controller_api-0.4.0/skills/unifi-controller-api/references/setup.md +113 -0
- unifi_controller_api-0.4.0/skills/unifi-controller-api/references/troubleshooting.md +55 -0
- unifi_controller_api-0.4.0/skills/unifi-controller-api/scripts/read_only_inventory.py +241 -0
- unifi_controller_api-0.4.0/tests/test_agent_skill.py +259 -0
- unifi_controller_api-0.4.0/tests/test_api_error_details.py +101 -0
- unifi_controller_api-0.4.0/tests/test_api_response_handling.py +48 -0
- unifi_controller_api-0.4.0/tests/test_csrf_token.py +38 -0
- unifi_controller_api-0.4.0/tests/test_device_report.py +85 -0
- unifi_controller_api-0.4.0/tests/test_firewall_rules.py +154 -0
- unifi_controller_api-0.4.0/tests/test_model_export.py +59 -0
- unifi_controller_api-0.4.0/tests/test_package_smoke.py +35 -0
- unifi_controller_api-0.4.0/tests/test_request_timeout.py +136 -0
- unifi_controller_api-0.4.0/tests/test_response_contract_regressions.py +172 -0
- unifi_controller_api-0.4.0/tests/test_stats_endpoints.py +74 -0
- {unifi_controller_api-0.3.2 → unifi_controller_api-0.4.0}/unifi_controller_api/__init__.py +3 -1
- {unifi_controller_api-0.3.2 → unifi_controller_api-0.4.0}/unifi_controller_api/api_client.py +186 -19
- {unifi_controller_api-0.3.2 → unifi_controller_api-0.4.0}/unifi_controller_api/device-models.json +49 -0
- unifi_controller_api-0.4.0/unifi_controller_api/exceptions.py +114 -0
- {unifi_controller_api-0.3.2 → unifi_controller_api-0.4.0}/unifi_controller_api/export.py +12 -1
- {unifi_controller_api-0.3.2 → unifi_controller_api-0.4.0}/unifi_controller_api/models/__init__.py +2 -0
- {unifi_controller_api-0.3.2 → unifi_controller_api-0.4.0}/unifi_controller_api/models/client.py +10 -1
- unifi_controller_api-0.4.0/unifi_controller_api/models/firewall_rule.py +62 -0
- {unifi_controller_api-0.3.2 → unifi_controller_api-0.4.0}/unifi_controller_api/models/portconf.py +18 -0
- unifi_controller_api-0.4.0/unifi_controller_api.egg-info/PKG-INFO +244 -0
- {unifi_controller_api-0.3.2 → unifi_controller_api-0.4.0}/unifi_controller_api.egg-info/SOURCES.txt +19 -0
- {unifi_controller_api-0.3.2 → unifi_controller_api-0.4.0}/unifi_controller_api.egg-info/requires.txt +2 -4
- unifi_controller_api-0.3.2/PKG-INFO +0 -158
- unifi_controller_api-0.3.2/README.md +0 -128
- unifi_controller_api-0.3.2/unifi_controller_api/exceptions.py +0 -28
- unifi_controller_api-0.3.2/unifi_controller_api.egg-info/PKG-INFO +0 -158
- {unifi_controller_api-0.3.2 → unifi_controller_api-0.4.0}/LICENSE +0 -0
- {unifi_controller_api-0.3.2 → unifi_controller_api-0.4.0}/setup.cfg +0 -0
- {unifi_controller_api-0.3.2 → unifi_controller_api-0.4.0}/unifi_controller_api/logging.py +0 -0
- {unifi_controller_api-0.3.2 → unifi_controller_api-0.4.0}/unifi_controller_api/models/alarm.py +0 -0
- {unifi_controller_api-0.3.2 → unifi_controller_api-0.4.0}/unifi_controller_api/models/device.py +0 -0
- {unifi_controller_api-0.3.2 → unifi_controller_api-0.4.0}/unifi_controller_api/models/event.py +0 -0
- {unifi_controller_api-0.3.2 → unifi_controller_api-0.4.0}/unifi_controller_api/models/health.py +0 -0
- {unifi_controller_api-0.3.2 → unifi_controller_api-0.4.0}/unifi_controller_api/models/networkconf.py +0 -0
- {unifi_controller_api-0.3.2 → unifi_controller_api-0.4.0}/unifi_controller_api/models/rogueap.py +0 -0
- {unifi_controller_api-0.3.2 → unifi_controller_api-0.4.0}/unifi_controller_api/models/site.py +0 -0
- {unifi_controller_api-0.3.2 → unifi_controller_api-0.4.0}/unifi_controller_api/models/wlanconf.py +0 -0
- {unifi_controller_api-0.3.2 → unifi_controller_api-0.4.0}/unifi_controller_api/utils.py +0 -0
- {unifi_controller_api-0.3.2 → unifi_controller_api-0.4.0}/unifi_controller_api.egg-info/dependency_links.txt +0 -0
- {unifi_controller_api-0.3.2 → unifi_controller_api-0.4.0}/unifi_controller_api.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
recursive-include skills/unifi-controller-api *.md *.py
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: unifi-controller-api
|
|
3
|
+
Version: 0.4.0
|
|
4
|
+
Summary: A Python library for interacting with the Unifi Controller API
|
|
5
|
+
Author-email: Tyler Woods <tyler@tylermade.net>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/tnware/unifi-controller-api
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/tnware/unifi-controller-api/issues
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Topic :: System :: Networking :: Monitoring
|
|
12
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
13
|
+
Classifier: Topic :: Utilities
|
|
14
|
+
Requires-Python: >=3.9
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Requires-Dist: requests
|
|
18
|
+
Provides-Extra: dev
|
|
19
|
+
Requires-Dist: pytest>=6.0; extra == "dev"
|
|
20
|
+
Requires-Dist: pytest-cov>=2.12; extra == "dev"
|
|
21
|
+
Requires-Dist: pytest-mock>=3.10.0; extra == "dev"
|
|
22
|
+
Requires-Dist: python-dotenv>=1.0.0; extra == "dev"
|
|
23
|
+
Requires-Dist: ruff==0.17.6; extra == "dev"
|
|
24
|
+
Provides-Extra: docs
|
|
25
|
+
Requires-Dist: griffe>=2.0; python_version >= "3.11" and extra == "docs"
|
|
26
|
+
Dynamic: license-file
|
|
27
|
+
|
|
28
|
+
# unifi-controller-api
|
|
29
|
+
|
|
30
|
+
A Python client library for interacting with Ubiquiti UniFi Network Controllers.
|
|
31
|
+
|
|
32
|
+
[](https://pypi.org/project/unifi-controller-api/)
|
|
33
|
+
[](https://pypi.org/project/unifi-controller-api/)
|
|
34
|
+
|
|
35
|
+
**Documentation:** <https://tnware.github.io/unifi-controller-api/>: guides, a method index with the controller
|
|
36
|
+
endpoint each method calls, and the generated Python API reference.
|
|
37
|
+
|
|
38
|
+
> **Warning:** This package is under active development and is subject to breaking changes, incomplete or incorrect documentation, etc.
|
|
39
|
+
|
|
40
|
+
## Relationship to the official UniFi API
|
|
41
|
+
|
|
42
|
+
Ubiquiti provides an [official UniFi API reference](https://developer.ui.com/),
|
|
43
|
+
including the [UniFi Network API](https://developer.ui.com/network).
|
|
44
|
+
There is substantial overlap with this library; consult the official documentation
|
|
45
|
+
when choosing an API for a new integration.
|
|
46
|
+
|
|
47
|
+
This project is an **unofficial local-controller client**, not Ubiquiti's official
|
|
48
|
+
SDK. It currently authenticates with a local username/password and uses the
|
|
49
|
+
controller's private APIs, rather than the official API-key authentication flow.
|
|
50
|
+
It provides convenience methods, optional typed models, and access to additional
|
|
51
|
+
controller endpoints that are not necessarily covered by the official API.
|
|
52
|
+
Availability and behavior depend on the controller and Network application version.
|
|
53
|
+
|
|
54
|
+
Existing methods retain their private-API behavior: an overlapping official endpoint
|
|
55
|
+
does not mean that a method uses it or silently switches to it. Do not assume that
|
|
56
|
+
legacy settings describe every modern policy; these may be separate resources on
|
|
57
|
+
newer controllers.
|
|
58
|
+
|
|
59
|
+
## Core Features
|
|
60
|
+
|
|
61
|
+
* **Structured Data Models:** Optionally parses API responses into typed Python objects (e.g., `UnifiDevice`, `UnifiSite`, `UnifiClient`, `LLDPEntry`, `UnifiWlanConf`).
|
|
62
|
+
* **Automatic Model Mapping:** Translates model codes (e.g., `U7PG2`) to friendly names ("UniFi® AC Pro AP") via the `model_name` attribute.
|
|
63
|
+
* **Convenience Methods:** Includes helpers for data export (`export_csv`, `export_json`).
|
|
64
|
+
* **Minimal Dependencies:** Requires only `requests`.
|
|
65
|
+
* **Python >= 3.9**
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## Installation
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
pip install unifi-controller-api
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
## Use with an AI agent
|
|
78
|
+
|
|
79
|
+
The repository ships an [Agent Skills](https://agentskills.io/)-compatible
|
|
80
|
+
[skill](skills/unifi-controller-api/SKILL.md) that tells an agent how to install the
|
|
81
|
+
package, connect and read safely, with a guarded read-only example. Paste this prompt
|
|
82
|
+
into your agent:
|
|
83
|
+
|
|
84
|
+
```text
|
|
85
|
+
Help me set up or use the unifi-controller-api Python package with my UniFi controller.
|
|
86
|
+
Follow its agent skill and the references next to it, from the same revision:
|
|
87
|
+
https://github.com/tnware/unifi-controller-api/blob/main/skills/unifi-controller-api/SKILL.md
|
|
88
|
+
Use the docs for agents: https://tnware.github.io/unifi-controller-api/llms.txt
|
|
89
|
+
Keep TLS verification enabled, never ask me to paste passwords into chat, and make no
|
|
90
|
+
changes to my controller or network without separate approval.
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
To pin a tag or commit, replace `main` in the URL. Installing the skill doesn't install
|
|
94
|
+
the package or grant network access; review the skill and its scripts before running them.
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
## Quick Start
|
|
99
|
+
|
|
100
|
+
The example below matches this source checkout. Older published artifacts may
|
|
101
|
+
lack newer arguments such as `request_timeout`; inspect the installed interface
|
|
102
|
+
or use the skill's guarded compatibility example before connecting.
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
from unifi_controller_api import UnifiController
|
|
106
|
+
|
|
107
|
+
# 1. Initialize & Authenticate
|
|
108
|
+
controller = UnifiController(
|
|
109
|
+
controller_url="https://<CONTROLLER_IP_OR_HOSTNAME>", # Use :443 for UniFi OS, :8443 for legacy
|
|
110
|
+
username="<LOCAL_ADMIN_USER>",
|
|
111
|
+
password="<PASSWORD>",
|
|
112
|
+
is_udm_pro=True, # Set True for UniFi OS devices (UDM, Cloud Key Gen2+), False for legacy software/hardware controllers
|
|
113
|
+
verify_ssl=True, # Recommended; use a trusted CA bundle path for private certificates
|
|
114
|
+
auto_model_mapping=True, # Optional: Attempt to map device model codes to friendly names
|
|
115
|
+
model_db_path=None, # Optional: Path to a custom model database file
|
|
116
|
+
auth_retry_enabled=True, # Optional: Enable automatic retries on authentication failure
|
|
117
|
+
auth_retry_count=3, # Optional: Number of authentication retries
|
|
118
|
+
auth_retry_delay=5, # Optional: Delay in seconds between authentication retries
|
|
119
|
+
request_timeout=30 # Optional: Timeout in seconds for controller HTTP requests
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
# 2. Fetch Data (Example: Devices for the 'default' site)
|
|
123
|
+
site_name = "default" # Use the internal site name
|
|
124
|
+
try:
|
|
125
|
+
devices = controller.get_unifi_site_device(site_name=site_name, detailed=True, raw=False)
|
|
126
|
+
|
|
127
|
+
# 3. Use the Typed Data
|
|
128
|
+
for device in devices:
|
|
129
|
+
print(f"- {device.name} ({device.model_name}): {device.ip} / {device.mac}")
|
|
130
|
+
if device.lldp_info:
|
|
131
|
+
print(f" LLDP: {len(device.lldp_info)} neighbors")
|
|
132
|
+
|
|
133
|
+
except Exception:
|
|
134
|
+
# Exception strings can include controller-reflected data and private URLs.
|
|
135
|
+
print("Device discovery failed; inspect only approved, sanitized diagnostics.")
|
|
136
|
+
|
|
137
|
+
# Other available methods:
|
|
138
|
+
# sites = controller.get_unifi_site(include_health=False)
|
|
139
|
+
# clients = controller.get_unifi_site_client(site_name)
|
|
140
|
+
# wlans = controller.get_unifi_site_wlanconf(site_name)
|
|
141
|
+
# alarms = controller.get_unifi_site_alarm(site_name)
|
|
142
|
+
# events = controller.get_unifi_site_event(site_name)
|
|
143
|
+
# rogue_aps = controller.get_unifi_site_rogueap(site_name)
|
|
144
|
+
# networks = controller.get_unifi_site_networkconf(site_name)
|
|
145
|
+
# firewall_rules = controller.get_unifi_site_firewallrule(site_name)
|
|
146
|
+
# report = controller.devices_report(site_names=['site1', 'site2'])
|
|
147
|
+
|
|
148
|
+
# Exporting data:
|
|
149
|
+
# from unifi_controller_api.export import export_csv, export_json
|
|
150
|
+
# if devices:
|
|
151
|
+
# export_csv(devices, "devices.csv")
|
|
152
|
+
# export_json(devices, "devices.json")
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
## Connectivity Notes
|
|
158
|
+
|
|
159
|
+
* Requires direct network access to the controller.
|
|
160
|
+
* The `controller_url` should include the scheme (https) and port (e.g., `https://192.168.1.1:443` for UniFi OS, `https://unifi.example.com:8443` for older software controllers).
|
|
161
|
+
* Requires direct network access to the controller.
|
|
162
|
+
* Uses **HTTPS port 443** by default for UniFi OS devices (UDM, Cloud Key Gen2+).
|
|
163
|
+
* Uses **HTTPS port 8443** by default for legacy controllers (Software, Cloud Key Gen1).
|
|
164
|
+
* Requires a **local controller account**, not a UniFi Cloud/SSO account.
|
|
165
|
+
* Set `is_udm_pro=True` when connecting to UniFi OS based controllers (like UDM Pro, UDM SE, Cloud Key Gen2 Plus running UniFi OS). Set to `False` for software-based controllers or older Cloud Keys not running UniFi OS.
|
|
166
|
+
* For `verify_ssl`, keep `True` for a normally trusted certificate, or provide an approved CA bundle path for a private CA. `False` disables certificate verification and is not a safe setup default.
|
|
167
|
+
* `auto_model_mapping` (default `True`) automatically translates device model codes (e.g., "U7PG2") to human-readable names (e.g., "UniFi AC Pro AP").
|
|
168
|
+
* `auth_retry_enabled` (default `True`) allows the client to automatically retry authentication on failure, useful for temporary network issues.
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
## Firewall Rules
|
|
173
|
+
|
|
174
|
+
Firewall rule support follows the same raw/typed pattern as the rest of the client:
|
|
175
|
+
|
|
176
|
+
```python
|
|
177
|
+
rules = controller.get_unifi_site_firewallrule("default", raw=False)
|
|
178
|
+
for rule in rules:
|
|
179
|
+
print(rule.name, rule.ruleset, rule.action, rule.protocol, rule.dst_port)
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Mutating helpers are available for callers that deliberately want to manage rules:
|
|
183
|
+
|
|
184
|
+
```python
|
|
185
|
+
controller.create_unifi_site_firewallrule(
|
|
186
|
+
"default",
|
|
187
|
+
{
|
|
188
|
+
"name": "Block example traffic",
|
|
189
|
+
"enabled": True,
|
|
190
|
+
"action": "drop",
|
|
191
|
+
"ruleset": "WAN_OUT",
|
|
192
|
+
"protocol": "tcp",
|
|
193
|
+
"dst_port": "25",
|
|
194
|
+
},
|
|
195
|
+
)
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
> **Caution:** Firewall rule endpoints are private UniFi Network APIs. Valid fields,
|
|
199
|
+
> rule index conventions, and controller-side validation can vary across UniFi
|
|
200
|
+
> Network versions. Prefer read-only listing first, keep writes explicit, and inspect
|
|
201
|
+
> `UnifiAPIError.response_json` when the controller rejects a payload.
|
|
202
|
+
|
|
203
|
+
---
|
|
204
|
+
|
|
205
|
+
## Data Models
|
|
206
|
+
|
|
207
|
+
The library automatically maps JSON API responses to Python data classes located in `unifi_controller_api.models`. Key models include:
|
|
208
|
+
|
|
209
|
+
* `UnifiSite`: Represents a controller site.
|
|
210
|
+
* `UnifiDevice`: Represents a network device (AP, Switch, Gateway).
|
|
211
|
+
* `LLDPEntry`: Holds LLDP neighbor details (nested within `UnifiDevice`).
|
|
212
|
+
* `UnifiClient`: Represents a connected client (wired or wireless).
|
|
213
|
+
* `UnifiWlanConf`: Represents a Wireless LAN configuration.
|
|
214
|
+
* `UnifiNetworkConf`: Represents a Network configuration.
|
|
215
|
+
* `UnifiFirewallRule`: Represents a firewall rule.
|
|
216
|
+
* `UnifiAlarm`: Represents a controller alarm.
|
|
217
|
+
* `UnifiEvent`: Represents a controller event.
|
|
218
|
+
* `UnifiRogueAp`: Represents a detected rogue access point.
|
|
219
|
+
|
|
220
|
+
Use standard object attribute access (e.g., `device.ip`, `site.desc`) to work with the data.
|
|
221
|
+
|
|
222
|
+
---
|
|
223
|
+
|
|
224
|
+
## Logging
|
|
225
|
+
|
|
226
|
+
Uses standard Python `logging`. Start with ordinary informational logging:
|
|
227
|
+
|
|
228
|
+
```python
|
|
229
|
+
import logging
|
|
230
|
+
logging.basicConfig(level=logging.INFO)
|
|
231
|
+
logging.getLogger("unifi_controller_api").setLevel(logging.INFO)
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
Review the installed version's logging before enabling DEBUG: older releases can
|
|
235
|
+
log authentication payloads, and error messages can include controller responses
|
|
236
|
+
or private URLs. Even INFO output can identify sites/devices. For agent setup,
|
|
237
|
+
prefer the skill's guarded example, which suppresses SDK logs while credentials
|
|
238
|
+
and responses are in scope and emits only an allowlisted summary.
|
|
239
|
+
|
|
240
|
+
---
|
|
241
|
+
|
|
242
|
+
## Disclaimer
|
|
243
|
+
|
|
244
|
+
This is an unofficial library using APIs that may change without notice. Not affiliated with Ubiquiti Inc. Use at your own risk.
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
# unifi-controller-api
|
|
2
|
+
|
|
3
|
+
A Python client library for interacting with Ubiquiti UniFi Network Controllers.
|
|
4
|
+
|
|
5
|
+
[](https://pypi.org/project/unifi-controller-api/)
|
|
6
|
+
[](https://pypi.org/project/unifi-controller-api/)
|
|
7
|
+
|
|
8
|
+
**Documentation:** <https://tnware.github.io/unifi-controller-api/>: guides, a method index with the controller
|
|
9
|
+
endpoint each method calls, and the generated Python API reference.
|
|
10
|
+
|
|
11
|
+
> **Warning:** This package is under active development and is subject to breaking changes, incomplete or incorrect documentation, etc.
|
|
12
|
+
|
|
13
|
+
## Relationship to the official UniFi API
|
|
14
|
+
|
|
15
|
+
Ubiquiti provides an [official UniFi API reference](https://developer.ui.com/),
|
|
16
|
+
including the [UniFi Network API](https://developer.ui.com/network).
|
|
17
|
+
There is substantial overlap with this library; consult the official documentation
|
|
18
|
+
when choosing an API for a new integration.
|
|
19
|
+
|
|
20
|
+
This project is an **unofficial local-controller client**, not Ubiquiti's official
|
|
21
|
+
SDK. It currently authenticates with a local username/password and uses the
|
|
22
|
+
controller's private APIs, rather than the official API-key authentication flow.
|
|
23
|
+
It provides convenience methods, optional typed models, and access to additional
|
|
24
|
+
controller endpoints that are not necessarily covered by the official API.
|
|
25
|
+
Availability and behavior depend on the controller and Network application version.
|
|
26
|
+
|
|
27
|
+
Existing methods retain their private-API behavior: an overlapping official endpoint
|
|
28
|
+
does not mean that a method uses it or silently switches to it. Do not assume that
|
|
29
|
+
legacy settings describe every modern policy; these may be separate resources on
|
|
30
|
+
newer controllers.
|
|
31
|
+
|
|
32
|
+
## Core Features
|
|
33
|
+
|
|
34
|
+
* **Structured Data Models:** Optionally parses API responses into typed Python objects (e.g., `UnifiDevice`, `UnifiSite`, `UnifiClient`, `LLDPEntry`, `UnifiWlanConf`).
|
|
35
|
+
* **Automatic Model Mapping:** Translates model codes (e.g., `U7PG2`) to friendly names ("UniFi® AC Pro AP") via the `model_name` attribute.
|
|
36
|
+
* **Convenience Methods:** Includes helpers for data export (`export_csv`, `export_json`).
|
|
37
|
+
* **Minimal Dependencies:** Requires only `requests`.
|
|
38
|
+
* **Python >= 3.9**
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## Installation
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pip install unifi-controller-api
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## Use with an AI agent
|
|
51
|
+
|
|
52
|
+
The repository ships an [Agent Skills](https://agentskills.io/)-compatible
|
|
53
|
+
[skill](skills/unifi-controller-api/SKILL.md) that tells an agent how to install the
|
|
54
|
+
package, connect and read safely, with a guarded read-only example. Paste this prompt
|
|
55
|
+
into your agent:
|
|
56
|
+
|
|
57
|
+
```text
|
|
58
|
+
Help me set up or use the unifi-controller-api Python package with my UniFi controller.
|
|
59
|
+
Follow its agent skill and the references next to it, from the same revision:
|
|
60
|
+
https://github.com/tnware/unifi-controller-api/blob/main/skills/unifi-controller-api/SKILL.md
|
|
61
|
+
Use the docs for agents: https://tnware.github.io/unifi-controller-api/llms.txt
|
|
62
|
+
Keep TLS verification enabled, never ask me to paste passwords into chat, and make no
|
|
63
|
+
changes to my controller or network without separate approval.
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
To pin a tag or commit, replace `main` in the URL. Installing the skill doesn't install
|
|
67
|
+
the package or grant network access; review the skill and its scripts before running them.
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
71
|
+
## Quick Start
|
|
72
|
+
|
|
73
|
+
The example below matches this source checkout. Older published artifacts may
|
|
74
|
+
lack newer arguments such as `request_timeout`; inspect the installed interface
|
|
75
|
+
or use the skill's guarded compatibility example before connecting.
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
from unifi_controller_api import UnifiController
|
|
79
|
+
|
|
80
|
+
# 1. Initialize & Authenticate
|
|
81
|
+
controller = UnifiController(
|
|
82
|
+
controller_url="https://<CONTROLLER_IP_OR_HOSTNAME>", # Use :443 for UniFi OS, :8443 for legacy
|
|
83
|
+
username="<LOCAL_ADMIN_USER>",
|
|
84
|
+
password="<PASSWORD>",
|
|
85
|
+
is_udm_pro=True, # Set True for UniFi OS devices (UDM, Cloud Key Gen2+), False for legacy software/hardware controllers
|
|
86
|
+
verify_ssl=True, # Recommended; use a trusted CA bundle path for private certificates
|
|
87
|
+
auto_model_mapping=True, # Optional: Attempt to map device model codes to friendly names
|
|
88
|
+
model_db_path=None, # Optional: Path to a custom model database file
|
|
89
|
+
auth_retry_enabled=True, # Optional: Enable automatic retries on authentication failure
|
|
90
|
+
auth_retry_count=3, # Optional: Number of authentication retries
|
|
91
|
+
auth_retry_delay=5, # Optional: Delay in seconds between authentication retries
|
|
92
|
+
request_timeout=30 # Optional: Timeout in seconds for controller HTTP requests
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
# 2. Fetch Data (Example: Devices for the 'default' site)
|
|
96
|
+
site_name = "default" # Use the internal site name
|
|
97
|
+
try:
|
|
98
|
+
devices = controller.get_unifi_site_device(site_name=site_name, detailed=True, raw=False)
|
|
99
|
+
|
|
100
|
+
# 3. Use the Typed Data
|
|
101
|
+
for device in devices:
|
|
102
|
+
print(f"- {device.name} ({device.model_name}): {device.ip} / {device.mac}")
|
|
103
|
+
if device.lldp_info:
|
|
104
|
+
print(f" LLDP: {len(device.lldp_info)} neighbors")
|
|
105
|
+
|
|
106
|
+
except Exception:
|
|
107
|
+
# Exception strings can include controller-reflected data and private URLs.
|
|
108
|
+
print("Device discovery failed; inspect only approved, sanitized diagnostics.")
|
|
109
|
+
|
|
110
|
+
# Other available methods:
|
|
111
|
+
# sites = controller.get_unifi_site(include_health=False)
|
|
112
|
+
# clients = controller.get_unifi_site_client(site_name)
|
|
113
|
+
# wlans = controller.get_unifi_site_wlanconf(site_name)
|
|
114
|
+
# alarms = controller.get_unifi_site_alarm(site_name)
|
|
115
|
+
# events = controller.get_unifi_site_event(site_name)
|
|
116
|
+
# rogue_aps = controller.get_unifi_site_rogueap(site_name)
|
|
117
|
+
# networks = controller.get_unifi_site_networkconf(site_name)
|
|
118
|
+
# firewall_rules = controller.get_unifi_site_firewallrule(site_name)
|
|
119
|
+
# report = controller.devices_report(site_names=['site1', 'site2'])
|
|
120
|
+
|
|
121
|
+
# Exporting data:
|
|
122
|
+
# from unifi_controller_api.export import export_csv, export_json
|
|
123
|
+
# if devices:
|
|
124
|
+
# export_csv(devices, "devices.csv")
|
|
125
|
+
# export_json(devices, "devices.json")
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
## Connectivity Notes
|
|
131
|
+
|
|
132
|
+
* Requires direct network access to the controller.
|
|
133
|
+
* The `controller_url` should include the scheme (https) and port (e.g., `https://192.168.1.1:443` for UniFi OS, `https://unifi.example.com:8443` for older software controllers).
|
|
134
|
+
* Requires direct network access to the controller.
|
|
135
|
+
* Uses **HTTPS port 443** by default for UniFi OS devices (UDM, Cloud Key Gen2+).
|
|
136
|
+
* Uses **HTTPS port 8443** by default for legacy controllers (Software, Cloud Key Gen1).
|
|
137
|
+
* Requires a **local controller account**, not a UniFi Cloud/SSO account.
|
|
138
|
+
* Set `is_udm_pro=True` when connecting to UniFi OS based controllers (like UDM Pro, UDM SE, Cloud Key Gen2 Plus running UniFi OS). Set to `False` for software-based controllers or older Cloud Keys not running UniFi OS.
|
|
139
|
+
* For `verify_ssl`, keep `True` for a normally trusted certificate, or provide an approved CA bundle path for a private CA. `False` disables certificate verification and is not a safe setup default.
|
|
140
|
+
* `auto_model_mapping` (default `True`) automatically translates device model codes (e.g., "U7PG2") to human-readable names (e.g., "UniFi AC Pro AP").
|
|
141
|
+
* `auth_retry_enabled` (default `True`) allows the client to automatically retry authentication on failure, useful for temporary network issues.
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
## Firewall Rules
|
|
146
|
+
|
|
147
|
+
Firewall rule support follows the same raw/typed pattern as the rest of the client:
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
rules = controller.get_unifi_site_firewallrule("default", raw=False)
|
|
151
|
+
for rule in rules:
|
|
152
|
+
print(rule.name, rule.ruleset, rule.action, rule.protocol, rule.dst_port)
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Mutating helpers are available for callers that deliberately want to manage rules:
|
|
156
|
+
|
|
157
|
+
```python
|
|
158
|
+
controller.create_unifi_site_firewallrule(
|
|
159
|
+
"default",
|
|
160
|
+
{
|
|
161
|
+
"name": "Block example traffic",
|
|
162
|
+
"enabled": True,
|
|
163
|
+
"action": "drop",
|
|
164
|
+
"ruleset": "WAN_OUT",
|
|
165
|
+
"protocol": "tcp",
|
|
166
|
+
"dst_port": "25",
|
|
167
|
+
},
|
|
168
|
+
)
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
> **Caution:** Firewall rule endpoints are private UniFi Network APIs. Valid fields,
|
|
172
|
+
> rule index conventions, and controller-side validation can vary across UniFi
|
|
173
|
+
> Network versions. Prefer read-only listing first, keep writes explicit, and inspect
|
|
174
|
+
> `UnifiAPIError.response_json` when the controller rejects a payload.
|
|
175
|
+
|
|
176
|
+
---
|
|
177
|
+
|
|
178
|
+
## Data Models
|
|
179
|
+
|
|
180
|
+
The library automatically maps JSON API responses to Python data classes located in `unifi_controller_api.models`. Key models include:
|
|
181
|
+
|
|
182
|
+
* `UnifiSite`: Represents a controller site.
|
|
183
|
+
* `UnifiDevice`: Represents a network device (AP, Switch, Gateway).
|
|
184
|
+
* `LLDPEntry`: Holds LLDP neighbor details (nested within `UnifiDevice`).
|
|
185
|
+
* `UnifiClient`: Represents a connected client (wired or wireless).
|
|
186
|
+
* `UnifiWlanConf`: Represents a Wireless LAN configuration.
|
|
187
|
+
* `UnifiNetworkConf`: Represents a Network configuration.
|
|
188
|
+
* `UnifiFirewallRule`: Represents a firewall rule.
|
|
189
|
+
* `UnifiAlarm`: Represents a controller alarm.
|
|
190
|
+
* `UnifiEvent`: Represents a controller event.
|
|
191
|
+
* `UnifiRogueAp`: Represents a detected rogue access point.
|
|
192
|
+
|
|
193
|
+
Use standard object attribute access (e.g., `device.ip`, `site.desc`) to work with the data.
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
## Logging
|
|
198
|
+
|
|
199
|
+
Uses standard Python `logging`. Start with ordinary informational logging:
|
|
200
|
+
|
|
201
|
+
```python
|
|
202
|
+
import logging
|
|
203
|
+
logging.basicConfig(level=logging.INFO)
|
|
204
|
+
logging.getLogger("unifi_controller_api").setLevel(logging.INFO)
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
Review the installed version's logging before enabling DEBUG: older releases can
|
|
208
|
+
log authentication payloads, and error messages can include controller responses
|
|
209
|
+
or private URLs. Even INFO output can identify sites/devices. For agent setup,
|
|
210
|
+
prefer the skill's guarded example, which suppresses SDK logs while credentials
|
|
211
|
+
and responses are in scope and emits only an allowlisted summary.
|
|
212
|
+
|
|
213
|
+
---
|
|
214
|
+
|
|
215
|
+
## Disclaimer
|
|
216
|
+
|
|
217
|
+
This is an unofficial library using APIs that may change without notice. Not affiliated with Ubiquiti Inc. Use at your own risk.
|
|
@@ -4,14 +4,13 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "unifi-controller-api"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.4.0"
|
|
8
8
|
description = "A Python library for interacting with the Unifi Controller API"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
authors = [{ name = "Tyler Woods", email = "tyler@tylermade.net" }]
|
|
11
|
-
license =
|
|
11
|
+
license = "MIT"
|
|
12
12
|
classifiers = [
|
|
13
13
|
"Programming Language :: Python :: 3",
|
|
14
|
-
"License :: OSI Approved :: MIT License",
|
|
15
14
|
"Operating System :: OS Independent",
|
|
16
15
|
"Topic :: System :: Networking :: Monitoring",
|
|
17
16
|
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
@@ -25,11 +24,11 @@ dependencies = ["requests"]
|
|
|
25
24
|
"Homepage" = "https://github.com/tnware/unifi-controller-api"
|
|
26
25
|
"Bug Tracker" = "https://github.com/tnware/unifi-controller-api/issues"
|
|
27
26
|
|
|
28
|
-
[tool.setuptools]
|
|
29
|
-
|
|
27
|
+
[tool.setuptools.packages.find]
|
|
28
|
+
include = ["unifi_controller_api*"]
|
|
30
29
|
|
|
31
30
|
[tool.setuptools.package-data]
|
|
32
|
-
"unifi_controller_api" = ["
|
|
31
|
+
"unifi_controller_api" = ["device-models.json"]
|
|
33
32
|
|
|
34
33
|
[project.optional-dependencies]
|
|
35
34
|
dev = [
|
|
@@ -37,10 +36,18 @@ dev = [
|
|
|
37
36
|
"pytest-cov>=2.12",
|
|
38
37
|
"pytest-mock>=3.10.0",
|
|
39
38
|
"python-dotenv>=1.0.0",
|
|
40
|
-
"ruff
|
|
39
|
+
"ruff==0.17.6",
|
|
41
40
|
]
|
|
41
|
+
# The docs site (docs/) is Astro Starlight. Its API reference is extracted with
|
|
42
|
+
# Griffe, run through `uvx` when uv is installed or from this extra otherwise.
|
|
42
43
|
docs = [
|
|
43
|
-
"
|
|
44
|
-
"sphinx-rtd-theme>=3.0.2; python_version >= '3.11'",
|
|
45
|
-
"sphinx-autodoc-typehints>=3.1.0; python_version >= '3.11'",
|
|
44
|
+
"griffe>=2.0; python_version >= '3.11'",
|
|
46
45
|
]
|
|
46
|
+
|
|
47
|
+
[tool.ruff]
|
|
48
|
+
target-version = "py39"
|
|
49
|
+
|
|
50
|
+
[tool.ruff.lint]
|
|
51
|
+
# Keep the existing baseline explicit as Ruff's defaults evolve. Broader rule
|
|
52
|
+
# adoption should be deliberate, not an incidental development-tool upgrade.
|
|
53
|
+
select = ["E4", "E7", "E9", "F"]
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: unifi-controller-api
|
|
3
|
+
description: Set up and use the Python UniFi client safely.
|
|
4
|
+
license: MIT
|
|
5
|
+
compatibility: Python 3.9+ with unifi-controller-api installed; live use requires authorized network access and a local controller account.
|
|
6
|
+
metadata:
|
|
7
|
+
author: tnware
|
|
8
|
+
version: "1.1"
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
# UniFi Controller API
|
|
12
|
+
|
|
13
|
+
## When to use
|
|
14
|
+
|
|
15
|
+
Use this skill when asked to install, connect to, diagnose, or automate a UniFi
|
|
16
|
+
Network controller using the `unifi-controller-api` Python package. It is an
|
|
17
|
+
agent-neutral procedure: no particular assistant, secret manager, shell, or
|
|
18
|
+
private environment is required. If skill installation is unavailable, read this
|
|
19
|
+
file and its linked resources directly from the same repository revision.
|
|
20
|
+
|
|
21
|
+
This is an **unofficial local/private-controller client** using local account
|
|
22
|
+
credentials. It is not the official API-key SDK. Consult the
|
|
23
|
+
[official UniFi API](https://developer.ui.com/) when choosing an integration; do
|
|
24
|
+
not silently switch authentication, endpoints, IDs, pagination or return shapes
|
|
25
|
+
because an official resource has a similar name.
|
|
26
|
+
|
|
27
|
+
## Operating contract
|
|
28
|
+
|
|
29
|
+
1. Establish the requested outcome and authorization. Setup and investigation
|
|
30
|
+
authorize dependency installation only in the agreed environment, authentication
|
|
31
|
+
and selected reads—not firewall changes, adoption, provisioning, firmware,
|
|
32
|
+
restarts, credential rotation or configuration experiments.
|
|
33
|
+
2. Establish the controller origin, UniFi OS versus legacy mode, site scope and
|
|
34
|
+
certificate trust. Obtain missing nonsecret settings from the operator. Never
|
|
35
|
+
guess credentials, request passwords in chat or put them in command arguments.
|
|
36
|
+
Use an approved secret-store handoff or a local password prompt.
|
|
37
|
+
3. Inspect the installed distribution version, constructor and relevant methods
|
|
38
|
+
**before connecting**: construction authenticates immediately. A source checkout
|
|
39
|
+
can declare the same version as an older release; record its Git revision too.
|
|
40
|
+
Never imply merged changes are available on PyPI without checking the artifact.
|
|
41
|
+
4. Use an isolated environment. Keep TLS verification enabled; use the correct
|
|
42
|
+
hostname and a trusted CA bundle for private certificates. Do not default to
|
|
43
|
+
`verify_ssl=False` or disable warnings to get past certificate failures.
|
|
44
|
+
5. Use finite HTTP timeouts and bounded attempts. Prefer `request_timeout` when
|
|
45
|
+
supported. Older releases lack that constructor option: do not pass an invented
|
|
46
|
+
argument or silently continue with unbounded authentication. The accompanying
|
|
47
|
+
isolated CLI example enforces timeouts at the transport boundary on both paths.
|
|
48
|
+
6. Start with basic read-only discovery. Use actual internal site names, not UI
|
|
49
|
+
display labels, and never silently pick the first of several sites. Only read
|
|
50
|
+
what is necessary; prefer package helpers over another login implementation.
|
|
51
|
+
7. Whitelist output before it reaches logs, chat or files. Avoid full responses,
|
|
52
|
+
cookies, headers, credentials, VPN/network objects, client inventories, MACs,
|
|
53
|
+
serials and device names/addresses unless specifically required and authorized.
|
|
54
|
+
Error strings can contain URLs or controller-reflected values too.
|
|
55
|
+
8. Keep mutations separately authorized, scoped and verified. Follow
|
|
56
|
+
[change safety](references/change-safety.md); discovery success grants no write
|
|
57
|
+
permission and an SDK method name is not a safety boundary.
|
|
58
|
+
|
|
59
|
+
## First successful connection
|
|
60
|
+
|
|
61
|
+
Follow [setup and compatibility](references/setup.md). Inspect the complete skill
|
|
62
|
+
folder before running supplied code; do not pipe downloaded code into a shell.
|
|
63
|
+
|
|
64
|
+
The commands below assume a repository checkout. If the skill is installed
|
|
65
|
+
elsewhere, resolve its actual directory and run that copy of `scripts/read_only_inventory.py`;
|
|
66
|
+
do not assume the agent's current working directory contains the repository.
|
|
67
|
+
|
|
68
|
+
The included example is deliberately limited to site discovery and basic device
|
|
69
|
+
counts. It blocks all other requests, including redirects and configuration writes:
|
|
70
|
+
|
|
71
|
+
```sh
|
|
72
|
+
python skills/unifi-controller-api/scripts/read_only_inventory.py --check-install
|
|
73
|
+
python skills/unifi-controller-api/scripts/read_only_inventory.py \
|
|
74
|
+
--url https://controller.example.com --mode unifi-os
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
The second command prompts locally for the account and password. For an approved
|
|
78
|
+
noninteractive secret-provider handoff, use `--credentials-stdin`; its contract is
|
|
79
|
+
in [setup](references/setup.md). Do not paste that input into chat or shell history.
|
|
80
|
+
If several sites exist, supply the approved `--site INTERNAL_NAME` explicitly.
|
|
81
|
+
Use `--ca-bundle /path/to/trusted-ca.pem` where appropriate.
|
|
82
|
+
|
|
83
|
+
Report installed version/source identity, verified controller mode/site scope,
|
|
84
|
+
which bounded checks ran, safe summary counts, and remaining limitations. Never
|
|
85
|
+
claim live access from an offline example test or installation-only check.
|
|
86
|
+
|
|
87
|
+
## Correct API use
|
|
88
|
+
|
|
89
|
+
Read [response handling](references/response-handling.md) before expanding beyond
|
|
90
|
+
the example. Key rules:
|
|
91
|
+
|
|
92
|
+
- `get_unifi_site(include_health=False, raw=True)` returns site dictionaries.
|
|
93
|
+
`include_health` has no default. A site's `name` is the internal ID that every
|
|
94
|
+
`site_name` argument takes; `desc` is only its display label.
|
|
95
|
+
- `get_unifi_site_device(site_name=..., detailed=False, raw=False)` returns typed
|
|
96
|
+
device objects; `raw=True` returns dictionaries. Choose explicitly.
|
|
97
|
+
- `invoke_get_rest_api_call(...)` returns a `requests.Response`, not decoded JSON.
|
|
98
|
+
Its `timeout` parameter is also version-dependent. Inspect the installed API.
|
|
99
|
+
- HTTP 200 can still carry `meta.rc == "error"`. Error handling differs by package
|
|
100
|
+
revision. Never interpret an error envelope as a record or empty success.
|
|
101
|
+
- Preserve unknown fields and exact model codes; friendly names can lag new
|
|
102
|
+
hardware. Unknown-code fallback does not establish connection failure.
|
|
103
|
+
- Look a helper up before writing a request. The documentation's
|
|
104
|
+
[method index](https://tnware.github.io/unifi-controller-api/reference/methods/) lists every public
|
|
105
|
+
`UnifiController` method with its HTTP verb, private endpoint and summary, read
|
|
106
|
+
from the documented release's source. Confirm the signature in the installed package.
|
|
107
|
+
- Mind units and identifiers: statistics, speed-test and IPS windows are Unix
|
|
108
|
+
milliseconds, session/authorization windows are seconds, and history lookbacks
|
|
109
|
+
are hours. Connection commands take MAC addresses; record edits take `_id` values.
|
|
110
|
+
See [response handling](references/response-handling.md#identifiers-and-units).
|
|
111
|
+
- Releases up to 0.3.2 have helper defects that later source fixes. Check them in
|
|
112
|
+
[version-specific defects](references/response-handling.md#version-specific-defects)
|
|
113
|
+
before relying on device reports, daily AP statistics or model exports.
|
|
114
|
+
- For missing helpers, first verify an endpoint against the target Network version.
|
|
115
|
+
The leading-underscore authenticated invoker is private/version-bound, not a
|
|
116
|
+
stable public request API. Do not use it casually or bypass CSRF with bare writes.
|
|
117
|
+
|
|
118
|
+
## Troubleshooting and limits
|
|
119
|
+
|
|
120
|
+
Use [troubleshooting](references/troubleshooting.md) for TLS, authentication,
|
|
121
|
+
authorization, endpoint and model-mapping differences. Stop on denied access;
|
|
122
|
+
do not rotate accounts, change roles, scan alternate controllers or try writes.
|
|
123
|
+
Do not enable SDK DEBUG logging on unreviewed credentials/responses just to debug
|
|
124
|
+
an older release. Never print an exception object or traceback indiscriminately.
|
|
125
|
+
|
|
126
|
+
## Resources
|
|
127
|
+
|
|
128
|
+
- [Setup and compatibility](references/setup.md)
|
|
129
|
+
- [Response handling](references/response-handling.md)
|
|
130
|
+
- [Authorized change safety](references/change-safety.md)
|
|
131
|
+
- [Troubleshooting](references/troubleshooting.md)
|
|
132
|
+
- [Executable read-only example](scripts/read_only_inventory.py)
|
|
133
|
+
- [Project README](https://github.com/tnware/unifi-controller-api/blob/main/README.md)
|
|
134
|
+
- [Documentation](https://tnware.github.io/unifi-controller-api/): guides, the generated
|
|
135
|
+
[method index](https://tnware.github.io/unifi-controller-api/reference/methods/) and
|
|
136
|
+
[Python API reference](https://tnware.github.io/unifi-controller-api/api/unifi_controller_api/), also as
|
|
137
|
+
[Markdown](https://tnware.github.io/unifi-controller-api/api/unifi_controller_api/llms.txt). The site is
|
|
138
|
+
published from each release and its footer names the version and commit; it can be
|
|
139
|
+
newer than the installed artifact.
|
|
140
|
+
- [llms.txt](https://tnware.github.io/unifi-controller-api/llms.txt): an index of the
|
|
141
|
+
documentation for language models, with abridged and complete text versions.
|
|
142
|
+
|
|
143
|
+
Keep this procedure versioned with the package. Validate examples offline in CI
|
|
144
|
+
and against any claimed released artifacts; live tests require their own scope,
|
|
145
|
+
credentials and transport restrictions. This skill does not grant network access.
|