From 4b247557d45fafba1fb092c1d00e14baf6c4ede9 Mon Sep 17 00:00:00 2001 From: Admin Date: Thu, 14 Dec 2017 14:54:50 -0600 Subject: [PATCH 01/40] Start adding OpenHAB to the bridge. --- pom.xml | 2 +- .../HABridge/BridgeSettingsDescriptor.java | 27 ++++ .../HABridge/plugins/openhab/OpenHABHome.java | 82 +++++++++++++ .../plugins/openhab/OpenHABInstance.java | 116 ++++++++++++++++++ .../HABridge/plugins/openhab/OpenHABItem.java | 110 +++++++++++++++++ .../HABridge/plugins/openhab/Option.java | 32 +++++ .../plugins/openhab/StateDescription.java | 44 +++++++ 7 files changed, 412 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/bwssystems/HABridge/plugins/openhab/OpenHABHome.java create mode 100644 src/main/java/com/bwssystems/HABridge/plugins/openhab/OpenHABInstance.java create mode 100644 src/main/java/com/bwssystems/HABridge/plugins/openhab/OpenHABItem.java create mode 100644 src/main/java/com/bwssystems/HABridge/plugins/openhab/Option.java create mode 100644 src/main/java/com/bwssystems/HABridge/plugins/openhab/StateDescription.java diff --git a/pom.xml b/pom.xml index a55b3255..f9fa4773 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.bwssystems.HABridge ha-bridge - 5.1.0 + 5.1.0a jar HA Bridge diff --git a/src/main/java/com/bwssystems/HABridge/BridgeSettingsDescriptor.java b/src/main/java/com/bwssystems/HABridge/BridgeSettingsDescriptor.java index 7da15511..3c1f5caf 100644 --- a/src/main/java/com/bwssystems/HABridge/BridgeSettingsDescriptor.java +++ b/src/main/java/com/bwssystems/HABridge/BridgeSettingsDescriptor.java @@ -87,6 +87,9 @@ public class BridgeSettingsDescriptor { @SerializedName("somfyaddress") @Expose private IpList somfyaddress; + @SerializedName("openhabaddress") + @Expose + private IpList openhabaddress; @SerializedName("hubversion") @Expose private String hubversion; @@ -113,6 +116,7 @@ public class BridgeSettingsDescriptor { private boolean somfyconfigured; private boolean lifxconfigured; private boolean homewizardconfigured; + private boolean openhabconfigured; // Deprecated settings private String haltoken; @@ -136,6 +140,7 @@ public BridgeSettingsDescriptor() { this.domoticzconfigured = false; this.homewizardconfigured = false; this.lifxconfigured = false; + this.openhabconfigured = false; this.farenheit = true; this.securityData = null; this.settingsChanged = false; @@ -384,6 +389,18 @@ public boolean isHassconfigured() { public void setHassconfigured(boolean hassconfigured) { this.hassconfigured = hassconfigured; } + public IpList getOpenhabaddress() { + return openhabaddress; + } + public void setOpenhabaddress(IpList openhabaddress) { + this.openhabaddress = openhabaddress; + } + public boolean isOpenhabconfigured() { + return openhabconfigured; + } + public void setOpenhabconfigured(boolean openhabconfigured) { + this.openhabconfigured = openhabconfigured; + } public String getHubversion() { return hubversion; } @@ -527,4 +544,14 @@ public Boolean isValidHomeWizard() { return true; } + public Boolean isValidOpenhab() { + if(this.getOpenhabaddress() == null || this.getOpenhabaddress().getDevices().size() <= 0) + return false; + + List devicesList = this.getOpenhabaddress().getDevices(); + if(devicesList.get(0).getIp().contains(Configuration.DEFAULT_ADDRESS)) + return false; + + return true; + } } diff --git a/src/main/java/com/bwssystems/HABridge/plugins/openhab/OpenHABHome.java b/src/main/java/com/bwssystems/HABridge/plugins/openhab/OpenHABHome.java new file mode 100644 index 00000000..3057fb8c --- /dev/null +++ b/src/main/java/com/bwssystems/HABridge/plugins/openhab/OpenHABHome.java @@ -0,0 +1,82 @@ +package com.bwssystems.HABridge.plugins.openhab; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.bwssystems.HABridge.BridgeSettings; +import com.bwssystems.HABridge.Home; +import com.bwssystems.HABridge.NamedIP; +import com.bwssystems.HABridge.api.CallItem; +import com.bwssystems.HABridge.dao.DeviceDescriptor; +import com.bwssystems.HABridge.hue.ColorData; +import com.bwssystems.HABridge.hue.MultiCommandUtil; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +public class OpenHABHome implements Home { + private static final Logger log = LoggerFactory.getLogger(OpenHABHome.class); + private Map openhabMap; + private Boolean validOpenhab; + private Gson aGsonHandler; + private boolean closed; + + public OpenHABHome(BridgeSettings bridgeSettings) { + super(); + closed = true; + createHome(bridgeSettings); + closed = false; + } + + + @Override + public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity, + Integer targetBri, Integer targetBriInc, ColorData colorData, DeviceDescriptor device, String body) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Object getItems(String type) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Home createHome(BridgeSettings bridgeSettings) { + openhabMap = null; + aGsonHandler = null; + validOpenhab = bridgeSettings.getBridgeSettingsDescriptor().isValidOpenhab(); + log.info("OpenHAB Home created." + (validOpenhab ? "" : " No OpenHABs configured.")); + if(validOpenhab) { + openhabMap = new HashMap(); + aGsonHandler = + new GsonBuilder() + .create(); + Iterator theList = bridgeSettings.getBridgeSettingsDescriptor().getHassaddress().getDevices().iterator(); + while(theList.hasNext() && validOpenhab) { + NamedIP anOpenhab = theList.next(); + try { + openhabMap.put(anOpenhab.getName(), new OpenHABInstance(anOpenhab)); + } catch (Exception e) { + log.error("Cannot get hass (" + anOpenhab.getName() + ") setup, Exiting with message: " + e.getMessage(), e); + validOpenhab = false; + } + } + } + return this; + } + + @Override + public void closeHome() { + if(!closed) { + + } + + } + + +} diff --git a/src/main/java/com/bwssystems/HABridge/plugins/openhab/OpenHABInstance.java b/src/main/java/com/bwssystems/HABridge/plugins/openhab/OpenHABInstance.java new file mode 100644 index 00000000..f81306d2 --- /dev/null +++ b/src/main/java/com/bwssystems/HABridge/plugins/openhab/OpenHABInstance.java @@ -0,0 +1,116 @@ +package com.bwssystems.HABridge.plugins.openhab; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.bwssystems.HABridge.NamedIP; +import com.bwssystems.HABridge.api.NameValue; +import com.bwssystems.HABridge.plugins.http.HTTPHandler; +import com.bwssystems.HABridge.plugins.http.HTTPHome; +import com.google.gson.Gson; + +public class OpenHABInstance { + private static final Logger log = LoggerFactory.getLogger(OpenHABInstance.class); + private NamedIP theOpenHAB; + private HTTPHandler anHttpHandler; + + public OpenHABInstance(NamedIP openhabLocation) { + super(); + anHttpHandler = HTTPHome.getHandler(); + theOpenHAB = openhabLocation; + } + + public NamedIP getOpenHABAddress() { + return theOpenHAB; + } + + public void setOpenHABAddress(NamedIP openhabAddress) { + this.theOpenHAB = openhabAddress; + } + + public Boolean callCommand(String aCommand) { + log.debug("calling HomeAssistant: " + aCommand); + String aUrl = null; + if(theOpenHAB.getSecure() != null && theOpenHAB.getSecure()) + aUrl = "https"; + else + aUrl = "http"; +/* String domain = aCommand.getEntityId().substring(0, aCommand.getEntityId().indexOf(".")); + aUrl = aUrl + "://" + theOpenHAB.getIp() + ":" + theOpenHAB.getPort() + "/api/services/"; + if(domain.equals("group")) + aUrl = aUrl + "homeassistant"; + else + aUrl = aUrl + domain; + String aBody = "{\"entity_id\":\"" + aCommand.getEntityId() + "\""; + NameValue[] headers = null; + if(theOpenHAB.getPassword() != null && !theOpenHAB.getPassword().isEmpty()) { + NameValue password = new NameValue(); + password.setName("x-ha-access"); + password.setValue(theOpenHAB.getPassword()); + headers = new NameValue[1]; + headers[0] = password; + } + if(aCommand.getState().equalsIgnoreCase("on")) { + aUrl = aUrl + "/turn_on"; + if(aCommand.getBri() != null) + aBody = aBody + ",\"brightness\":" + aCommand.getBri() + "}"; + else + aBody = aBody + "}"; + } + else { + aUrl = aUrl + "/turn_off"; + 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 + ">"); + */ + return true; + } + + public List getDevices() { + List theDeviceStates = null; + OpenHABItem[] theOpenhabStates; + String theUrl = null; + String theData; + NameValue[] headers = null; + if(theOpenHAB.getPassword() != null && !theOpenHAB.getPassword().isEmpty()) { + NameValue password = new NameValue(); + password.setName("x-ha-access"); + password.setValue(theOpenHAB.getPassword()); + headers = new NameValue[1]; + headers[0] = password; + } + if(theOpenHAB.getSecure() != null && theOpenHAB.getSecure()) + theUrl = "https"; + else + theUrl = "http"; + theUrl = theUrl + "://" + theOpenHAB.getIp() + ":" + theOpenHAB.getPort() + "/rest/items?recursive=false"; + theData = anHttpHandler.doHttpRequest(theUrl, HttpGet.METHOD_NAME, "application/json", null, headers); + if(theData != null) { + log.debug("GET OpenHAB States - data: " + theData); + theOpenhabStates = new Gson().fromJson(theData, OpenHABItem[].class); + if(theOpenhabStates == null) { + log.warn("Cannot get an devices for OpenHAB " + theOpenHAB.getName() + " as response is not parsable."); + } + else { + theDeviceStates = new ArrayList(Arrays.asList(theOpenhabStates)); + } + } + else + log.warn("Cannot get an devices for OpenHAB " + theOpenHAB.getName() + " http call failed."); + return theDeviceStates; + } + + + protected void closeClient() { + anHttpHandler.closeHandler(); + anHttpHandler = null; + } +} diff --git a/src/main/java/com/bwssystems/HABridge/plugins/openhab/OpenHABItem.java b/src/main/java/com/bwssystems/HABridge/plugins/openhab/OpenHABItem.java new file mode 100644 index 00000000..82efa72d --- /dev/null +++ b/src/main/java/com/bwssystems/HABridge/plugins/openhab/OpenHABItem.java @@ -0,0 +1,110 @@ + +package com.bwssystems.HABridge.plugins.openhab; + +import java.util.List; +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +public class OpenHABItem { + + @SerializedName("link") + @Expose + private String link; + @SerializedName("state") + @Expose + private String state; + @SerializedName("type") + @Expose + private String type; + @SerializedName("name") + @Expose + private String name; + @SerializedName("label") + @Expose + private String label; + @SerializedName("category") + @Expose + private String category; + @SerializedName("tags") + @Expose + private List tags = null; + @SerializedName("groupNames") + @Expose + private List groupNames = null; + @SerializedName("stateDescription") + @Expose + private StateDescription stateDescription; + + public String getLink() { + return link; + } + + public void setLink(String link) { + this.link = link; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } + + public List getTags() { + return tags; + } + + public void setTags(List tags) { + this.tags = tags; + } + + public List getGroupNames() { + return groupNames; + } + + public void setGroupNames(List groupNames) { + this.groupNames = groupNames; + } + + public StateDescription getStateDescription() { + return stateDescription; + } + + public void setStateDescription(StateDescription stateDescription) { + this.stateDescription = stateDescription; + } + +} diff --git a/src/main/java/com/bwssystems/HABridge/plugins/openhab/Option.java b/src/main/java/com/bwssystems/HABridge/plugins/openhab/Option.java new file mode 100644 index 00000000..3c705146 --- /dev/null +++ b/src/main/java/com/bwssystems/HABridge/plugins/openhab/Option.java @@ -0,0 +1,32 @@ + +package com.bwssystems.HABridge.plugins.openhab; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +public class Option { + + @SerializedName("value") + @Expose + private String value; + @SerializedName("label") + @Expose + private String label; + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + +} diff --git a/src/main/java/com/bwssystems/HABridge/plugins/openhab/StateDescription.java b/src/main/java/com/bwssystems/HABridge/plugins/openhab/StateDescription.java new file mode 100644 index 00000000..5205f06e --- /dev/null +++ b/src/main/java/com/bwssystems/HABridge/plugins/openhab/StateDescription.java @@ -0,0 +1,44 @@ + +package com.bwssystems.HABridge.plugins.openhab; + +import java.util.List; +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +public class StateDescription { + + @SerializedName("pattern") + @Expose + private String pattern; + @SerializedName("readOnly") + @Expose + private Boolean readOnly; + @SerializedName("options") + @Expose + private List