Skip to content

Commit

Permalink
Optional homekit integration.
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitriy Ponomarev <diamond5170@gmail.com>
  • Loading branch information
MakeSimpleOrg committed Jun 4, 2017
1 parent 8b6bf92 commit f682e2f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
<description>Default name for room, if no room specified.</description>
<default>No Room</default>
</parameter>

<parameter name="homekitIntegration" groupName="binding" type="boolean" required="false">
<label>Homekit integration</label>
<description>If enabled, homekit tags are created for all supported devices. Please read Homekit add-on instructions.</description>
<default>true</default>
</parameter>
</config-description>

</config-description:config-descriptions>
15 changes: 8 additions & 7 deletions addons/binding/org.openhab.binding.vera/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ The textual configuration (via \*.thing files) isn't useful because the resultin

### Vera Controller (Bridge)

| Name | Type | Description |
|-----------------|---------------|----------------------------------------------------------------------------------------------------|
| veraIpAddress | string | The IP address or hostname of the Vera controller. |
| veraPort | int | The port of the Vera controller |
| pollingInterval | int | Refresh all values (name, room, state) for all devices and scenes. |
| clearNames | boolean | Remove digits, slashes and double spaces from all names. Good for some voice recognition services. |
| defaulRoomName | string | Default name for room, if no room specified. |
| Name | Type | Description |
|---------------------|---------------|----------------------------------------------------------------------------------------------------------|
| veraIpAddress | string | The IP address or hostname of the Vera controller. |
| veraPort | int | The port of the Vera controller |
| pollingInterval | int | Refresh all values (name, room, state) for all devices and scenes. |
| clearNames | boolean | Remove digits, slashes and double spaces from all names. Good for some voice recognition services. |
| defaulRoomName | string | Default name for room, if no room specified. |
| homekitIntegration | boolean | If enabled, homekit tags are created for all supported devices. Please read Homekit add-on instructions. |

