[unifi] Fixed 404 error with reconnect and no default value set for UniFiOS Checkbox (#11004)

* Fixed 404 error by obtaining Csrf token before login

Signed-off-by: Mathias Maes <watcherwhale@maes.family>

* Only try to obtain a csrf token when using UniFi OS

Signed-off-by: Mathias Maes <watcherwhale@maes.family>

* Updated documentation

Signed-off-by: Mathias Maes <watcherwhale@maes.family>

* Updated thing configuration in README.md

Signed-off-by: Mathias Maes <watcherwhale@maes.family>
This commit is contained in:
Mathias Maes 2021-07-31 18:55:34 +02:00 committed by GitHub
parent a1ec5eb241
commit 9e7f23e921
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 2 deletions

View File

@ -29,6 +29,7 @@ The following table describes the Bridge configuration parameters:
| ------------------------ | ---------------------------------------------- |--------- | ------- | | ------------------------ | ---------------------------------------------- |--------- | ------- |
| host | Hostname of IP address of the UniFi Controller | Required | - | | host | Hostname of IP address of the UniFi Controller | Required | - |
| port | Port of the UniFi Controller | Required | - | | port | Port of the UniFi Controller | Required | - |
| unifios | If the UniFi Controller is running on UniFi OS | Required | false |
| username | The username to access the UniFi Controller | Required | - | | username | The username to access the UniFi Controller | Required | - |
| password | The password to access the UniFi Controller | Required | - | | password | The password to access the UniFi Controller | Required | - |
| refresh | Refresh interval in seconds | Optional | 10 | | refresh | Refresh interval in seconds | Optional | 10 |
@ -106,7 +107,7 @@ The `reconnect` channel allows you to force a client to reconnect. Sending `ON`
things/unifi.things things/unifi.things
``` ```
Bridge unifi:controller:home "UniFi Controller" [ host="unifi", port=8443, username="$username", password="$password", refresh=10 ] { Bridge unifi:controller:home "UniFi Controller" [ host="unifi", port=8443, unifios=false, username="$username", password="$password", refresh=10 ] {
Thing wirelessClient matthewsPhone "Matthew's iPhone" [ cid="$cid", site="default", considerHome=180 ] Thing wirelessClient matthewsPhone "Matthew's iPhone" [ cid="$cid", site="default", considerHome=180 ]
} }
``` ```

View File

@ -94,6 +94,10 @@ public class UniFiController {
// Public API // Public API
public void start() throws UniFiException { public void start() throws UniFiException {
if (unifios) {
obtainCsrfToken();
}
login(); login();
} }
@ -101,9 +105,15 @@ public class UniFiController {
logout(); logout();
} }
public void login() throws UniFiException { public void obtainCsrfToken() throws UniFiException {
csrfToken = ""; csrfToken = "";
UniFiControllerRequest<Void> req = newRequest(Void.class);
req.setPath("/");
executeRequest(req);
}
public void login() throws UniFiException {
UniFiControllerRequest<Void> req = newRequest(Void.class); UniFiControllerRequest<Void> req = newRequest(Void.class);
req.setPath(unifios ? "/api/auth/login" : "/api/login"); req.setPath(unifios ? "/api/auth/login" : "/api/login");
req.setBodyParameter("username", username); req.setBodyParameter("username", username);

View File

@ -24,6 +24,7 @@
<parameter name="unifios" type="boolean" required="true"> <parameter name="unifios" type="boolean" required="true">
<label>UniFi OS</label> <label>UniFi OS</label>
<description>If the UniFi Controller is running on UniFi OS.</description> <description>If the UniFi Controller is running on UniFi OS.</description>
<default>false</default>
</parameter> </parameter>
<parameter name="username" type="text" required="true"> <parameter name="username" type="text" required="true">
<label>Username</label> <label>Username</label>