2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.enocean.internal.eep;
15 import javax.measure.quantity.Energy;
17 import org.openhab.binding.enocean.internal.config.EnOceanChannelTotalusageConfig;
18 import org.openhab.core.config.core.Configuration;
19 import org.openhab.core.library.types.QuantityType;
20 import org.openhab.core.library.unit.Units;
21 import org.openhab.core.types.State;
22 import org.openhab.core.types.UnDefType;
26 * @author Dominik Vorreiter - initial contribution
29 public abstract class EEPHelper {
30 public static State validateTotalUsage(State value, State currentState, Configuration config) {
31 EnOceanChannelTotalusageConfig c = config.as(EnOceanChannelTotalusageConfig.class);
33 if (c.validateValue && (value instanceof QuantityType) && (currentState instanceof QuantityType)) {
34 @SuppressWarnings("unchecked")
35 QuantityType<Energy> newValue = value.as(QuantityType.class);
37 if (newValue != null) {
38 newValue = newValue.toUnit(Units.KILOWATT_HOUR);
41 @SuppressWarnings("unchecked")
42 QuantityType<Energy> oldValue = currentState.as(QuantityType.class);
44 if (oldValue != null) {
45 oldValue = oldValue.toUnit(Units.KILOWATT_HOUR);
48 if ((newValue != null) && (oldValue != null)) {
49 if (newValue.compareTo(oldValue) < 0) {
50 if ((oldValue.subtract(newValue).doubleValue() < 1.0)) {
51 return UnDefType.UNDEF;
54 if (newValue.subtract(oldValue).doubleValue() > 10.0) {
55 return UnDefType.UNDEF;