Compare commits

..

2 Commits

Author SHA1 Message Date
BWS Systems
46ad4489ad Finish HomeAssistant auth changes and implement no scroll for pages 2019-06-14 10:32:15 -05:00
BWS Systems
bfd1b94473 Start adding new Bearer Token for Home Assistant 2019-06-13 15:51:49 -05:00
6 changed files with 544 additions and 628 deletions

View File

@@ -5,7 +5,7 @@
<groupId>com.bwssystems.HABridge</groupId>
<artifactId>ha-bridge</artifactId>
<version>5.3.0RC2</version>
<version>5.3.0RC3</version>
<packaging>jar</packaging>
<name>HA Bridge</name>

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

@@ -19,11 +19,16 @@ public class HomeAssistant {
private static final Logger log = LoggerFactory.getLogger(HomeAssistant.class);
private NamedIP hassAddress;
private HTTPHandler anHttpHandler;
private HassAuth theAuthType;
private NameValue[] headers;
public HomeAssistant(NamedIP addressName) {
super();
anHttpHandler = HTTPHome.getHandler();
hassAddress = addressName;
theAuthType = null;
headers = null;
isLegacyAuth();
}
public NamedIP getHassAddress() {
@@ -35,8 +40,8 @@ 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/";
@@ -45,22 +50,14 @@ public class HomeAssistant {
else
aUrl = aUrl + domain;
String aBody = "{\"entity_id\":\"" + aCommand.getEntityId() + "\"";
NameValue[] headers = null;
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;
}
headers = getAuthHeader();
if (aCommand.getState().equalsIgnoreCase("on")) {
aUrl = aUrl + "/turn_on";
if (aCommand.getBri() != null)
aBody = aBody + ",\"brightness\":" + aCommand.getBri() + "}";
else
aBody = aBody + "}";
}
else {
} else {
aUrl = aUrl + "/turn_off";
aBody = aBody + "}";
}
@@ -75,14 +72,7 @@ public class HomeAssistant {
State[] theHassStates;
String theUrl = null;
String theData;
NameValue[] headers = null;
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;
}
headers = getAuthHeader();
if (hassAddress.getSecure() != null && hassAddress.getSecure())
theUrl = "https";
else
@@ -93,20 +83,50 @@ public class HomeAssistant {
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 {
log.warn("Cannot get an devices for HomeAssistant " + hassAddress.getName()
+ " as response is not parsable.");
} else {
theDeviceStates = new ArrayList<State>(Arrays.asList(theHassStates));
}
}
else
} 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();
}
private NameValue[] getAuthHeader() {
if (headers == null) {
if (hassAddress.getPassword() != null && !hassAddress.getPassword().isEmpty()) {
headers = new NameValue[1];
headers[0] = new NameValue();
if (isLegacyAuth()) {
headers[0].setName("x-ha-access");
headers[0].setValue(hassAddress.getPassword());
} else {
headers[0].setName("Authorization");
headers[0].setValue("Bearer " + hassAddress.getPassword());
}
}
} else if(hassAddress.getPassword() == null || hassAddress.getPassword().isEmpty()) {
headers = null;
}
return headers;
}
}

View File

@@ -29,7 +29,9 @@
.scrollArea {
height: 100%;
/* THis makes the table scroll - disabled as a feature for now
max-height: 800px;
*/
overflow-x: auto;
overflow-y: auto;
border: 1px solid #d5d5d5;

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) {
$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;

File diff suppressed because it is too large Load Diff