-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add humidity and pressure converters (#149)
Signed-off-by: Chris Jackson <chris@cd-jackson.com>
- Loading branch information
Showing
6 changed files
with
232 additions
and
7 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
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
127 changes: 127 additions & 0 deletions
127
...ava/org/openhab/binding/zigbee/internal/converter/ZigBeeConverterAtmosphericPressure.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,127 @@ | ||
/** | ||
* Copyright (c) 2014-2017 by the respective copyright holders. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.openhab.binding.zigbee.internal.converter; | ||
|
||
import java.math.BigDecimal; | ||
|
||
import org.eclipse.smarthome.core.library.types.DecimalType; | ||
import org.eclipse.smarthome.core.thing.Channel; | ||
import org.eclipse.smarthome.core.thing.ThingUID; | ||
import org.openhab.binding.zigbee.ZigBeeBindingConstants; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.zsmartsystems.zigbee.ZigBeeEndpoint; | ||
import com.zsmartsystems.zigbee.zcl.ZclAttribute; | ||
import com.zsmartsystems.zigbee.zcl.ZclAttributeListener; | ||
import com.zsmartsystems.zigbee.zcl.clusters.ZclPressureMeasurementCluster; | ||
import com.zsmartsystems.zigbee.zcl.protocol.ZclClusterType; | ||
|
||
/** | ||
* Converter for the atmospheric pressure channel. | ||
* This channel will attempt to detect if the device is supporting the enhanced (scaled) value reports and use them if | ||
* they are available. | ||
* | ||
* @author Chris Jackson - Initial Contribution | ||
* | ||
*/ | ||
public class ZigBeeConverterAtmosphericPressure extends ZigBeeBaseChannelConverter implements ZclAttributeListener { | ||
private Logger logger = LoggerFactory.getLogger(ZigBeeConverterAtmosphericPressure.class); | ||
|
||
private ZclPressureMeasurementCluster cluster; | ||
|
||
/** | ||
* If enhancedScale is null, then the binding will use the MeasuredValue report, | ||
* otherwise it will use the ScaledValue report | ||
*/ | ||
private Integer enhancedScale = null; | ||
|
||
@Override | ||
public boolean initializeConverter() { | ||
cluster = (ZclPressureMeasurementCluster) endpoint.getInputCluster(ZclPressureMeasurementCluster.CLUSTER_ID); | ||
if (cluster == null) { | ||
logger.error("{}: Error opening device pressure measurement cluster", endpoint.getIeeeAddress()); | ||
return false; | ||
} | ||
|
||
// Check if the enhanced attributes are supported | ||
if (cluster.getScaledValue(Long.MAX_VALUE) != null) { | ||
enhancedScale = cluster.getScale(Long.MAX_VALUE); | ||
if (enhancedScale != null) { | ||
enhancedScale *= -1; | ||
} | ||
} | ||
|
||
cluster.bind(); | ||
|
||
// Add a listener, then request the status | ||
cluster.addAttributeListener(this); | ||
|
||
// Configure reporting - no faster than once per second - no slower than 10 minutes. | ||
if (enhancedScale != null) { | ||
cluster.setScaledValueReporting(1, REPORTING_PERIOD_DEFAULT_MAX, 0.1); | ||
} else { | ||
cluster.setMeasuredValueReporting(1, REPORTING_PERIOD_DEFAULT_MAX, 0.1); | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public void disposeConverter() { | ||
cluster.removeAttributeListener(this); | ||
} | ||
|
||
@Override | ||
public void handleRefresh() { | ||
if (enhancedScale != null) { | ||
cluster.getScaledValue(0); | ||
} else { | ||
cluster.getMeasuredValue(0); | ||
} | ||
} | ||
|
||
@Override | ||
public Channel getChannel(ThingUID thingUID, ZigBeeEndpoint endpoint) { | ||
if (endpoint.getInputCluster(ZclPressureMeasurementCluster.CLUSTER_ID) == null) { | ||
return null; | ||
} | ||
return createChannel(thingUID, endpoint, ZigBeeBindingConstants.CHANNEL_PRESSURE_VALUE, | ||
ZigBeeBindingConstants.ITEM_TYPE_NUMBER, "Atmospheric Pressure"); | ||
} | ||
|
||
@Override | ||
public void attributeUpdated(ZclAttribute attribute) { | ||
logger.debug("{}: ZigBee attribute reports {}", endpoint.getIeeeAddress(), attribute); | ||
if (attribute.getCluster() == ZclClusterType.PRESSURE_MEASUREMENT) { | ||
// Handle automatic reporting of the enhanced attribute configuration | ||
if (attribute.getId() == ZclPressureMeasurementCluster.ATTR_SCALE) { | ||
enhancedScale = (Integer) attribute.getLastValue(); | ||
if (enhancedScale != null) { | ||
enhancedScale *= -1; | ||
} | ||
return; | ||
} | ||
|
||
if (attribute.getId() == ZclPressureMeasurementCluster.ATTR_SCALEDVALUE && enhancedScale != null) { | ||
Integer value = (Integer) attribute.getLastValue(); | ||
if (value != null) { | ||
updateChannelState(new DecimalType(BigDecimal.valueOf(value, enhancedScale))); | ||
} | ||
return; | ||
} | ||
|
||
if (attribute.getId() == ZclPressureMeasurementCluster.ATTR_MEASUREDVALUE && enhancedScale == null) { | ||
Integer value = (Integer) attribute.getLastValue(); | ||
if (value != null) { | ||
updateChannelState(new DecimalType(BigDecimal.valueOf(value, 0))); | ||
} | ||
return; | ||
} | ||
} | ||
} | ||
} |
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
85 changes: 85 additions & 0 deletions
85
...n/java/org/openhab/binding/zigbee/internal/converter/ZigBeeConverterRelativeHumidity.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,85 @@ | ||
/** | ||
* Copyright (c) 2014-2017 by the respective copyright holders. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.openhab.binding.zigbee.internal.converter; | ||
|
||
import java.math.BigDecimal; | ||
|
||
import org.eclipse.smarthome.core.library.types.DecimalType; | ||
import org.eclipse.smarthome.core.thing.Channel; | ||
import org.eclipse.smarthome.core.thing.ThingUID; | ||
import org.openhab.binding.zigbee.ZigBeeBindingConstants; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.zsmartsystems.zigbee.ZigBeeEndpoint; | ||
import com.zsmartsystems.zigbee.zcl.ZclAttribute; | ||
import com.zsmartsystems.zigbee.zcl.ZclAttributeListener; | ||
import com.zsmartsystems.zigbee.zcl.clusters.ZclRelativeHumidityMeasurementCluster; | ||
import com.zsmartsystems.zigbee.zcl.protocol.ZclClusterType; | ||
|
||
/** | ||
* Converter for the relative humidity channel | ||
* | ||
* @author Chris Jackson - Initial Contribution | ||
* | ||
*/ | ||
public class ZigBeeConverterRelativeHumidity extends ZigBeeBaseChannelConverter implements ZclAttributeListener { | ||
private Logger logger = LoggerFactory.getLogger(ZigBeeConverterRelativeHumidity.class); | ||
|
||
private ZclRelativeHumidityMeasurementCluster cluster; | ||
|
||
@Override | ||
public boolean initializeConverter() { | ||
cluster = (ZclRelativeHumidityMeasurementCluster) endpoint | ||
.getInputCluster(ZclRelativeHumidityMeasurementCluster.CLUSTER_ID); | ||
if (cluster == null) { | ||
logger.error("{}: Error opening device relative humidity measurement cluster", endpoint.getIeeeAddress()); | ||
return false; | ||
} | ||
|
||
cluster.bind(); | ||
|
||
// Add a listener, then request the status | ||
cluster.addAttributeListener(this); | ||
|
||
// Configure reporting - no faster than once per second - no slower than 10 minutes. | ||
cluster.setMeasuredValueReporting(1, REPORTING_PERIOD_DEFAULT_MAX, 0.1); | ||
return true; | ||
} | ||
|
||
@Override | ||
public void disposeConverter() { | ||
cluster.removeAttributeListener(this); | ||
} | ||
|
||
@Override | ||
public void handleRefresh() { | ||
cluster.getMeasuredValue(0); | ||
} | ||
|
||
@Override | ||
public Channel getChannel(ThingUID thingUID, ZigBeeEndpoint endpoint) { | ||
if (endpoint.getInputCluster(ZclRelativeHumidityMeasurementCluster.CLUSTER_ID) == null) { | ||
return null; | ||
} | ||
return createChannel(thingUID, endpoint, ZigBeeBindingConstants.CHANNEL_HUMIDITY_VALUE, | ||
ZigBeeBindingConstants.ITEM_TYPE_NUMBER, "Relative Humidity"); | ||
} | ||
|
||
@Override | ||
public void attributeUpdated(ZclAttribute attribute) { | ||
logger.debug("{}: ZigBee attribute reports {}", endpoint.getIeeeAddress(), attribute); | ||
if (attribute.getCluster() == ZclClusterType.RELATIVE_HUMIDITY_MEASUREMENT | ||
&& attribute.getId() == ZclRelativeHumidityMeasurementCluster.ATTR_MEASUREDVALUE) { | ||
Integer value = (Integer) attribute.getLastValue(); | ||
if (value != null) { | ||
updateChannelState(new DecimalType(BigDecimal.valueOf(value, 2))); | ||
} | ||
} | ||
} | ||
} |