Start adding new Bearer Token for Home Assistant

This commit is contained in:
BWS Systems
2019-06-13 15:51:49 -05:00
parent 7d920d3885
commit bfd1b94473
4 changed files with 79 additions and 38 deletions

View File

@@ -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;
}
}

View File

@@ -16,14 +16,17 @@ import com.bwssystems.HABridge.plugins.http.HTTPHome;
import com.google.gson.Gson; import com.google.gson.Gson;
public class HomeAssistant { public class HomeAssistant {
private static final Logger log = LoggerFactory.getLogger(HomeAssistant.class); private static final Logger log = LoggerFactory.getLogger(HomeAssistant.class);
private NamedIP hassAddress; private NamedIP hassAddress;
private HTTPHandler anHttpHandler; private HTTPHandler anHttpHandler;
private HassAuth theAuthType;
public HomeAssistant(NamedIP addressName) { public HomeAssistant(NamedIP addressName) {
super(); super();
anHttpHandler = HTTPHome.getHandler(); anHttpHandler = HTTPHome.getHandler();
hassAddress = addressName; hassAddress = addressName;
theAuthType = null;
isLegacyAuth();
} }
public NamedIP getHassAddress() { public NamedIP getHassAddress() {
@@ -35,38 +38,37 @@ public class HomeAssistant {
} }
public Boolean callCommand(HassCommand aCommand) { public Boolean callCommand(HassCommand aCommand) {
log.debug("calling HomeAssistant: " + aCommand.getHassName() + " - " log.debug("calling HomeAssistant: " + aCommand.getHassName() + " - " + aCommand.getEntityId() + " - "
+ aCommand.getEntityId() + " - " + aCommand.getState() + " - " + aCommand.getBri()); + aCommand.getState() + " - " + aCommand.getBri());
String aUrl = null; String aUrl = null;
String domain = aCommand.getEntityId().substring(0, aCommand.getEntityId().indexOf(".")); String domain = aCommand.getEntityId().substring(0, aCommand.getEntityId().indexOf("."));
aUrl = hassAddress.getHttpPreamble() + "/api/services/"; aUrl = hassAddress.getHttpPreamble() + "/api/services/";
if(domain.equals("group")) if (domain.equals("group"))
aUrl = aUrl + "homeassistant"; aUrl = aUrl + "homeassistant";
else else
aUrl = aUrl + domain; aUrl = aUrl + domain;
String aBody = "{\"entity_id\":\"" + aCommand.getEntityId() + "\""; String aBody = "{\"entity_id\":\"" + aCommand.getEntityId() + "\"";
NameValue[] headers = null; NameValue[] headers = null;
if(hassAddress.getPassword() != null && !hassAddress.getPassword().isEmpty()) { if (hassAddress.getPassword() != null && !hassAddress.getPassword().isEmpty()) {
NameValue password = new NameValue(); NameValue password = new NameValue();
password.setName("x-ha-access"); password.setName("x-ha-access");
password.setValue(hassAddress.getPassword()); password.setValue(hassAddress.getPassword());
headers = new NameValue[1]; headers = new NameValue[1];
headers[0] = password; headers[0] = password;
} }
if(aCommand.getState().equalsIgnoreCase("on")) { if (aCommand.getState().equalsIgnoreCase("on")) {
aUrl = aUrl + "/turn_on"; aUrl = aUrl + "/turn_on";
if(aCommand.getBri() != null) if (aCommand.getBri() != null)
aBody = aBody + ",\"brightness\":" + aCommand.getBri() + "}"; aBody = aBody + ",\"brightness\":" + aCommand.getBri() + "}";
else else
aBody = aBody + "}"; aBody = aBody + "}";
} } else {
else {
aUrl = aUrl + "/turn_off"; aUrl = aUrl + "/turn_off";
aBody = aBody + "}"; aBody = aBody + "}";
} }
log.debug("Calling HomeAssistant with url: " + aUrl); log.debug("Calling HomeAssistant with url: " + aUrl);
String theData = anHttpHandler.doHttpRequest(aUrl, HttpPost.METHOD_NAME, "application/json", aBody, headers); String theData = anHttpHandler.doHttpRequest(aUrl, HttpPost.METHOD_NAME, "application/json", aBody, headers);
log.debug("call Command return is: <" + theData + ">"); log.debug("call Command return is: <" + theData + ">");
return true; return true;
} }
@@ -74,39 +76,51 @@ public class HomeAssistant {
List<State> theDeviceStates = null; List<State> theDeviceStates = null;
State[] theHassStates; State[] theHassStates;
String theUrl = null; String theUrl = null;
String theData; String theData;
NameValue[] headers = null; 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(); NameValue password = new NameValue();
password.setName("x-ha-access"); password.setName("x-ha-access");
password.setValue(hassAddress.getPassword()); password.setValue(hassAddress.getPassword());
headers = new NameValue[1]; headers = new NameValue[1];
headers[0] = password; headers[0] = password;
} }
if(hassAddress.getSecure() != null && hassAddress.getSecure()) if (hassAddress.getSecure() != null && hassAddress.getSecure())
theUrl = "https"; theUrl = "https";
else else
theUrl = "http"; theUrl = "http";
theUrl = theUrl + "://" + hassAddress.getIp() + ":" + hassAddress.getPort() + "/api/states"; theUrl = theUrl + "://" + hassAddress.getIp() + ":" + hassAddress.getPort() + "/api/states";
theData = anHttpHandler.doHttpRequest(theUrl, HttpGet.METHOD_NAME, "application/json", null, headers); theData = anHttpHandler.doHttpRequest(theUrl, HttpGet.METHOD_NAME, "application/json", null, headers);
if(theData != null) { if (theData != null) {
log.debug("GET Hass States - data: " + theData); log.debug("GET Hass States - data: " + theData);
theHassStates = new Gson().fromJson(theData, State[].class); theHassStates = new Gson().fromJson(theData, State[].class);
if(theHassStates == null) { if (theHassStates == null) {
log.warn("Cannot get an devices for HomeAssistant " + hassAddress.getName() + " as response is not parsable."); log.warn("Cannot get an devices for HomeAssistant " + hassAddress.getName()
} + " as response is not parsable.");
else { } else {
theDeviceStates = new ArrayList<State>(Arrays.asList(theHassStates)); theDeviceStates = new ArrayList<State>(Arrays.asList(theHassStates));
} }
} } else
else log.warn("Cannot get an devices for HomeAssistant " + hassAddress.getName() + " http call failed.");
log.warn("Cannot get an devices for HomeAssistant " + hassAddress.getName() + " http call failed.");
return theDeviceStates; return theDeviceStates;
} }
protected void closeClient() { protected void closeClient() {
anHttpHandler.closeHandler(); anHttpHandler.closeHandler();
anHttpHandler = null; 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();
}
} }

View File

@@ -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) { if ($scope.bridge.settings.hassaddress === undefined || $scope.bridge.settings.hassaddress === null) {
$scope.bridge.settings.hassaddress = { $scope.bridge.settings.hassaddress = {
devices: [] devices: []
@@ -1886,7 +1886,8 @@ app.controller('SystemController', function ($scope, $location, bridgeService, n
ip: newhassip, ip: newhassip,
port: newhassport, port: newhassport,
password: newhasspassword, password: newhasspassword,
secure: newhasssecure secure: newhasssecure,
extensions: newhassauth
}; };
$scope.bridge.settings.hassaddress.devices.push(newhass); $scope.bridge.settings.hassaddress.devices.push(newhass);
$scope.newhassname = null; $scope.newhassname = null;

View File

@@ -411,7 +411,8 @@
<th>Name</th> <th>Name</th>
<th>IP</th> <th>IP</th>
<th>Port</th> <th>Port</th>
<th>Password (opt)</th> <th>Token/Password</th>
<th>Auth Type</th>
<th>Use SSL</th> <th>Use SSL</th>
<th>Manage</th> <th>Manage</th>
</tr> </tr>
@@ -429,6 +430,12 @@
<td><input id="bridge-settings-next-hass-password" <td><input id="bridge-settings-next-hass-password"
class="form-control" type="password" ng-model="hass.password" class="form-control" type="password" ng-model="hass.password"
placeholder="Home Assistant password (opt)"></td> placeholder="Home Assistant password (opt)"></td>
<td>
<td>Legacy Password<input type="checkbox"
ng-model="hass.extensions.legacyauth" ng-true-value=true
ng-false-value=false>
</td>
<td><input type="checkbox" <td><input type="checkbox"
ng-model="hass.secure" ng-true-value=true ng-model="hass.secure" ng-true-value=true
ng-false-value=false></td> ng-false-value=false></td>
@@ -448,11 +455,17 @@
<td><input id="bridge-settings-new-hass-password" <td><input id="bridge-settings-new-hass-password"
class="form-control" type="password" ng-model="newhasspassword" class="form-control" type="password" ng-model="newhasspassword"
placeholder="Home Assistant password (opt)"></td> placeholder="Home Assistant password (opt)"></td>
<td><input type="checkbox" <td>
<td>Legacy Password<input type="checkbox"
ng-model="newhassauth.legacyauth" ng-true-value=true
ng-false-value=false>
</td>
<td><input type="checkbox"
ng-model="newhasssecure" ng-true-value=true ng-model="newhasssecure" ng-true-value=true
ng-false-value=false></td> ng-false-value=false></td>
<td><button class="btn btn-success" type="submit" <td><button class="btn btn-success" type="submit"
ng-click="addHasstoSettings(newhassname, newhassip, newhassport, newhasspassword, newhasssecure)">Add</button></td> ng-click="addHasstoSettings(newhassname, newhassip, newhassport, newhasspassword, newhasssecure, newhassauth)">Add</button></td>
</tr> </tr>
</table></td> </table></td>
</tr> </tr>