From 5a292c98a2b1efeedb5802e531fd3e413de23370 Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 3 Jan 2017 16:36:45 -0600 Subject: [PATCH] Continue testing and fixing 4 beta --- pom.xml | 2 +- .../bwssystems/HABridge/hue/HueMulator.java | 102 ++++++++---------- .../HABridge/plugins/hass/HassHome.java | 7 +- .../HABridge/plugins/hass/HomeAssistant.java | 16 ++- 4 files changed, 63 insertions(+), 64 deletions(-) diff --git a/pom.xml b/pom.xml index b5b61f9..7f3801d 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.bwssystems.HABridge ha-bridge - 4beta2.2 + 4beta2.4 jar HA Bridge diff --git a/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java b/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java index ea58628..5b9eb67 100644 --- a/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java +++ b/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java @@ -261,30 +261,30 @@ public class HueMulator { }); } - private String formatSuccessHueResponse(StateChangeBody state, String body, String lightId, - DeviceState deviceState) { + private String formatSuccessHueResponse(StateChangeBody stateChanges, String body, String lightId, + DeviceState deviceState, Integer targetBri, Integer targetBriInc) { String responseString = "["; boolean notFirstChange = false; if (body.contains("\"on\"")) { responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/on\":"; - if (state.isOn()) { + if (stateChanges.isOn()) { responseString = responseString + "true}}"; } else { responseString = responseString + "false}}"; } if (deviceState != null) - deviceState.setOn(state.isOn()); + deviceState.setOn(stateChanges.isOn()); notFirstChange = true; } if (body.contains("\"bri\"")) { if (notFirstChange) responseString = responseString + ","; - responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/bri\":" + state.getBri() + responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/bri\":" + stateChanges.getBri() + "}}"; if (deviceState != null) - deviceState.setBri(state.getBri()); + deviceState.setBri(stateChanges.getBri()); notFirstChange = true; } @@ -292,49 +292,52 @@ public class HueMulator { if (notFirstChange) responseString = responseString + ","; 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 // this method + if (deviceState != null) + deviceState.setBri(BrightnessDecode.calculateIntensity(deviceState.getBri(), targetBri, targetBriInc)); + notFirstChange = true; } if (body.contains("\"ct\"")) { if (notFirstChange) responseString = responseString + ","; - responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/ct\":" + state.getCt() + responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/ct\":" + stateChanges.getCt() + "}}"; if (deviceState != null) - deviceState.setCt(state.getCt()); + deviceState.setCt(stateChanges.getCt()); notFirstChange = true; } if (body.contains("\"xy\"")) { if (notFirstChange) responseString = responseString + ","; - responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/xy\":" + state.getXy() + responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/xy\":" + stateChanges.getXy() + "}}"; if (deviceState != null) - deviceState.setXy(state.getXy()); + deviceState.setXy(stateChanges.getXy()); notFirstChange = true; } if (body.contains("\"hue\"")) { if (notFirstChange) responseString = responseString + ","; - responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/hue\":" + state.getHue() + responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/hue\":" + stateChanges.getHue() + "}}"; if (deviceState != null) - deviceState.setHue(state.getHue()); + deviceState.setHue(stateChanges.getHue()); notFirstChange = true; } if (body.contains("\"sat\"")) { if (notFirstChange) responseString = responseString + ","; - responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/sat\":" + state.getSat() + responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/sat\":" + stateChanges.getSat() + "}}"; if (deviceState != null) - deviceState.setSat(state.getSat()); + deviceState.setSat(stateChanges.getSat()); notFirstChange = true; } @@ -342,9 +345,9 @@ public class HueMulator { if (notFirstChange) responseString = responseString + ","; responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/ct_inc\":" - + state.getCt_inc() + "}}"; + + stateChanges.getCt_inc() + "}}"; if (deviceState != null) - deviceState.setCt(deviceState.getCt() + state.getCt_inc()); + deviceState.setCt(deviceState.getCt() + stateChanges.getCt_inc()); notFirstChange = true; } @@ -352,9 +355,9 @@ public class HueMulator { if (notFirstChange) responseString = responseString + ","; responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/xy_inc\":" - + state.getXy_inc() + "}}"; + + stateChanges.getXy_inc() + "}}"; if (deviceState != null) - deviceState.setXy(state.getXy()); + deviceState.setXy(stateChanges.getXy()); notFirstChange = true; } @@ -362,9 +365,9 @@ public class HueMulator { if (notFirstChange) responseString = responseString + ","; responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/hue_inc\":" - + state.getHue_inc() + "}}"; + + stateChanges.getHue_inc() + "}}"; if (deviceState != null) - deviceState.setHue(deviceState.getHue() + state.getHue_inc()); + deviceState.setHue(deviceState.getHue() + stateChanges.getHue_inc()); notFirstChange = true; } @@ -372,9 +375,9 @@ public class HueMulator { if (notFirstChange) responseString = responseString + ","; responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/sat_inc\":" - + state.getSat_inc() + "}}"; + + stateChanges.getSat_inc() + "}}"; if (deviceState != null) - deviceState.setSat(deviceState.getSat() + state.getSat_inc()); + deviceState.setSat(deviceState.getSat() + stateChanges.getSat_inc()); notFirstChange = true; } @@ -382,9 +385,9 @@ public class HueMulator { if (notFirstChange) responseString = responseString + ","; responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/effect\":" - + state.getEffect() + "}}"; + + stateChanges.getEffect() + "}}"; if (deviceState != null) - deviceState.setEffect(state.getEffect()); + deviceState.setEffect(stateChanges.getEffect()); notFirstChange = true; } @@ -392,7 +395,7 @@ public class HueMulator { if (notFirstChange) responseString = responseString + ","; responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/transitiontime\":" - + state.getTransitiontime() + "}}"; + + stateChanges.getTransitiontime() + "}}"; // if(deviceState != null) // deviceState.setTransitiontime(state.getTransitiontime()); notFirstChange = true; @@ -402,12 +405,18 @@ public class HueMulator { if (notFirstChange) responseString = responseString + ","; responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/alert\":" - + state.getAlert() + "}}"; + + stateChanges.getAlert() + "}}"; if (deviceState != null) - deviceState.setAlert(state.getAlert()); + deviceState.setAlert(stateChanges.getAlert()); notFirstChange = true; } + if(deviceState.isOn() && deviceState.getBri() <= 0) + deviceState.setBri(255); + + if(!deviceState.isOn() && (targetBri != null || targetBriInc != null)) + deviceState.setOn(true); + responseString = responseString + "]"; return responseString; @@ -648,36 +657,15 @@ public class HueMulator { if (body.contains("\"bri_inc\"")) targetBriInc = new Integer(theStateChanges.getBri_inc()); 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(); if (state == null) state = DeviceState.createDeviceState(); - state.fillIn(); - if (targetBri != null) { - if (targetBri > 0 && !state.isOn()) - 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)); + + responseString = this.formatSuccessHueResponse(theStateChanges, body, lightId, state, targetBri, targetBriInc); + device.setDeviceState(state); return responseString; } @@ -725,9 +713,6 @@ public class HueMulator { state = DeviceState.createDeviceState(); if (targetBri != null || targetBriInc != null) { - if(!state.isOn()) - state.setOn(true); - url = device.getDimUrl(); if (url == null || url.length() == 0) @@ -735,10 +720,8 @@ public class HueMulator { } else { if (theStateChanges.isOn()) { url = device.getOnUrl(); - state.setOn(true); } else if (!theStateChanges.isOn()) { url = device.getOffUrl(); - state.setOn(false); } } @@ -807,8 +790,7 @@ public class HueMulator { } if (responseString == null || !responseString.contains("[{\"error\":")) { - responseString = this.formatSuccessHueResponse(theStateChanges, body, lightId, state); - state.setBri(BrightnessDecode.calculateIntensity(state.getBri(), targetBri, targetBriInc)); + responseString = this.formatSuccessHueResponse(theStateChanges, body, lightId, state, targetBri, targetBriInc); device.setDeviceState(state); } return responseString; diff --git a/src/main/java/com/bwssystems/HABridge/plugins/hass/HassHome.java b/src/main/java/com/bwssystems/HABridge/plugins/hass/HassHome.java index 7c4871b..fd8df64 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/hass/HassHome.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/hass/HassHome.java @@ -18,6 +18,7 @@ import com.bwssystems.HABridge.hue.BrightnessDecode; import com.bwssystems.HABridge.hue.MultiCommandUtil; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; public class HassHome implements Home { private static final Logger log = LoggerFactory.getLogger(HassHome.class); @@ -101,7 +102,11 @@ public class HassHome implements Home { aNewHassDevice.setDeviceState(theDevice); aNewHassDevice.setHassaddress(hassMap.get(theKey).getHassAddress().getIp()); 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("."))); theDeviceList.add(aNewHassDevice); } 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 8cb62d5..582a9d0 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/hass/HomeAssistant.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/hass/HomeAssistant.java @@ -40,7 +40,11 @@ public class HomeAssistant { String aBody = "{\"entity_id\":\"" + aCommand.getEntityId() + "\""; NameValue[] headers = null; 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")) { aUrl = aUrl + "/turn_on"; @@ -63,8 +67,16 @@ 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; + } 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) { log.debug("GET Hass States - data: " + theData); theHassStates = new Gson().fromJson(theData, State[].class);