Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added converter for Binary Input cluster (#519)
Added converter for Binary Input cluster Signed-off-by: Witold Sowa <witold.sowa@gmail.com>
- Loading branch information
Showing
5 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
...c/main/java/org/openhab/binding/zigbee/internal/converter/ZigBeeConverterBinaryInput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters