Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reporting configuration for temperature and humidity channels #870

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@
package org.openhab.binding.zigbee.internal.converter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;

import org.eclipse.jdt.annotation.NonNull;
import org.openhab.binding.zigbee.ZigBeeBindingConstants;
import org.openhab.binding.zigbee.converter.ZigBeeBaseChannelConverter;
import org.openhab.binding.zigbee.handler.ZigBeeThingHandler;
import org.openhab.binding.zigbee.internal.converter.config.ZclReportingConfig;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ThingUID;
Expand All @@ -46,6 +51,12 @@ public class ZigBeeConverterRelativeHumidity extends ZigBeeBaseChannelConverter
private ZclRelativeHumidityMeasurementCluster cluster;
private ZclAttribute attribute;

private static BigDecimal CHANGE_DEFAULT = new BigDecimal(1);
private static BigDecimal CHANGE_MIN = new BigDecimal(1);
private static BigDecimal CHANGE_MAX = new BigDecimal(100);

private ZclReportingConfig configReporting;

@Override
public Set<Integer> getImplementedClientClusters() {
return Collections.singleton(ZclRelativeHumidityMeasurementCluster.CLUSTER_ID);
Expand All @@ -65,14 +76,16 @@ public boolean initializeDevice() {
return false;
}

ZclReportingConfig reporting = new ZclReportingConfig(channel);

try {
CommandResult bindResponse = bind(serverCluster).get();
if (bindResponse.isSuccess()) {
// Configure reporting
ZclAttribute attribute = serverCluster
.getAttribute(ZclRelativeHumidityMeasurementCluster.ATTR_MEASUREDVALUE);
CommandResult reportingResponse = attribute
.setReporting(REPORTING_PERIOD_DEFAULT_MIN, REPORTING_PERIOD_DEFAULT_MAX, 50).get();
CommandResult reportingResponse = attribute.setReporting(reporting.getReportingTimeMin(),
reporting.getReportingTimeMax(), reporting.getReportingChange()).get();
handleReportingResponse(reportingResponse, POLLING_PERIOD_DEFAULT, REPORTING_PERIOD_DEFAULT_MAX);
}
} catch (InterruptedException | ExecutionException e) {
Expand Down Expand Up @@ -101,6 +114,13 @@ public boolean initializeConverter(ZigBeeThingHandler thing) {

// Add a listener, then request the status
cluster.addAttributeListener(this);

// Create a configuration handler and get the available options
configReporting = new ZclReportingConfig(channel);
configReporting.setAnalogue(CHANGE_DEFAULT, CHANGE_MIN, CHANGE_MAX);
configOptions = new ArrayList<>();
configOptions.addAll(configReporting.getConfiguration());

return true;
}

Expand All @@ -114,6 +134,24 @@ public void handleRefresh() {
attribute.readValue(0);
}

@Override
public void updateConfiguration(@NonNull Configuration currentConfiguration,
Map<String, Object> updatedParameters) {
if (configReporting.updateConfiguration(currentConfiguration, updatedParameters)) {
try {
ZclAttribute attribute = cluster.getAttribute(ZclRelativeHumidityMeasurementCluster.ATTR_MEASUREDVALUE);
CommandResult reportingResponse;
reportingResponse = attribute.setReporting(configReporting.getReportingTimeMin(),
configReporting.getReportingTimeMax(), configReporting.getReportingChange()).get();
handleReportingResponse(reportingResponse, configReporting.getPollingPeriod(),
configReporting.getReportingTimeMax());
} catch (InterruptedException | ExecutionException e) {
logger.debug("{}: Relative humidity measurement exception setting reporting", endpoint.getIeeeAddress(),
e);
}
}
}

@Override
public Channel getChannel(ThingUID thingUID, ZigBeeEndpoint endpoint) {
if (endpoint.getInputCluster(ZclRelativeHumidityMeasurementCluster.CLUSTER_ID) == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,24 @@
*/
package org.openhab.binding.zigbee.internal.converter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;

import org.eclipse.jdt.annotation.NonNull;
import org.openhab.binding.zigbee.ZigBeeBindingConstants;
import org.openhab.binding.zigbee.converter.ZigBeeBaseChannelConverter;
import org.openhab.binding.zigbee.handler.ZigBeeThingHandler;
import org.openhab.binding.zigbee.internal.converter.config.ZclReportingConfig;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.binding.builder.ChannelBuilder;
import org.openhab.core.types.Command;
import org.openhab.core.types.UnDefType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -47,6 +54,12 @@ public class ZigBeeConverterTemperature extends ZigBeeBaseChannelConverter imple
private ZclAttribute attribute;
private ZclAttribute attributeClient;

private static BigDecimal CHANGE_DEFAULT = new BigDecimal(15);
private static BigDecimal CHANGE_MIN = new BigDecimal(1);
private static BigDecimal CHANGE_MAX = new BigDecimal(200);

private ZclReportingConfig configReporting;

@Override
public Set<Integer> getImplementedClientClusters() {
return Collections.singleton(ZclTemperatureMeasurementCluster.CLUSTER_ID);
Expand All @@ -73,13 +86,16 @@ public boolean initializeDevice() {
return false;
}

ZclReportingConfig reporting = new ZclReportingConfig(channel);

try {
CommandResult bindResponse = bind(serverCluster).get();
if (bindResponse.isSuccess()) {
// Configure reporting
ZclAttribute attribute = serverCluster
.getAttribute(ZclTemperatureMeasurementCluster.ATTR_MEASUREDVALUE);
CommandResult reportingResponse = attribute.setReporting(1, REPORTING_PERIOD_DEFAULT_MAX, 10).get();
CommandResult reportingResponse = attribute.setReporting(reporting.getReportingTimeMin(),
reporting.getReportingTimeMax(), reporting.getReportingChange()).get();
handleReportingResponse(reportingResponse, POLLING_PERIOD_DEFAULT, REPORTING_PERIOD_DEFAULT_MAX);
} else {
logger.debug("{}: Failed to bind temperature measurement cluster", endpoint.getIeeeAddress());
Expand All @@ -101,6 +117,12 @@ public boolean initializeConverter(ZigBeeThingHandler thing) {
attribute = cluster.getAttribute(ZclTemperatureMeasurementCluster.ATTR_MEASUREDVALUE);
// Add a listener
cluster.addAttributeListener(this);

// Create a configuration handler and get the available options
configReporting = new ZclReportingConfig(channel);
configReporting.setAnalogue(CHANGE_DEFAULT, CHANGE_MIN, CHANGE_MAX);
configOptions = new ArrayList<>();
configOptions.addAll(configReporting.getConfiguration());
} else {
clusterClient = (ZclTemperatureMeasurementCluster) endpoint
.getOutputCluster(ZclTemperatureMeasurementCluster.CLUSTER_ID);
Expand Down Expand Up @@ -150,6 +172,23 @@ public void handleCommand(final Command command) {
attributeClient.reportValue(value);
}

@Override
public void updateConfiguration(@NonNull Configuration currentConfiguration,
Map<String, Object> updatedParameters) {
if (configReporting.updateConfiguration(currentConfiguration, updatedParameters)) {
try {
ZclAttribute attribute = cluster.getAttribute(ZclTemperatureMeasurementCluster.ATTR_MEASUREDVALUE);
CommandResult reportingResponse;
reportingResponse = attribute.setReporting(configReporting.getReportingTimeMin(),
configReporting.getReportingTimeMax(), configReporting.getReportingChange()).get();
handleReportingResponse(reportingResponse, configReporting.getPollingPeriod(),
configReporting.getReportingTimeMax());
} catch (InterruptedException | ExecutionException e) {
logger.debug("{}: Temperature measurement exception setting reporting", endpoint.getIeeeAddress(), e);
}
}
}

@Override
public Channel getChannel(ThingUID thingUID, ZigBeeEndpoint endpoint) {
if (endpoint.getOutputCluster(ZclTemperatureMeasurementCluster.CLUSTER_ID) == null
Expand All @@ -171,7 +210,12 @@ public void attributeUpdated(ZclAttribute attribute, Object val) {
if (attribute.getClusterType() == ZclClusterType.TEMPERATURE_MEASUREMENT
&& attribute.getId() == ZclTemperatureMeasurementCluster.ATTR_MEASUREDVALUE) {
logger.debug("{}: ZigBee attribute reports {}", endpoint.getIeeeAddress(), attribute);
updateChannelState(valueToTemperature((Integer) val));
Integer iVal = (Integer) val;
if (iVal == 0x8000) {
updateChannelState(UnDefType.UNDEF);
} else {
updateChannelState(valueToTemperature(iVal));
}
}
}
}