### Vera Device

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public class VeraBindingConstants {
public static final String BRIDGE_CONFIG_POLLING_INTERVAL = "pollingInterval";
public static final String BRIDGE_CONFIG_CLEAR_NAMES = "clearNames";
public static final String BRIDGE_CONFIG_DEFAULT_ROOM_NAME = "defaultRoomName";
public static final String BRIDGE_CONFIG_HOMEKIT_INTEGRATION = "homekitIntegration";

public static final String DEVICE_CONFIG_ID = "deviceId";
public static final String DEVICE_PROP_CATEGORY = "category";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class VeraBridgeConfiguration {
private Integer pollingInterval;
private Boolean clearNames;
private String defaulRoomName;
private Boolean homekitIntegration;

public String getVeraIpAddress() {
return veraIpAddress;
Expand Down Expand Up @@ -64,12 +65,21 @@ public void setDefaulRoomName(String defaulRoomName) {
this.defaulRoomName = defaulRoomName;
}

public Boolean getHomekitIntegration() {
return homekitIntegration;
}

public void setHomekitIntegration(Boolean homekitIntegration) {
this.homekitIntegration = homekitIntegration;
}

@Override
public String toString() {
return new ToStringBuilder(this).append(BRIDGE_CONFIG_VERA_SERVER_IP_ADDRESS, this.getVeraIpAddress())
.append(BRIDGE_CONFIG_VERA_SERVER_PORT, this.getVeraPort())
.append(BRIDGE_CONFIG_POLLING_INTERVAL, this.getPollingInterval())
.append(BRIDGE_CONFIG_CLEAR_NAMES, this.getClearNames())
.append(BRIDGE_CONFIG_DEFAULT_ROOM_NAME, this.getDefaulRoomName()).toString();
.append(BRIDGE_CONFIG_DEFAULT_ROOM_NAME, this.getDefaulRoomName())
.append(BRIDGE_CONFIG_HOMEKIT_INTEGRATION, this.getHomekitIntegration()).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import static org.openhab.binding.vera.VeraBindingConstants.*;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.eclipse.smarthome.core.library.types.DecimalType;
Expand Down Expand Up @@ -70,14 +72,15 @@ public void run() {
if (device != null) {
logger.debug("Found {} device", device.getName());
updateLabelAndLocation(device.getName(), device.getRoomName());
addDeviceAsChannel(device);
addDeviceAsChannel(device,
veraBridgeHandler.getVeraBridgeConfiguration().getHomekitIntegration());
}
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR,
"Controller is not online");
}
} catch (Exception e) {
logger.error("Error occurred when adding device as channel: {}", e);
logger.error("Error occurred when adding device as channel: ", e);
if (getThing().getStatus() == ThingStatus.ONLINE) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR,
"Error occurred when adding device as channel: " + e.getMessage());
Expand Down Expand Up @@ -282,15 +285,16 @@ public void handleCommand(ChannelUID channelUID, final Command command) {
}
}

protected synchronized void addDeviceAsChannel(Device device) {
protected synchronized void addDeviceAsChannel(Device device, boolean homekitIntegration) {
if (device != null) {
logger.debug("Add device as channel: {}", device.getName());

HashMap<String, String> properties = new HashMap<>();
properties.put("deviceId", device.getId());

String id = "";
String id = null;
String acceptedItemType = "";
String tag = null;

int subcategory = Integer.parseInt(device.getSubcategory());
switch (device.getCategoryType()) {
Expand All @@ -304,16 +308,19 @@ protected synchronized void addDeviceAsChannel(Device device) {
case 3:
id = SWITCH_MULTILEVEL_CHANNEL;
acceptedItemType = "Dimmer";
tag = "Lighting";
break;
case 4:
id = SWITCH_COLOR_CHANNEL;
acceptedItemType = "Color";
tag = "Lighting";
break;
}
break;
case Switch:
id = SWITCH_BINARY_CHANNEL;
acceptedItemType = "Switch";
tag = "Switchable";
break;
case SecuritySensor:
switch (subcategory) {
Expand Down Expand Up @@ -348,6 +355,7 @@ protected synchronized void addDeviceAsChannel(Device device) {
case DoorLock:
id = DOORLOCK_CHANNEL;
acceptedItemType = "Switch";
tag = "Switchable";
break;
case WindowCovering:
id = SWITCH_ROLLERSHUTTER_CHANNEL;
Expand All @@ -364,10 +372,12 @@ protected synchronized void addDeviceAsChannel(Device device) {
case HumiditySensor:
id = SENSOR_HUMIDITY_CHANNEL;
acceptedItemType = "Number";
tag = "CurrentHumidity";
break;
case TemperatureSensor:
id = SENSOR_TEMPERATURE_CHANNEL;
acceptedItemType = "Number";
tag = "CurrentTemperature";
break;
case LightSensor:
id = SENSOR_LUMINOSITY_CHANNEL;
Expand Down Expand Up @@ -402,23 +412,22 @@ protected synchronized void addDeviceAsChannel(Device device) {
break;
}

// If at least one rule could mapped to a channel
if (!id.isEmpty()) {
addChannel(id, acceptedItemType, device.getName(), properties);
if (id != null) {
addChannel(id, acceptedItemType, device.getName(), properties, homekitIntegration ? tag : null);

logger.debug("Channel for device added with channel id: {}, accepted item type: {} and title: {}", id,
acceptedItemType, device.getName());

if (device.getBatterylevel() != null && !device.getBatterylevel().isEmpty()) {
addChannel(BATTERY_CHANNEL, "Number", "Battery", properties);
addChannel(BATTERY_CHANNEL, "Number", "Battery", properties, null);
}

if (device.getKwh() != null) {
addChannel(SENSOR_METER_KWH_CHANNEL, "Number", "Energy ALL", properties);
addChannel(SENSOR_METER_KWH_CHANNEL, "Number", "Energy ALL", properties, null);
}

if (device.getWatts() != null) {
addChannel(SENSOR_METER_W_CHANNEL, "Number", "Energy Current", properties);
addChannel(SENSOR_METER_W_CHANNEL, "Number", "Energy Current", properties, null);
}
} else {
// Thing status will not be updated because thing could have more than one channel
Expand All @@ -428,7 +437,7 @@ protected synchronized void addDeviceAsChannel(Device device) {
}

private synchronized void addChannel(String id, String acceptedItemType, String label,
HashMap<String, String> properties) {
HashMap<String, String> properties, String tag) {
String channelId = id + "-" + properties.get("deviceId");
boolean channelExists = false;
// Check if a channel for this device exist.
Expand All @@ -446,6 +455,11 @@ private synchronized void addChannel(String id, String acceptedItemType, String
channelBuilder.withType(channelTypeUID);
channelBuilder.withLabel(label);
channelBuilder.withProperties(properties);
if (tag != null) {
Set<String> tags = new HashSet<String>();
tags.add(tag);
channelBuilder.withDefaultTags(tags);
}
thingBuilder.withChannel(channelBuilder.build());
thingBuilder.withLabel(thing.getLabel());
updateThing(thingBuilder.build());
Expand Down

0 comments on commit f682e2f

Please sign in to comment.