Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for cheap HomeWizard SmartPlugs (Smartwares Smarthome Controller) #836

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ The Add/Edit tab will show you the fields to fill in for the above in a form, wh
The format of the item can be the default HTTP request which executes the URLs formatted as `https://<your stuff here>` as a GET. Other options to this are to select the HTTP Verb and add the data type and add a body that is passed with the request. Secure https is supported as well, just use `https://<your secure call here>`. When using POST and PUT, you have the ability to specify the body that will be sent with the request as well as the application type for the http call.

The valid device types are: "custom", "veraDevice", "veraScene", "harmonyActivity", "harmonyButton", "nestHomeAway", "nestThermoSet", "hueDevice", "halDevice",
"halButton", "halHome", "halThermoSet", "mqttMessage", "cmdDevice", "hassDevice", "tcpDevice", "udpDevice", "httpDevice", "domoticzDevice", "somfyDevice"
"halButton", "halHome", "halThermoSet", "mqttMessage", "cmdDevice", "hassDevice", "homewizardDevice", "tcpDevice", "udpDevice", "httpDevice", "domoticzDevice", "somfyDevice"

Filter Ip example:
```
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/bwssystems/HABridge/BridgeSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ public void buildSettings() {
theBridgeSettings.setHassconfigured(theBridgeSettings.isValidHass());
theBridgeSettings.setDomoticzconfigured(theBridgeSettings.isValidDomoticz());
theBridgeSettings.setSomfyconfigured(theBridgeSettings.isValidSomfy());
theBridgeSettings.setHomeWizardConfigured(theBridgeSettings.isValidHomeWizard());
// Lifx is either configured or not, so it does not need an update.
if(serverPortOverride != null)
theBridgeSettings.setServerPort(serverPortOverride);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ public class BridgeSettingsDescriptor {
@SerializedName("securityData")
@Expose
private String securityData;

@SerializedName("homewizardaddress")
@Expose
private IpList homewizardaddress;

private boolean settingsChanged;
private boolean veraconfigured;
Expand All @@ -107,7 +109,8 @@ public class BridgeSettingsDescriptor {
private boolean domoticzconfigured;
private boolean somfyconfigured;
private boolean lifxconfigured;

private boolean homewizardconfigured;

// Deprecated settings
private String haltoken;
private boolean upnpstrict;
Expand All @@ -127,7 +130,7 @@ public BridgeSettingsDescriptor() {
this.mqttconfigured = false;
this.hassconfigured = false;
this.domoticzconfigured = false;
this.somfyconfigured = false;
this.homewizardconfigured = false;
this.lifxconfigured = false;
this.farenheit = true;
this.securityData = null;
Expand Down Expand Up @@ -188,6 +191,9 @@ public IpList getFibaroAddress() {
public IpList getSomfyAddress() {
return somfyaddress;
}
public IpList getHomeWizardAddress() {
return homewizardaddress;
}
public void setVeraAddress(IpList veraAddress) {
this.veraaddress = veraAddress;
}
Expand All @@ -197,6 +203,9 @@ public void setFibaroAddress(IpList fibaroAddress) {
public void setSomfyAddress(IpList somfyAddress) {
this.somfyaddress = somfyAddress;
}
public void setHomeWizardAddress(IpList homewizardaddress) {
this.homewizardaddress = homewizardaddress;
}
public IpList getHarmonyAddress() {
return harmonyaddress;
}
Expand Down Expand Up @@ -236,6 +245,9 @@ public boolean isFibaroconfigured() {
public boolean isSomfyconfigured() {
return somfyconfigured;
}
public boolean isHomeWizardConfigured() {
return homewizardconfigured;
}
public void setVeraconfigured(boolean veraconfigured) {
this.veraconfigured = veraconfigured;
}
Expand All @@ -245,6 +257,9 @@ public void setFibaroconfigured(boolean fibaroconfigured) {
public void setSomfyconfigured(boolean somfyconfigured) {
this.somfyconfigured = somfyconfigured;
}
public void setHomeWizardConfigured(boolean homewizardconfigured) {
this.homewizardconfigured = homewizardconfigured;
}
public boolean isHarmonyconfigured() {
return harmonyconfigured;
}
Expand Down Expand Up @@ -492,4 +507,14 @@ public void updateHue(NamedIP aHue) {
this.setSettingsChanged(true);
}
}
public Boolean isValidHomeWizard() {
if(this.getHomeWizardAddress() == null || this.getHomeWizardAddress().getDevices().size() <= 0)
return false;

List<NamedIP> devicesList = this.getHomeWizardAddress().getDevices();
if(devicesList.get(0).getIp().contains(Configuration.DEFAULT_ADDRESS))
return false;

return true;
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/bwssystems/HABridge/DeviceMapTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class DeviceMapTypes {
public final static String[] EXEC_DEVICE_COMPAT = { "exec", "Execute Script/Program"};
public final static String[] CMD_DEVICE = { "cmdDevice", "Execute Command/Script/Program"};
public final static String[] HASS_DEVICE = { "hassDevice", "HomeAssistant Device"};
public final static String[] HOMEWIZARD_DEVICE = { "homewizardDevice", "HomeWizard Device"};
public final static String[] TCP_DEVICE = { "tcpDevice", "TCP Device"};
public final static String[] TCP_DEVICE_COMPAT = { "TCP", "TCP Device"};
public final static String[] UDP_DEVICE = { "udpDevice", "UDP Device"};
Expand All @@ -48,6 +49,7 @@ public DeviceMapTypes() {
deviceMapTypes.add(HARMONY_ACTIVITY);
deviceMapTypes.add(HARMONY_BUTTON);
deviceMapTypes.add(HASS_DEVICE);
deviceMapTypes.add(HOMEWIZARD_DEVICE);
deviceMapTypes.add(HTTP_DEVICE);
deviceMapTypes.add(HUE_DEVICE);
deviceMapTypes.add(LIFX_DEVICE);
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/bwssystems/HABridge/HomeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.bwssystems.HABridge.plugins.hal.HalHome;
import com.bwssystems.HABridge.plugins.harmony.HarmonyHome;
import com.bwssystems.HABridge.plugins.hass.HassHome;
import com.bwssystems.HABridge.plugins.homewizard.HomeWizardHome;
import com.bwssystems.HABridge.plugins.http.HTTPHome;
import com.bwssystems.HABridge.plugins.hue.HueHome;
import com.bwssystems.HABridge.plugins.lifx.LifxHome;
Expand Down Expand Up @@ -68,6 +69,10 @@ public void buildHomes(BridgeSettings bridgeSettings, UDPDatagramSender aUdpData
aHome = new HassHome(bridgeSettings);
resourceList.put(DeviceMapTypes.HASS_DEVICE[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.HASS_DEVICE[DeviceMapTypes.typeIndex], aHome);
// Setup the HomeWizard configuration if available
aHome = new HomeWizardHome(bridgeSettings);
resourceList.put(DeviceMapTypes.HOMEWIZARD_DEVICE[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.HOMEWIZARD_DEVICE[DeviceMapTypes.typeIndex], aHome);
//setup the command execution Home
aHome = new CommandHome(bridgeSettings);
homeList.put(DeviceMapTypes.EXEC_DEVICE_COMPAT[DeviceMapTypes.typeIndex], aHome);
Expand Down Expand Up @@ -96,7 +101,7 @@ public void buildHomes(BridgeSettings bridgeSettings, UDPDatagramSender aUdpData
aHome = new FibaroHome(bridgeSettings);
resourceList.put(DeviceMapTypes.FIBARO_DEVICE[DeviceMapTypes.typeIndex], aHome);
resourceList.put(DeviceMapTypes.FIBARO_SCENE[DeviceMapTypes.typeIndex], aHome);
//setup the Domoticz configuration if available
//setup the Domoticz configuration if available
aHome = new DomoticzHome(bridgeSettings);
homeList.put(DeviceMapTypes.DOMOTICZ_DEVICE[DeviceMapTypes.typeIndex], aHome);
resourceList.put(DeviceMapTypes.DOMOTICZ_DEVICE[DeviceMapTypes.typeIndex], aHome);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ private void setupEndpoints() {
return homeManager.findResource(DeviceMapTypes.HASS_DEVICE[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.HASS_DEVICE[DeviceMapTypes.typeIndex]);
}, new JsonTransformer());

get (API_CONTEXT + "/homewizard/devices", "application/json", (request, response) -> {
log.debug("Get HomeWizard Clients");
response.status(HttpStatus.SC_OK);
return homeManager.findResource(DeviceMapTypes.HOMEWIZARD_DEVICE[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.HOMEWIZARD_DEVICE[DeviceMapTypes.typeIndex]);
}, new JsonTransformer());

get (API_CONTEXT + "/domoticz/devices", "application/json", (request, response) -> {
log.debug("Get Domoticz Clients");
response.status(HttpStatus.SC_OK);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package com.bwssystems.HABridge.plugins.homewizard;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bwssystems.HABridge.BridgeSettings;
import com.bwssystems.HABridge.DeviceMapTypes;
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;

/**
* Control HomeWizard devices over HomeWizard Cloud
*
* @author Björn Rennfanz ([email protected])
*
*/
public class HomeWizardHome implements Home {

private static final Logger log = LoggerFactory.getLogger(HomeWizardHome.class);

private Map<String, HomeWizzardSmartPlugInfo> plugGateways;
private Boolean validHomeWizard;
private boolean closed;

public HomeWizardHome(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) {

String responseString = null;
if (!validHomeWizard) {

log.warn("Should not get here, no HomeWizard smart plug available");
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Should not get here, no HomeWizard smart plug available\", \"parameter\": \"/lights/"
+ lightId + "state\"}}]";
} else {

if (anItem.getType() != null && anItem.getType().trim().equalsIgnoreCase(DeviceMapTypes.HOMEWIZARD_DEVICE[DeviceMapTypes.typeIndex])) {

log.debug("Executing HUE api request to change activity to HomeWizard smart plug: " + anItem.getItem().toString());
String jsonToPost = anItem.getItem().toString();

HomeWizzardSmartPlugInfo homeWizzardHandler = getHomeWizzardHandler(device.getTargetDevice());
if(homeWizzardHandler == null) {
log.warn("Should not get here, no HomeWizard smart plug configured");
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Should not get here, no HomeWizard smart plug configured\", \"parameter\": \"/lights/"
+ lightId + "state\"}}]";
} else {
try {
homeWizzardHandler.execApply(jsonToPost);
} catch (Exception e) {

log.warn("Error posting request to HomeWizard smart plug");
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Error posting request to HomeWizard smart plug\", \"parameter\": \"/lights/" + lightId + "state\"}}]";
}
}
}
}

return responseString;
}

public HomeWizzardSmartPlugInfo getHomeWizzardHandler(String plugName) {
return plugGateways.get(plugName);
}

public List<HomeWizardSmartPlugDevice> getDevices() {

log.debug("consolidating devices for plug gateways");
Iterator<String> keys = plugGateways.keySet().iterator();
ArrayList<HomeWizardSmartPlugDevice> deviceList = new ArrayList<>();

while(keys.hasNext())
{
String key = keys.next();
for(HomeWizardSmartPlugDevice device : plugGateways.get(key).getDevices())
deviceList.add(device);
}

return deviceList;
}

@Override
public Object getItems(String type) {

if (validHomeWizard)
{
if (type.equalsIgnoreCase(DeviceMapTypes.HOMEWIZARD_DEVICE[DeviceMapTypes.typeIndex]))
{
return getDevices();
}
}

return null;
}

@Override
public Home createHome(BridgeSettings bridgeSettings) {

validHomeWizard = bridgeSettings.getBridgeSettingsDescriptor().isValidHomeWizard();
log.info("HomeWizard Home created. " + (validHomeWizard ? "" : "No HomeWizard gateways configured."));

if (validHomeWizard)
{
plugGateways = new HashMap<>();
Iterator<NamedIP> gatewaysList = bridgeSettings.getBridgeSettingsDescriptor().getHomeWizardAddress().getDevices().iterator();

while(gatewaysList.hasNext()) {

NamedIP gateway = gatewaysList.next();
plugGateways.put(gateway.getName(), new HomeWizzardSmartPlugInfo(gateway, gateway.getName()));
}
}

return this;
}

@Override
public void closeHome() {

log.debug("Closing Home.");
if(closed) {

log.debug("Home is already closed....");
return;
}

plugGateways = null;
closed = true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.bwssystems.HABridge.plugins.homewizard;

/**
* Control HomeWizard devices over HomeWizard Cloud
*
* @author Björn Rennfanz ([email protected])
*
*/
public class HomeWizardSmartPlugDevice {

private String name;
private String gateway;
private String id;
private String typeName;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getGateway() {
return gateway;
}

public void setGateway(String gateway) {
this.gateway = gateway;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getTypeName() {
return this.typeName;
}

public void setTypeName(String typeName) {
this.typeName = typeName;
}
}
Loading