Continue testing and fixing 4 beta

This commit is contained in:
Admin
2017-01-03 16:36:45 -06:00
parent 9155989791
commit 5a292c98a2
4 changed files with 63 additions and 64 deletions

View File

@@ -5,7 +5,7 @@
<groupId>com.bwssystems.HABridge</groupId> <groupId>com.bwssystems.HABridge</groupId>
<artifactId>ha-bridge</artifactId> <artifactId>ha-bridge</artifactId>
<version>4beta2.2</version> <version>4beta2.4</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>HA Bridge</name> <name>HA Bridge</name>

View File

@@ -261,30 +261,30 @@ public class HueMulator {
}); });
} }
private String formatSuccessHueResponse(StateChangeBody state, String body, String lightId, private String formatSuccessHueResponse(StateChangeBody stateChanges, String body, String lightId,
DeviceState deviceState) { DeviceState deviceState, Integer targetBri, Integer targetBriInc) {
String responseString = "["; String responseString = "[";
boolean notFirstChange = false; boolean notFirstChange = false;
if (body.contains("\"on\"")) { if (body.contains("\"on\"")) {
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/on\":"; responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/on\":";
if (state.isOn()) { if (stateChanges.isOn()) {
responseString = responseString + "true}}"; responseString = responseString + "true}}";
} else { } else {
responseString = responseString + "false}}"; responseString = responseString + "false}}";
} }
if (deviceState != null) if (deviceState != null)
deviceState.setOn(state.isOn()); deviceState.setOn(stateChanges.isOn());
notFirstChange = true; notFirstChange = true;
} }
if (body.contains("\"bri\"")) { if (body.contains("\"bri\"")) {
if (notFirstChange) if (notFirstChange)
responseString = responseString + ","; responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/bri\":" + state.getBri() responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/bri\":" + stateChanges.getBri()
+ "}}"; + "}}";
if (deviceState != null) if (deviceState != null)
deviceState.setBri(state.getBri()); deviceState.setBri(stateChanges.getBri());
notFirstChange = true; notFirstChange = true;
} }
@@ -292,49 +292,52 @@ public class HueMulator {
if (notFirstChange) if (notFirstChange)
responseString = responseString + ","; responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/bri_inc\":" responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/bri_inc\":"
+ state.getBri_inc() + "}}"; + stateChanges.getBri_inc() + "}}";
// INFO: Bright inc check for deviceState needs to be outside of // INFO: Bright inc check for deviceState needs to be outside of
// this method // this method
if (deviceState != null)
deviceState.setBri(BrightnessDecode.calculateIntensity(deviceState.getBri(), targetBri, targetBriInc));
notFirstChange = true; notFirstChange = true;
} }
if (body.contains("\"ct\"")) { if (body.contains("\"ct\"")) {
if (notFirstChange) if (notFirstChange)
responseString = responseString + ","; responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/ct\":" + state.getCt() responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/ct\":" + stateChanges.getCt()
+ "}}"; + "}}";
if (deviceState != null) if (deviceState != null)
deviceState.setCt(state.getCt()); deviceState.setCt(stateChanges.getCt());
notFirstChange = true; notFirstChange = true;
} }
if (body.contains("\"xy\"")) { if (body.contains("\"xy\"")) {
if (notFirstChange) if (notFirstChange)
responseString = responseString + ","; responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/xy\":" + state.getXy() responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/xy\":" + stateChanges.getXy()
+ "}}"; + "}}";
if (deviceState != null) if (deviceState != null)
deviceState.setXy(state.getXy()); deviceState.setXy(stateChanges.getXy());
notFirstChange = true; notFirstChange = true;
} }
if (body.contains("\"hue\"")) { if (body.contains("\"hue\"")) {
if (notFirstChange) if (notFirstChange)
responseString = responseString + ","; responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/hue\":" + state.getHue() responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/hue\":" + stateChanges.getHue()
+ "}}"; + "}}";
if (deviceState != null) if (deviceState != null)
deviceState.setHue(state.getHue()); deviceState.setHue(stateChanges.getHue());
notFirstChange = true; notFirstChange = true;
} }
if (body.contains("\"sat\"")) { if (body.contains("\"sat\"")) {
if (notFirstChange) if (notFirstChange)
responseString = responseString + ","; responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/sat\":" + state.getSat() responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/sat\":" + stateChanges.getSat()
+ "}}"; + "}}";
if (deviceState != null) if (deviceState != null)
deviceState.setSat(state.getSat()); deviceState.setSat(stateChanges.getSat());
notFirstChange = true; notFirstChange = true;
} }
@@ -342,9 +345,9 @@ public class HueMulator {
if (notFirstChange) if (notFirstChange)
responseString = responseString + ","; responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/ct_inc\":" responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/ct_inc\":"
+ state.getCt_inc() + "}}"; + stateChanges.getCt_inc() + "}}";
if (deviceState != null) if (deviceState != null)
deviceState.setCt(deviceState.getCt() + state.getCt_inc()); deviceState.setCt(deviceState.getCt() + stateChanges.getCt_inc());
notFirstChange = true; notFirstChange = true;
} }
@@ -352,9 +355,9 @@ public class HueMulator {
if (notFirstChange) if (notFirstChange)
responseString = responseString + ","; responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/xy_inc\":" responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/xy_inc\":"
+ state.getXy_inc() + "}}"; + stateChanges.getXy_inc() + "}}";
if (deviceState != null) if (deviceState != null)
deviceState.setXy(state.getXy()); deviceState.setXy(stateChanges.getXy());
notFirstChange = true; notFirstChange = true;
} }
@@ -362,9 +365,9 @@ public class HueMulator {
if (notFirstChange) if (notFirstChange)
responseString = responseString + ","; responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/hue_inc\":" responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/hue_inc\":"
+ state.getHue_inc() + "}}"; + stateChanges.getHue_inc() + "}}";
if (deviceState != null) if (deviceState != null)
deviceState.setHue(deviceState.getHue() + state.getHue_inc()); deviceState.setHue(deviceState.getHue() + stateChanges.getHue_inc());
notFirstChange = true; notFirstChange = true;
} }
@@ -372,9 +375,9 @@ public class HueMulator {
if (notFirstChange) if (notFirstChange)
responseString = responseString + ","; responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/sat_inc\":" responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/sat_inc\":"
+ state.getSat_inc() + "}}"; + stateChanges.getSat_inc() + "}}";
if (deviceState != null) if (deviceState != null)
deviceState.setSat(deviceState.getSat() + state.getSat_inc()); deviceState.setSat(deviceState.getSat() + stateChanges.getSat_inc());
notFirstChange = true; notFirstChange = true;
} }
@@ -382,9 +385,9 @@ public class HueMulator {
if (notFirstChange) if (notFirstChange)
responseString = responseString + ","; responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/effect\":" responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/effect\":"
+ state.getEffect() + "}}"; + stateChanges.getEffect() + "}}";
if (deviceState != null) if (deviceState != null)
deviceState.setEffect(state.getEffect()); deviceState.setEffect(stateChanges.getEffect());
notFirstChange = true; notFirstChange = true;
} }
@@ -392,7 +395,7 @@ public class HueMulator {
if (notFirstChange) if (notFirstChange)
responseString = responseString + ","; responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/transitiontime\":" responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/transitiontime\":"
+ state.getTransitiontime() + "}}"; + stateChanges.getTransitiontime() + "}}";
// if(deviceState != null) // if(deviceState != null)
// deviceState.setTransitiontime(state.getTransitiontime()); // deviceState.setTransitiontime(state.getTransitiontime());
notFirstChange = true; notFirstChange = true;
@@ -402,12 +405,18 @@ public class HueMulator {
if (notFirstChange) if (notFirstChange)
responseString = responseString + ","; responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/alert\":" responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/alert\":"
+ state.getAlert() + "}}"; + stateChanges.getAlert() + "}}";
if (deviceState != null) if (deviceState != null)
deviceState.setAlert(state.getAlert()); deviceState.setAlert(stateChanges.getAlert());
notFirstChange = true; notFirstChange = true;
} }
if(deviceState.isOn() && deviceState.getBri() <= 0)
deviceState.setBri(255);
if(!deviceState.isOn() && (targetBri != null || targetBriInc != null))
deviceState.setOn(true);
responseString = responseString + "]"; responseString = responseString + "]";
return responseString; return responseString;
@@ -648,36 +657,15 @@ public class HueMulator {
if (body.contains("\"bri_inc\"")) if (body.contains("\"bri_inc\""))
targetBriInc = new Integer(theStateChanges.getBri_inc()); targetBriInc = new Integer(theStateChanges.getBri_inc());
else if (body.contains("\"bri\"")) { else if (body.contains("\"bri\"")) {
if (theStateChanges.isOn() && theStateChanges.getBri() == 0)
targetBri = null;
else
targetBri = new Integer(theStateChanges.getBri()); targetBri = new Integer(theStateChanges.getBri());
} }
state = device.getDeviceState(); state = device.getDeviceState();
if (state == null) if (state == null)
state = DeviceState.createDeviceState(); state = DeviceState.createDeviceState();
state.fillIn();
if (targetBri != null) { responseString = this.formatSuccessHueResponse(theStateChanges, body, lightId, state, targetBri, targetBriInc);
if (targetBri > 0 && !state.isOn()) device.setDeviceState(state);
state.setOn(true);
} else if (targetBriInc != null) {
if ((state.getBri() + targetBriInc) > 0 && !state.isOn())
state.setOn(true);
else if ((state.getBri() + targetBriInc) <= 0 && state.isOn())
state.setOn(false);
} else {
if (theStateChanges.isOn()) {
state.setOn(true);
if (state.getBri() <= 0)
state.setBri(255);
} else {
state.setOn(false);
state.setBri(0);
}
}
responseString = this.formatSuccessHueResponse(theStateChanges, body, lightId, device.getDeviceState());
device.getDeviceState().setBri(BrightnessDecode.calculateIntensity(state.getBri(), targetBri, targetBriInc));
return responseString; return responseString;
} }
@@ -725,9 +713,6 @@ public class HueMulator {
state = DeviceState.createDeviceState(); state = DeviceState.createDeviceState();
if (targetBri != null || targetBriInc != null) { if (targetBri != null || targetBriInc != null) {
if(!state.isOn())
state.setOn(true);
url = device.getDimUrl(); url = device.getDimUrl();
if (url == null || url.length() == 0) if (url == null || url.length() == 0)
@@ -735,10 +720,8 @@ public class HueMulator {
} else { } else {
if (theStateChanges.isOn()) { if (theStateChanges.isOn()) {
url = device.getOnUrl(); url = device.getOnUrl();
state.setOn(true);
} else if (!theStateChanges.isOn()) { } else if (!theStateChanges.isOn()) {
url = device.getOffUrl(); url = device.getOffUrl();
state.setOn(false);
} }
} }
@@ -807,8 +790,7 @@ public class HueMulator {
} }
if (responseString == null || !responseString.contains("[{\"error\":")) { if (responseString == null || !responseString.contains("[{\"error\":")) {
responseString = this.formatSuccessHueResponse(theStateChanges, body, lightId, state); responseString = this.formatSuccessHueResponse(theStateChanges, body, lightId, state, targetBri, targetBriInc);
state.setBri(BrightnessDecode.calculateIntensity(state.getBri(), targetBri, targetBriInc));
device.setDeviceState(state); device.setDeviceState(state);
} }
return responseString; return responseString;

View File

@@ -18,6 +18,7 @@ import com.bwssystems.HABridge.hue.BrightnessDecode;
import com.bwssystems.HABridge.hue.MultiCommandUtil; import com.bwssystems.HABridge.hue.MultiCommandUtil;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
public class HassHome implements Home { public class HassHome implements Home {
private static final Logger log = LoggerFactory.getLogger(HassHome.class); private static final Logger log = LoggerFactory.getLogger(HassHome.class);
@@ -101,7 +102,11 @@ public class HassHome implements Home {
aNewHassDevice.setDeviceState(theDevice); aNewHassDevice.setDeviceState(theDevice);
aNewHassDevice.setHassaddress(hassMap.get(theKey).getHassAddress().getIp()); aNewHassDevice.setHassaddress(hassMap.get(theKey).getHassAddress().getIp());
aNewHassDevice.setHassname(theKey); aNewHassDevice.setHassname(theKey);
aNewHassDevice.setDeviceName(theDevice.getAttributes().get("friendly_name").getAsString()); JsonElement friendlyName = theDevice.getAttributes().get("friendly_name");
if(friendlyName == null)
aNewHassDevice.setDeviceName(theDevice.getEntityId());
else
aNewHassDevice.setDeviceName(friendlyName.getAsString());
aNewHassDevice.setDomain(theDevice.getEntityId().substring(0, theDevice.getEntityId().indexOf("."))); aNewHassDevice.setDomain(theDevice.getEntityId().substring(0, theDevice.getEntityId().indexOf(".")));
theDeviceList.add(aNewHassDevice); theDeviceList.add(aNewHassDevice);
} }

View File

@@ -40,7 +40,11 @@ public class HomeAssistant {
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()) {
headers = new Gson().fromJson("{\"name\":\"x-ha-access\",\"value\":\"" + hassAddress.getPassword() + "\"}", NameValue[].class); 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"; aUrl = aUrl + "/turn_on";
@@ -63,8 +67,16 @@ public class HomeAssistant {
State[] theHassStates; State[] theHassStates;
String theUrl = null; String theUrl = null;
String theData; 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;
}
theUrl = "http://" + hassAddress.getIp() + ":" + hassAddress.getPort() + "/api/states"; theUrl = "http://" + hassAddress.getIp() + ":" + hassAddress.getPort() + "/api/states";
theData = anHttpHandler.doHttpRequest(theUrl, HttpGet.METHOD_NAME, null, null, null); 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);