Finished Domoticz impl and secure calls for Home Assistant

This commit is contained in:
Admin
2017-01-25 16:24:44 -06:00
parent 6abe1b082c
commit 4b7ba0fabe
8 changed files with 59 additions and 40 deletions

View File

@@ -5,7 +5,7 @@
<groupId>com.bwssystems.HABridge</groupId>
<artifactId>ha-bridge</artifactId>
<version>4.1.0beta3</version>
<version>4.1.0beta4</version>
<packaging>jar</packaging>

View File

@@ -3,6 +3,7 @@ package com.bwssystems.HABridge.plugins.domoticz;
public class DomoticzDevice {
private String devicetype;
private String devicename;
private String idx;
private String domoticzaddress;
private String domoticzname;
public String getDevicetype() {
@@ -17,6 +18,12 @@ public class DomoticzDevice {
public void setDevicename(String devicename) {
this.devicename = devicename;
}
public String getIdx() {
return idx;
}
public void setIdx(String idx) {
this.idx = idx;
}
public String getDomoticzaddress() {
return domoticzaddress;
}

View File

@@ -57,7 +57,8 @@ public class DomoticzHandler {
DomoticzDevice aNewDomoticzDevice = new DomoticzDevice();
aNewDomoticzDevice.setDevicetype(theDevice.getType());
aNewDomoticzDevice.setDevicename(theDevice.getName());
aNewDomoticzDevice.setDomoticzaddress(domoticzAddress.getIp());
aNewDomoticzDevice.setIdx(theDevice.getIdx());
aNewDomoticzDevice.setDomoticzaddress(domoticzAddress.getIp() + ":" + domoticzAddress.getPort());
aNewDomoticzDevice.setDomoticzname(domoticzAddress.getName());
deviceList.add(aNewDomoticzDevice);

View File

@@ -65,12 +65,7 @@ public class DomoticzHome implements Home {
Iterator<DomoticzDevice> devices = theSourceList.iterator();
while(devices.hasNext()) {
DomoticzDevice theDevice = devices.next();
DomoticzDevice aNewDomoticzDevice = new DomoticzDevice();
aNewDomoticzDevice.setDevicetype(theDevice.getDevicetype());
aNewDomoticzDevice.setDevicename(theDevice.getDevicename());
aNewDomoticzDevice.setDomoticzaddress(domoticzs.get(theKey).getDomoticzAddress().getIp());
aNewDomoticzDevice.setDomoticzname(theKey);
theDeviceList.add(aNewDomoticzDevice);
theDeviceList.add(theDevice);
}
anHttpHandler = new HTTPHandler();
return true;

View File

@@ -4,6 +4,7 @@ public class HassDevice {
private State deviceState;
private String deviceName;
private String domain;
private Boolean secure;
private String hassaddress;
private String hassname;
public State getDeviceState() {
@@ -24,6 +25,12 @@ public class HassDevice {
public void setDomain(String domain) {
this.domain = domain;
}
public Boolean getSecure() {
return secure;
}
public void setSecure(Boolean secure) {
this.secure = secure;
}
public String getHassaddress() {
return hassaddress;
}

View File

@@ -36,7 +36,12 @@ public class HomeAssistant {
public Boolean callCommand(HassCommand aCommand) {
log.debug("calling HomeAssistant: " + aCommand.getHassName() + " - "
+ aCommand.getEntityId() + " - " + aCommand.getState() + " - " + aCommand.getBri());
String aUrl = "http://" + hassAddress.getIp() + ":" + hassAddress.getPort() + "/api/services/" + aCommand.getEntityId().substring(0, aCommand.getEntityId().indexOf("."));
String aUrl = null;
if(hassAddress.getSecure())
aUrl = "https";
else
aUrl = "http";
aUrl = aUrl + "://" + hassAddress.getIp() + ":" + hassAddress.getPort() + "/api/services/" + aCommand.getEntityId().substring(0, aCommand.getEntityId().indexOf("."));
String aBody = "{\"entity_id\":\"" + aCommand.getEntityId() + "\"";
NameValue[] headers = null;
if(hassAddress.getPassword() != null && !hassAddress.getPassword().isEmpty()) {
@@ -75,7 +80,11 @@ public class HomeAssistant {
headers = new NameValue[1];
headers[0] = password;
}
theUrl = "http://" + hassAddress.getIp() + ":" + hassAddress.getPort() + "/api/states";
if(hassAddress.getSecure())
theUrl = "https";
else
theUrl = "http";
theUrl = theUrl + "://" + hassAddress.getIp() + ":" + hassAddress.getPort() + "/api/states";
theData = anHttpHandler.doHttpRequest(theUrl, HttpGet.METHOD_NAME, "application/json", null, headers);
if(theData != null) {
log.debug("GET Hass States - data: " + theData);

View File

@@ -947,11 +947,11 @@ app.controller ('SystemController', function ($scope, $location, $http, $window,
}
}
};
$scope.addHasstoSettings = function (newhassname, newhassip, newhassport, newhasspassword) {
$scope.addHasstoSettings = function (newhassname, newhassip, newhassport, newhasspassword, newhasssecure) {
if($scope.bridge.settings.hassaddress === undefined || $scope.bridge.settings.hassaddress === null) {
$scope.bridge.settings.hassaddress = { devices: [] };
}
var newhass = {name: newhassname, ip: newhassip, port: newhassport, password: newhasspassword }
var newhass = {name: newhassname, ip: newhassip, port: newhassport, password: newhasspassword, secure: newhasssecure }
$scope.bridge.settings.hassaddress.devices.push(newhass);
$scope.newhassname = null;
$scope.newhassip = null;
@@ -2141,47 +2141,42 @@ app.controller('DomoticzController', function ($scope, $location, $http, bridgeS
};
$scope.buildDeviceUrls = function (domoticzdevice, dim_control) {
var preOnCmd = "";
var preDimCmd = "";
var preOffCmd = "";
var preCmd = "";
var postOnCmd = "";
var postDimCmd = "";
var postOffCmd = "";
var nameCmd = "";
var aDeviceType;
var postCmd = "";
if(domoticzdevice.devicetype === "Scene") {
aDeviceType = "scene";
preOnCmd = "/SceneService!SceneCmd=Set!SceneName=";
preOffCmd = preOnCmd;
preCmd = "/json.htm?type=command&param=switchscene&idx="
postOnCmd = "&switchcmd=On";
postOffCmd = "&switchcmd=Off";
}
else {
aDeviceType = "switch";
preOnCmd = "/DeviceService!DeviceCmd=SetDevice!DeviceValue=On";
preDimCmd = "/DeviceService!DeviceCmd=SetDevice!DeviceValue=Dim!DevicePercent=";
preOffCmd = "/DeviceService!DeviceCmd=SetDevice!DeviceValue=Off";
nameCmd = "!DeviceName=";
preCmd = "/json.htm?type=command&param=switchlight&idx="
postOnCmd = "&switchcmd=On";
postDimCmd = "&switchcmd=Set%20Level&level=";
postOffCmd = "&switchcmd=Off";
}
if((dim_control.indexOf("byte") >= 0 || dim_control.indexOf("percent") >= 0 || dim_control.indexOf("math") >= 0) && aDeviceType === "switch")
dimpayload = "http://" + domoticzdevice.domoticzaddress
+ preDimCmd
+ dim_control
+ nameCmd
+ domoticzdevice.devicename.replaceAll(" ", "%20")
+ postCmd;
+ preCmd
+ domoticzdevice.idx
+ postDimCmd
+ dim_control;
else
dimpayload = "http://" + domoticzdevice.domoticzaddress
+ preOnCmd
+ nameCmd
+ domoticzdevice.devicename.replaceAll(" ", "%20")
+ postCmd;
dimpayload = null;
onpayload = "http://" + domoticzdevice.domoticzaddress
+ preOnCmd
+ nameCmd
+ domoticzdevice.devicename.replaceAll(" ", "%20")
+ postCmd;
+ preCmd
+ domoticzdevice.idx
+ postOnCmd;
offpayload = "http://" + domoticzdevice.domoticzaddress
+ preOffCmd
+ nameCmd
+ domoticzdevice.haldevicename.replaceAll(" ", "%20")
+ postCmd;
+ preCmd
+ domoticzdevice.idx
+ postOffCmd;
bridgeService.buildUrls(onpayload, dimpayload, offpayload, false, domoticzdevice.devicename + "-" + domoticzdevice.domoticzname, domoticzdevice.devicename, domoticzdevice.domoticzname, aDeviceType, "domoticzDevice", null, null);
$scope.device = bridgeService.state.device;
bridgeService.editNewDevice($scope.device);

View File

@@ -277,6 +277,7 @@
<th>IP</th>
<th>Port</th>
<th>Password (opt)</th>
<th>Use SSL</th>
<th>Manage</th>
</tr>
</thead>
@@ -286,6 +287,7 @@
<td>{{hass.port}}</td>
<td ng-if="hass.password">*******</td>
<td ng-if="!hass.password"> </td>
<td>{{hass.secure}}</td>
<td><button class="btn btn-danger" type="submit"
ng-click="removeHasstoSettings(hass.name, hass.ip)">Del</button></td>
</tr>
@@ -302,8 +304,11 @@
<td><input id="bridge-settings-next-hass-password"
class="form-control" type="password" ng-model="newhasspassword"
placeholder="Home Assistant password (opt)"></td>
<td><input type="checkbox"
ng-model="newhasssecure" ng-true-value=true
ng-false-value=false></td>
<td><button class="btn btn-success" type="submit"
ng-click="addHasstoSettings(newhassname, newhassip, newhassport, newhasspassword)">Add</button></td>
ng-click="addHasstoSettings(newhassname, newhassip, newhassport, newhasspassword, newhasssecure)">Add</button></td>
</tr>
</table></td>
</tr>