/** {@inheritDoc} */
  @Override
  public State handleEvent(ZWaveThingChannel channel, ZWaveCommandClassValueEvent event) {
    // We ignore any meter reports for item bindings configured with 'reset=true'
    // since we don't want to be updating the 'reset' switch
    if ("true".equalsIgnoreCase(channel.getArguments().get("reset"))) {
      return null;
    }

    String meterScale = channel.getArguments().get("type");
    String meterZero = channel.getArguments().get("zero"); // needs to be a config setting - not arg
    ZWaveMeterValueEvent meterEvent = (ZWaveMeterValueEvent) event;
    // logger.debug("Meter converter: scale {} <> {}", meterScale, meterEvent.getMeterScale());

    // Don't trigger event if this item is bound to another sensor type
    if (meterScale != null && MeterScale.getMeterScale(meterScale) != meterEvent.getMeterScale()) {
      logger.debug("Not the right scale {} <> {}", meterScale, meterEvent.getMeterScale());
      return null;
    }

    BigDecimal val = (BigDecimal) event.getValue();

    // If we've set a zero, then anything below this value needs to be considered ZERO
    if (meterZero != null) {
      if (val.doubleValue() <= Double.parseDouble(meterZero)) {
        val = BigDecimal.ZERO;
      }
    }

    return new DecimalType(val);
  }
  /** {@inheritDoc} */
  @Override
  public List<SerialMessage> receiveCommand(
      ZWaveThingChannel channel, ZWaveNode node, Command command) {
    // Is this channel a reset button - if not, just return
    if ("true".equalsIgnoreCase(channel.getArguments().get("reset")) == false) {
      return null;
    }

    // It's not an ON command from a button switch, do not reset
    if (command != OnOffType.ON) {
      return null;
    }

    ZWaveMeterCommandClass commandClass =
        (ZWaveMeterCommandClass)
            node.resolveCommandClass(ZWaveCommandClass.CommandClass.METER, channel.getEndpoint());

    // Get the reset message - will return null if not supported
    SerialMessage serialMessage =
        node.encapsulate(commandClass.getResetMessage(), commandClass, channel.getEndpoint());

    if (serialMessage == null) {
      return null;
    }

    // Queue reset message
    List<SerialMessage> messages = new ArrayList<SerialMessage>();
    messages.add(serialMessage);

    // And poll the device
    for (SerialMessage serialGetMessage : commandClass.getDynamicValues(true)) {
      messages.add(node.encapsulate(serialGetMessage, commandClass, channel.getEndpoint()));
    }
    return messages;
  }
  /** {@inheritDoc} */
  @Override
  public List<SerialMessage> executeRefresh(ZWaveThingChannel channel, ZWaveNode node) {
    ZWaveMeterCommandClass commandClass =
        (ZWaveMeterCommandClass)
            node.resolveCommandClass(ZWaveCommandClass.CommandClass.METER, channel.getEndpoint());
    if (commandClass == null) {
      return null;
    }

    logger.debug(
        "NODE {}: Generating poll message for {}, endpoint {}",
        node.getNodeId(),
        commandClass.getCommandClass().getLabel(),
        channel.getEndpoint());
    SerialMessage serialMessage;

    // Don't refresh channels that are the reset button
    if ("true".equalsIgnoreCase(channel.getArguments().get("reset"))) {
      return null;
    }

    String meterScale = channel.getArguments().get("type");
    logger.debug(
        "NODE {}: Generating poll message for {}, endpoint {}",
        node.getNodeId(),
        commandClass.getCommandClass().getLabel(),
        channel.getEndpoint());

    if (meterScale != null) {
      serialMessage =
          node.encapsulate(
              commandClass.getMessage(MeterScale.getMeterScale(meterScale)),
              commandClass,
              channel.getEndpoint());
    } else {
      serialMessage =
          node.encapsulate(commandClass.getValueMessage(), commandClass, channel.getEndpoint());
    }

    List<SerialMessage> response = new ArrayList<SerialMessage>(1);
    response.add(serialMessage);
    return response;
  }