diff --git a/org.openhab.binding.zigbee/src/main/java/org/openhab/binding/zigbee/internal/converter/ZigBeeConverterFanControl.java b/org.openhab.binding.zigbee/src/main/java/org/openhab/binding/zigbee/internal/converter/ZigBeeConverterFanControl.java index af099b3f6..a1c247a2c 100644 --- a/org.openhab.binding.zigbee/src/main/java/org/openhab/binding/zigbee/internal/converter/ZigBeeConverterFanControl.java +++ b/org.openhab.binding.zigbee/src/main/java/org/openhab/binding/zigbee/internal/converter/ZigBeeConverterFanControl.java @@ -16,12 +16,16 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +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.ZclFanControlConfig; +import org.openhab.core.config.core.Configuration; import org.openhab.core.library.types.DecimalType; import org.openhab.core.library.types.OnOffType; import org.openhab.core.thing.Channel; @@ -59,6 +63,8 @@ public class ZigBeeConverterFanControl extends ZigBeeBaseChannelConverter implem private ZclFanControlCluster cluster; private ZclAttribute fanModeAttribute; + private ZclFanControlConfig configFanControl; + @Override public Set getImplementedClientClusters() { return Collections.singleton(ZclFanControlCluster.CLUSTER_ID); @@ -149,6 +155,11 @@ public boolean initializeConverter(ZigBeeThingHandler thing) { // Add the listener cluster.addAttributeListener(this); + configFanControl = new ZclFanControlConfig(); + configFanControl.initialize(cluster); + configOptions = new ArrayList<>(); + configOptions.addAll(configFanControl.getConfiguration()); + return true; } @@ -179,6 +190,13 @@ public void handleCommand(final Command command) { fanModeAttribute.writeValue(value); } + @Override + public void updateConfiguration(@NonNull Configuration currentConfiguration, + Map updatedParameters) { + + configFanControl.updateConfiguration(currentConfiguration, updatedParameters); + } + @Override public Channel getChannel(ThingUID thingUID, ZigBeeEndpoint endpoint) { ZclFanControlCluster cluster = (ZclFanControlCluster) endpoint.getInputCluster(ZclFanControlCluster.CLUSTER_ID); diff --git a/org.openhab.binding.zigbee/src/main/java/org/openhab/binding/zigbee/internal/converter/config/ZclFanControlConfig.java b/org.openhab.binding.zigbee/src/main/java/org/openhab/binding/zigbee/internal/converter/config/ZclFanControlConfig.java new file mode 100644 index 000000000..1ea43b1a6 --- /dev/null +++ b/org.openhab.binding.zigbee/src/main/java/org/openhab/binding/zigbee/internal/converter/config/ZclFanControlConfig.java @@ -0,0 +1,134 @@ +/** + * Copyright (c) 2010-2020 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.config; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; +import java.util.concurrent.ExecutionException; + +import org.eclipse.jdt.annotation.NonNull; +import org.openhab.core.config.core.ConfigDescriptionParameter; +import org.openhab.core.config.core.ConfigDescriptionParameter.Type; +import org.openhab.core.config.core.ConfigDescriptionParameterBuilder; +import org.openhab.core.config.core.Configuration; +import org.openhab.core.config.core.ParameterOption; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.zsmartsystems.zigbee.zcl.ZclAttribute; +import com.zsmartsystems.zigbee.zcl.ZclCluster; +import com.zsmartsystems.zigbee.zcl.clusters.ZclFanControlCluster; + +/** + * Configuration handler for the {@link ZclFanControlConfig} + * + * @author Chris Jackson + * + */ +public class ZclFanControlConfig implements ZclClusterConfigHandler { + private Logger logger = LoggerFactory.getLogger(ZclFanControlConfig.class); + + private static final String CONFIG_ID = "zigbee_fancontrol_"; + private static final String CONFIG_MODESEQUENCE = CONFIG_ID + "modesequence"; + + private ZclFanControlCluster fanControlCluster; + + private final List parameters = new ArrayList<>(); + + @Override + public boolean initialize(ZclCluster cluster) { + fanControlCluster = (ZclFanControlCluster) cluster; + try { + Boolean result = fanControlCluster.discoverAttributes(false).get(); + if (!result) { + logger.debug("{}: Unable to get supported attributes for {}.", fanControlCluster.getZigBeeAddress(), + fanControlCluster.getClusterName()); + } + } catch (InterruptedException | ExecutionException e) { + logger.error("{}: Error getting supported attributes for {}. ", fanControlCluster.getZigBeeAddress(), + fanControlCluster.getClusterName(), e); + } + + // Build a list of configuration supported by this channel based on the attributes the cluster supports + List options = new ArrayList<>(); + if (fanControlCluster.isAttributeSupported(ZclFanControlCluster.ATTR_FANMODESEQUENCE)) { + options.add(new ParameterOption("0", "Low/Med/High")); + options.add(new ParameterOption("1", "Low/High")); + options.add(new ParameterOption("2", "Low/Med/High/Auto")); + options.add(new ParameterOption("3", "Low/High/Auto")); + options.add(new ParameterOption("4", "On/Auto")); + + parameters.add(ConfigDescriptionParameterBuilder.create(CONFIG_MODESEQUENCE, Type.INTEGER) + .withLabel("Fan Mode Sequence").withDescription("Possible fan modes that may be selected") + .withOptions(options).withMinimum(new BigDecimal(0)).withMaximum(new BigDecimal(4)).build()); + } + + return !parameters.isEmpty(); + } + + @Override + public List getConfiguration() { + return parameters; + } + + @Override + public boolean updateConfiguration(@NonNull Configuration currentConfiguration, + Map configurationParameters) { + + boolean updated = false; + for (Entry configurationParameter : configurationParameters.entrySet()) { + if (!configurationParameter.getKey().startsWith(CONFIG_ID)) { + continue; + } + // Ignore any configuration parameters that have not changed + if (Objects.equals(configurationParameter.getValue(), + currentConfiguration.get(configurationParameter.getKey()))) { + logger.debug("Configuration update: Ignored {} as no change", configurationParameter.getKey()); + continue; + } + + logger.debug("{}: Update LevelControl configuration property {}->{} ({})", + fanControlCluster.getZigBeeAddress(), configurationParameter.getKey(), + configurationParameter.getValue(), configurationParameter.getValue().getClass().getSimpleName()); + Integer response = null; + switch (configurationParameter.getKey()) { + case CONFIG_MODESEQUENCE: + response = configureAttribute(ZclFanControlCluster.ATTR_FANMODESEQUENCE, + configurationParameter.getValue()); + break; + default: + logger.warn("{}: Unhandled configuration property {}", fanControlCluster.getZigBeeAddress(), + configurationParameter.getKey()); + break; + } + + if (response != null) { + currentConfiguration.put(configurationParameter.getKey(), BigInteger.valueOf(response)); + updated = true; + } + } + + return updated; + } + + private Integer configureAttribute(int attributeId, Object value) { + ZclAttribute attribute = fanControlCluster.getAttribute(attributeId); + attribute.writeValue(((BigDecimal) (value)).intValue()); + return (Integer) attribute.readValue(0); + } +} diff --git a/org.openhab.binding.zigbee/src/main/java/org/openhab/binding/zigbee/internal/converter/config/ZclOnOffSwitchConfig.java b/org.openhab.binding.zigbee/src/main/java/org/openhab/binding/zigbee/internal/converter/config/ZclOnOffSwitchConfig.java index 1df67169c..547467dd9 100755 --- a/org.openhab.binding.zigbee/src/main/java/org/openhab/binding/zigbee/internal/converter/config/ZclOnOffSwitchConfig.java +++ b/org.openhab.binding.zigbee/src/main/java/org/openhab/binding/zigbee/internal/converter/config/ZclOnOffSwitchConfig.java @@ -82,7 +82,7 @@ public boolean initialize(ZclCluster cluster) { .withDefault("65535").withMinimum(new BigDecimal(0)).withMaximum(new BigDecimal(60000)).build()); } if (onoffCluster.isAttributeSupported(ZclOnOffCluster.ATTR_STARTUPONOFF)) { - options = new ArrayList(); + options = new ArrayList<>(); options.add(new ParameterOption("0", "OFF")); options.add(new ParameterOption("1", "ON")); parameters.add(ConfigDescriptionParameterBuilder.create(CONFIG_STARTUPONOFF, Type.INTEGER)