Skip to content

Commit

Permalink
Added converter for Binary Input cluster (#519)
Browse files Browse the repository at this point in the history
Added converter for Binary Input cluster

Signed-off-by: Witold Sowa <witold.sowa@gmail.com>
  • Loading branch information
wsowa authored and cdjackson committed Nov 30, 2019
1 parent b02c2e9 commit 86a2733
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 0 deletions.
1 change: 1 addition & 0 deletions org.openhab.binding.zigbee/README.md
Expand Up @@ -286,6 +286,7 @@ The following channels are supported -:
| electrical_activepower | ```ELECTRICAL_MEASUREMENT``` (0x0B04) | Number:Power | |
| electrical_rmscurrent | ```ELECTRICAL_MEASUREMENT``` (0x0B04) | Number:ElectricCurrent | |
| electrical_rmsvoltage | ```ELECTRICAL_MEASUREMENT``` (0x0B04) | Number:ElectricPotential | |
| binaryinput | ```BINARY_INPUT__BASIC``` (0x000F) | Switch | |
| ias_codetector | ```IAS_ZONE``` (0x0500) | Switch | |
| ias_contactportal1 | ```IAS_ZONE``` (0x0500) | Switch | |
| ias_fire | ```IAS_ZONE``` (0x0500) | Switch | |
Expand Down
Expand Up @@ -86,6 +86,10 @@ public class ZigBeeBindingConstants {
public static final String CHANNEL_LABEL_FANCONTROL = "Fan Control";
public static final ChannelTypeUID CHANNEL_FANCONTROL = new ChannelTypeUID("zigbee:fancontrol");

public static final String CHANNEL_NAME_BINARYINPUT = "binaryinput";
public static final String CHANNEL_LABEL_BINARYINPUT = "Binary Input";
public static final ChannelTypeUID CHANNEL_BINARYINPUT = new ChannelTypeUID("zigbee:binaryinput");

public static final String CHANNEL_NAME_IAS_CODETECTOR = "cosensor";
public static final String CHANNEL_LABEL_IAS_CODETECTOR = "Carbon Monoxide Detector";
public static final ChannelTypeUID CHANNEL_IAS_CODETECTOR = new ChannelTypeUID("zigbee:ias_cosensor");
Expand Down
@@ -0,0 +1,121 @@
/**
* Copyright (c) 2010-2019 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.zigbee.internal.converter;

import com.zsmartsystems.zigbee.CommandResult;
import com.zsmartsystems.zigbee.ZigBeeEndpoint;
import com.zsmartsystems.zigbee.zcl.ZclAttribute;
import com.zsmartsystems.zigbee.zcl.ZclAttributeListener;
import com.zsmartsystems.zigbee.zcl.clusters.ZclBinaryInputBasicCluster;
import com.zsmartsystems.zigbee.zcl.protocol.ZclClusterType;
import org.eclipse.smarthome.core.library.types.OnOffType;
import org.eclipse.smarthome.core.thing.Channel;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.eclipse.smarthome.core.thing.binding.builder.ChannelBuilder;
import org.openhab.binding.zigbee.ZigBeeBindingConstants;
import org.openhab.binding.zigbee.converter.ZigBeeBaseChannelConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutionException;

/**
* Converter for the binary input sensor.
*
* @author Witold Sowa
*
*/
public class ZigBeeConverterBinaryInput extends ZigBeeBaseChannelConverter implements ZclAttributeListener {
private Logger logger = LoggerFactory.getLogger(ZigBeeConverterBinaryInput.class);

private ZclBinaryInputBasicCluster binaryInputCluster;

@Override
public boolean initializeDevice() {
logger.debug("{}: Initialising device binary input cluster", endpoint.getIeeeAddress());

ZclBinaryInputBasicCluster binaryInputCluster = (ZclBinaryInputBasicCluster) endpoint
.getInputCluster(ZclBinaryInputBasicCluster.CLUSTER_ID);
if (binaryInputCluster == null) {
logger.error("{}: Error opening binary input cluster", endpoint.getIeeeAddress());
return false;
}

try {
CommandResult bindResponse = bind(binaryInputCluster).get();
if (bindResponse.isSuccess()) {
// Configure reporting - no faster than once per second - no slower than 2 hours.
CommandResult reportingResponse = binaryInputCluster
.setPresentValueReporting(1, REPORTING_PERIOD_DEFAULT_MAX).get();
handleReportingResponse(reportingResponse, POLLING_PERIOD_DEFAULT, REPORTING_PERIOD_DEFAULT_MAX);
}
} catch (InterruptedException | ExecutionException e) {
logger.error("{}: Exception setting reporting ", endpoint.getIeeeAddress(), e);
return false;
}
return true;
}

@Override
public boolean initializeConverter() {
binaryInputCluster = (ZclBinaryInputBasicCluster) endpoint.getInputCluster(ZclBinaryInputBasicCluster.CLUSTER_ID);
if (binaryInputCluster == null) {
logger.error("{}: Error opening binary input cluster", endpoint.getIeeeAddress());
return false;
}

binaryInputCluster.addAttributeListener(this);
return true;
}

@Override
public void disposeConverter() {
logger.debug("{}: Closing device binary input cluster", endpoint.getIeeeAddress());

binaryInputCluster.removeAttributeListener(this);
}

@Override
public void handleRefresh() {
binaryInputCluster.getPresentValue(0);
}

@Override
public Channel getChannel(ThingUID thingUID, ZigBeeEndpoint endpoint) {
if (endpoint.getInputCluster(ZclBinaryInputBasicCluster.CLUSTER_ID) == null) {
logger.trace("{}: Binary input sensing cluster not found", endpoint.getIeeeAddress());
return null;
}

return ChannelBuilder
.create(createChannelUID(thingUID, endpoint, ZigBeeBindingConstants.CHANNEL_NAME_BINARYINPUT),
ZigBeeBindingConstants.ITEM_TYPE_SWITCH)
.withType(ZigBeeBindingConstants.CHANNEL_BINARYINPUT)
.withLabel(ZigBeeBindingConstants.CHANNEL_LABEL_BINARYINPUT)
.withProperties(createProperties(endpoint)).build();
}

@Override
public void attributeUpdated(ZclAttribute attribute, Object val) {
logger.debug("{}: ZigBee attribute reports {}", endpoint.getIeeeAddress(), attribute);
if (attribute.getCluster() == ZclClusterType.BINARY_INPUT_BASIC
&& attribute.getId() == ZclBinaryInputBasicCluster.ATTR_PRESENTVALUE) {
Boolean value = (Boolean) val;
if (value == Boolean.TRUE) {
updateChannelState(OnOffType.ON);
} else {
updateChannelState(OnOffType.OFF);
}
}
}
}
Expand Up @@ -42,6 +42,7 @@ public ZigBeeDefaultChannelConverterProvider() {
channelMap.put(ZigBeeBindingConstants.CHANNEL_DOORLOCK_STATE, ZigBeeConverterDoorLock.class);
channelMap.put(ZigBeeBindingConstants.CHANNEL_ELECTRICAL_ACTIVEPOWER, ZigBeeConverterMeasurementPower.class);
channelMap.put(ZigBeeBindingConstants.CHANNEL_HUMIDITY_VALUE, ZigBeeConverterRelativeHumidity.class);
channelMap.put(ZigBeeBindingConstants.CHANNEL_BINARYINPUT, ZigBeeConverterBinaryInput.class);
channelMap.put(ZigBeeBindingConstants.CHANNEL_IAS_CONTACTPORTAL1, ZigBeeConverterIasContactPortal1.class);
channelMap.put(ZigBeeBindingConstants.CHANNEL_IAS_CODETECTOR, ZigBeeConverterIasCoDetector.class);
channelMap.put(ZigBeeBindingConstants.CHANNEL_IAS_FIREINDICATION, ZigBeeConverterIasFireIndicator.class);
Expand Down
Expand Up @@ -92,6 +92,15 @@
<state pattern="%.1f %unit%" readOnly="true" />
</channel-type>

<!-- Binary input sensor -->
<channel-type id="binaryinput">
<item-type>Switch</item-type>
<label>Binary Input Sensor</label>
<description>Indicates a binary input sensor state</description>
<category></category>
<state readOnly="true"></state>
</channel-type>

<!-- IAS Contact Channel -->
<channel-type id="ias_contactportal1">
<item-type>Switch</item-type>
Expand Down

0 comments on commit 86a2733

Please sign in to comment.