Skip to content

Commit

Permalink
Avoid need for version increment
Browse files Browse the repository at this point in the history
  • Loading branch information
shamoon committed Mar 1, 2025
1 parent b17c79c commit 158da55
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 32 deletions.
14 changes: 3 additions & 11 deletions docs/widgets/info/unifi_controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,11 @@ An optional 'site' parameter can be supplied, if it is not the widget will use t

<img width="162" alt="unifi_infowidget" src="https://user-images.githubusercontent.com/4887959/197706832-f5a8706b-7282-4892-a666-b7d999752562.png">

Version 2 of the widget supports the Unifi Network API (2024) which requires an API key instead of a username and password. The API key can be generated in the Unifi Controller under Settings > Control Plane > Integrations

| Unifi API | Homepage Widget Version |
| ------------------ | ----------------------- |
| Controller API | 1 (default) |
| Network API (2024) | 2 |

```yaml
- unifi_console:
url: https://unifi.host.or.ip:port
site: Site Name # optional
username: user # version 1
password: pass # version 1
key: unifiapikey # version 2
version: 2 # default is 1
username: user
password: pass
key: unifiapikey # required if using API key instead of username/password
```
14 changes: 3 additions & 11 deletions docs/widgets/services/unifi-controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,12 @@ Allowed fields: `["uptime", "wan", "lan", "lan_users", "lan_devices", "wlan", "w

If you enter e.g. incorrect credentials and receive an "API Error", you may need to recreate the container or restart the service to clear the cache.

Version 2 of the widget supports the Unifi Network API (2024) which requires an API key instead of a username and password. The API key can be generated in the Unifi Controller under Settings > Control Plane > Integrations

| Unifi API | Homepage Widget Version |
| ------------------ | ----------------------- |
| Controller API | 1 (default) |
| Network API (2024) | 2 |

```yaml
widget:
type: unifi
url: https://unifi.host.or.ip:port
site: Site Name # optional
username: user # version 1
password: pass # version 1
key: unifiapikey # version 2
version: 2 # default is 1
username: user
password: pass
key: unifiapikey # required if using API key instead of username/password
```
4 changes: 2 additions & 2 deletions src/utils/config/service-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ export function cleanServiceGroups(groups) {
// frigate
enableRecentEvents,

// beszel, glances, immich, mealie, pihole, pfsense, speedtest, unifi
// beszel, glances, immich, mealie, pihole, pfsense, speedtest
version,

// glances
Expand Down Expand Up @@ -482,7 +482,7 @@ export function cleanServiceGroups(groups) {
if (snapshotHost) widget.snapshotHost = snapshotHost;
if (snapshotPath) widget.snapshotPath = snapshotPath;
}
if (["beszel", "glances", "immich", "mealie", "pfsense", "pihole", "speedtest", "unifi"].includes(type)) {
if (["beszel", "glances", "immich", "mealie", "pfsense", "pihole", "speedtest"].includes(type)) {
if (version) widget.version = parseInt(version, 10);
}
if (type === "glances") {
Expand Down
3 changes: 1 addition & 2 deletions src/utils/config/widget-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function getPrivateWidgetOptions(type, widgetIndex) {
const widgets = await widgetsFromConfig();

const privateOptions = widgets.map((widget) => {
const { index, url, username, password, key, apiKey, version } = widget.options;
const { index, url, username, password, key, apiKey } = widget.options;

return {
type: widget.type,
Expand All @@ -68,7 +68,6 @@ export async function getPrivateWidgetOptions(type, widgetIndex) {
password,
key,
apiKey,
version,
},
};
});
Expand Down
12 changes: 6 additions & 6 deletions src/widgets/unifi/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ export default async function unifiProxyHandler(req, res) {

let [status, contentType, data, responseHeaders] = [];
let prefix = cache.get(`${prefixCacheKey}.${service}`);
const headers = {};
let csrfToken;
if (widget.version === 2) {
prefix = "/proxy/network";
const headers = {};
if (widget.key) {
prefix = udmpPrefix;
headers["X-API-KEY"] = widget.key;
headers["Content-Type"] = "application/json";
} else if (prefix === null) {
// auto detect if we're talking to a UDM Pro, and cache the result so that we
// don't make two requests each time data from Unifi is required
// auto detect if we're talking to a UDM Pro or Network API device, and cache the result
// so that we don't make two requests each time data from Unifi is required
[status, contentType, data, responseHeaders] = await httpProxy(widget.url);
prefix = "";
if (responseHeaders?.["x-csrf-token"]) {
Expand All @@ -104,7 +104,7 @@ export default async function unifiProxyHandler(req, res) {

[status, contentType, data, responseHeaders] = await httpProxy(url, params);

if (status === 401 && widget.version < 2) {
if (status === 401 && !widget.key) {
logger.debug("Unifi isn't logged in or rejected the reqeust, attempting login.");
if (responseHeaders?.["x-csrf-token"]) {
csrfToken = responseHeaders["x-csrf-token"];
Expand Down

0 comments on commit 158da55

Please sign in to comment.