From bfd1b944732431543b86ebbef916dc5663e373c0 Mon Sep 17 00:00:00 2001 From: BWS Systems Date: Thu, 13 Jun 2019 15:51:49 -0500 Subject: [PATCH] Start adding new Bearer Token for Home Assistant --- .../HABridge/plugins/hass/HassAuth.java | 13 +++ .../HABridge/plugins/hass/HomeAssistant.java | 80 +++++++++++-------- src/main/resources/public/scripts/app.js | 5 +- src/main/resources/public/views/system.html | 19 ++++- 4 files changed, 79 insertions(+), 38 deletions(-) create mode 100644 src/main/java/com/bwssystems/HABridge/plugins/hass/HassAuth.java diff --git a/src/main/java/com/bwssystems/HABridge/plugins/hass/HassAuth.java b/src/main/java/com/bwssystems/HABridge/plugins/hass/HassAuth.java new file mode 100644 index 0000000..2291f36 --- /dev/null +++ b/src/main/java/com/bwssystems/HABridge/plugins/hass/HassAuth.java @@ -0,0 +1,13 @@ +package com.bwssystems.HABridge.plugins.hass; + +public class HassAuth { + boolean legacyauth; + + public boolean isLegacyauth() { + return legacyauth; + } + + public void setLegacyauth(boolean legacyauth) { + this.legacyauth = legacyauth; + } +} \ No newline at end of file diff --git a/src/main/java/com/bwssystems/HABridge/plugins/hass/HomeAssistant.java b/src/main/java/com/bwssystems/HABridge/plugins/hass/HomeAssistant.java index cc66f51..b5d40d6 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/hass/HomeAssistant.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/hass/HomeAssistant.java @@ -16,14 +16,17 @@ import com.bwssystems.HABridge.plugins.http.HTTPHome; import com.google.gson.Gson; public class HomeAssistant { - private static final Logger log = LoggerFactory.getLogger(HomeAssistant.class); - private NamedIP hassAddress; - private HTTPHandler anHttpHandler; + private static final Logger log = LoggerFactory.getLogger(HomeAssistant.class); + private NamedIP hassAddress; + private HTTPHandler anHttpHandler; + private HassAuth theAuthType; public HomeAssistant(NamedIP addressName) { super(); anHttpHandler = HTTPHome.getHandler(); - hassAddress = addressName; + hassAddress = addressName; + theAuthType = null; + isLegacyAuth(); } public NamedIP getHassAddress() { @@ -35,38 +38,37 @@ public class HomeAssistant { } public Boolean callCommand(HassCommand aCommand) { - log.debug("calling HomeAssistant: " + aCommand.getHassName() + " - " - + aCommand.getEntityId() + " - " + aCommand.getState() + " - " + aCommand.getBri()); + log.debug("calling HomeAssistant: " + aCommand.getHassName() + " - " + aCommand.getEntityId() + " - " + + aCommand.getState() + " - " + aCommand.getBri()); String aUrl = null; String domain = aCommand.getEntityId().substring(0, aCommand.getEntityId().indexOf(".")); aUrl = hassAddress.getHttpPreamble() + "/api/services/"; - if(domain.equals("group")) + if (domain.equals("group")) aUrl = aUrl + "homeassistant"; else aUrl = aUrl + domain; String aBody = "{\"entity_id\":\"" + aCommand.getEntityId() + "\""; NameValue[] headers = null; - if(hassAddress.getPassword() != null && !hassAddress.getPassword().isEmpty()) { + if (hassAddress.getPassword() != null && !hassAddress.getPassword().isEmpty()) { NameValue password = new NameValue(); password.setName("x-ha-access"); password.setValue(hassAddress.getPassword()); headers = new NameValue[1]; headers[0] = password; } - if(aCommand.getState().equalsIgnoreCase("on")) { + if (aCommand.getState().equalsIgnoreCase("on")) { aUrl = aUrl + "/turn_on"; - if(aCommand.getBri() != null) + if (aCommand.getBri() != null) aBody = aBody + ",\"brightness\":" + aCommand.getBri() + "}"; else aBody = aBody + "}"; - } - else { + } else { aUrl = aUrl + "/turn_off"; - aBody = aBody + "}"; + aBody = aBody + "}"; } log.debug("Calling HomeAssistant with url: " + aUrl); - String theData = anHttpHandler.doHttpRequest(aUrl, HttpPost.METHOD_NAME, "application/json", aBody, headers); - log.debug("call Command return is: <" + theData + ">"); + String theData = anHttpHandler.doHttpRequest(aUrl, HttpPost.METHOD_NAME, "application/json", aBody, headers); + log.debug("call Command return is: <" + theData + ">"); return true; } @@ -74,39 +76,51 @@ public class HomeAssistant { List theDeviceStates = null; State[] theHassStates; String theUrl = null; - String theData; + String theData; NameValue[] headers = null; - if(hassAddress.getPassword() != null && !hassAddress.getPassword().isEmpty()) { + // do check for what type of auth: bearer token or legacy password + if (hassAddress.getPassword() != null && !hassAddress.getPassword().isEmpty()) { NameValue password = new NameValue(); password.setName("x-ha-access"); password.setValue(hassAddress.getPassword()); headers = new NameValue[1]; headers[0] = password; } - if(hassAddress.getSecure() != null && hassAddress.getSecure()) + if (hassAddress.getSecure() != null && hassAddress.getSecure()) theUrl = "https"; else theUrl = "http"; - theUrl = theUrl + "://" + hassAddress.getIp() + ":" + hassAddress.getPort() + "/api/states"; - theData = anHttpHandler.doHttpRequest(theUrl, HttpGet.METHOD_NAME, "application/json", null, headers); - if(theData != null) { - log.debug("GET Hass States - data: " + theData); - theHassStates = new Gson().fromJson(theData, State[].class); - if(theHassStates == null) { - log.warn("Cannot get an devices for HomeAssistant " + hassAddress.getName() + " as response is not parsable."); - } - else { - theDeviceStates = new ArrayList(Arrays.asList(theHassStates)); - } - } - else - log.warn("Cannot get an devices for HomeAssistant " + hassAddress.getName() + " http call failed."); + theUrl = theUrl + "://" + hassAddress.getIp() + ":" + hassAddress.getPort() + "/api/states"; + theData = anHttpHandler.doHttpRequest(theUrl, HttpGet.METHOD_NAME, "application/json", null, headers); + if (theData != null) { + log.debug("GET Hass States - data: " + theData); + theHassStates = new Gson().fromJson(theData, State[].class); + if (theHassStates == null) { + log.warn("Cannot get an devices for HomeAssistant " + hassAddress.getName() + + " as response is not parsable."); + } else { + theDeviceStates = new ArrayList(Arrays.asList(theHassStates)); + } + } else + log.warn("Cannot get an devices for HomeAssistant " + hassAddress.getName() + " http call failed."); return theDeviceStates; } - protected void closeClient() { anHttpHandler.closeHandler(); anHttpHandler = null; } + + private boolean isLegacyAuth() { + if (theAuthType == null) { + try { + theAuthType = new Gson().fromJson(hassAddress.getExtensions(), HassAuth.class); + } catch (Exception e) { + log.warn("Could not read hass auth - continuing with defaults."); + theAuthType = new HassAuth(); + theAuthType.setLegacyauth(false); + } + } + return theAuthType.isLegacyauth(); + } } diff --git a/src/main/resources/public/scripts/app.js b/src/main/resources/public/scripts/app.js index 7c59837..60a5722 100644 --- a/src/main/resources/public/scripts/app.js +++ b/src/main/resources/public/scripts/app.js @@ -1875,7 +1875,7 @@ app.controller('SystemController', function ($scope, $location, bridgeService, n } } }; - $scope.addHasstoSettings = function (newhassname, newhassip, newhassport, newhasspassword, newhasssecure) { + $scope.addHasstoSettings = function (newhassname, newhassip, newhassport, newhasspassword, newhasssecure, newhassauth) { if ($scope.bridge.settings.hassaddress === undefined || $scope.bridge.settings.hassaddress === null) { $scope.bridge.settings.hassaddress = { devices: [] @@ -1886,7 +1886,8 @@ app.controller('SystemController', function ($scope, $location, bridgeService, n ip: newhassip, port: newhassport, password: newhasspassword, - secure: newhasssecure + secure: newhasssecure, + extensions: newhassauth }; $scope.bridge.settings.hassaddress.devices.push(newhass); $scope.newhassname = null; diff --git a/src/main/resources/public/views/system.html b/src/main/resources/public/views/system.html index df98856..bf31d2e 100644 --- a/src/main/resources/public/views/system.html +++ b/src/main/resources/public/views/system.html @@ -411,7 +411,8 @@ Name IP Port - Password (opt) + Token/Password + Auth Type Use SSL Manage @@ -429,6 +430,12 @@ + + Legacy Password + + @@ -448,11 +455,17 @@ - + Legacy Password + + + + ng-click="addHasstoSettings(newhassname, newhassip, newhassport, newhasspassword, newhasssecure, newhassauth)">Add