From 9e7f23e92123c35c239239460e809d8ff04762c0 Mon Sep 17 00:00:00 2001 From: Mathias Maes Date: Sat, 31 Jul 2021 18:55:34 +0200 Subject: [PATCH] [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 * Only try to obtain a csrf token when using UniFi OS Signed-off-by: Mathias Maes * Updated documentation Signed-off-by: Mathias Maes * Updated thing configuration in README.md Signed-off-by: Mathias Maes --- bundles/org.openhab.binding.unifi/README.md | 3 ++- .../unifi/internal/api/model/UniFiController.java | 12 +++++++++++- .../src/main/resources/OH-INF/thing/thing-types.xml | 1 + 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/bundles/org.openhab.binding.unifi/README.md b/bundles/org.openhab.binding.unifi/README.md index 6fdd0a243..48bfa135e 100644 --- a/bundles/org.openhab.binding.unifi/README.md +++ b/bundles/org.openhab.binding.unifi/README.md @@ -29,6 +29,7 @@ The following table describes the Bridge configuration parameters: | ------------------------ | ---------------------------------------------- |--------- | ------- | | host | Hostname of IP address 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 | - | | password | The password to access the UniFi Controller | Required | - | | 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 ``` -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 ] } ``` diff --git a/bundles/org.openhab.binding.unifi/src/main/java/org/openhab/binding/unifi/internal/api/model/UniFiController.java b/bundles/org.openhab.binding.unifi/src/main/java/org/openhab/binding/unifi/internal/api/model/UniFiController.java index 6dbdc35c8..639a07f7f 100644 --- a/bundles/org.openhab.binding.unifi/src/main/java/org/openhab/binding/unifi/internal/api/model/UniFiController.java +++ b/bundles/org.openhab.binding.unifi/src/main/java/org/openhab/binding/unifi/internal/api/model/UniFiController.java @@ -94,6 +94,10 @@ public class UniFiController { // Public API public void start() throws UniFiException { + if (unifios) { + obtainCsrfToken(); + } + login(); } @@ -101,9 +105,15 @@ public class UniFiController { logout(); } - public void login() throws UniFiException { + public void obtainCsrfToken() throws UniFiException { csrfToken = ""; + UniFiControllerRequest req = newRequest(Void.class); + req.setPath("/"); + executeRequest(req); + } + + public void login() throws UniFiException { UniFiControllerRequest req = newRequest(Void.class); req.setPath(unifios ? "/api/auth/login" : "/api/login"); req.setBodyParameter("username", username); diff --git a/bundles/org.openhab.binding.unifi/src/main/resources/OH-INF/thing/thing-types.xml b/bundles/org.openhab.binding.unifi/src/main/resources/OH-INF/thing/thing-types.xml index 7632c4b5a..87069a33c 100644 --- a/bundles/org.openhab.binding.unifi/src/main/resources/OH-INF/thing/thing-types.xml +++ b/bundles/org.openhab.binding.unifi/src/main/resources/OH-INF/thing/thing-types.xml @@ -24,6 +24,7 @@ If the UniFi Controller is running on UniFi OS. + false