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.http.internal.converter;
15 import java.math.BigDecimal;
16 import java.util.function.Consumer;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.http.internal.config.HttpChannelConfig;
21 import org.openhab.binding.http.internal.transform.ValueTransformation;
22 import org.openhab.core.library.types.PercentType;
23 import org.openhab.core.types.Command;
24 import org.openhab.core.types.State;
25 import org.openhab.core.types.UnDefType;
28 * The {@link DimmerItemConverter} implements {@link org.openhab.core.library.items.DimmerItem} conversions
30 * @author Jan N. Klug - Initial contribution
34 public class DimmerItemConverter extends AbstractTransformingItemConverter {
35 private static final BigDecimal HUNDRED = BigDecimal.valueOf(100);
37 private State state = UnDefType.UNDEF;
39 public DimmerItemConverter(Consumer<State> updateState, Consumer<Command> postCommand,
40 @Nullable Consumer<String> sendHttpValue, ValueTransformation stateTransformations,
41 ValueTransformation commandTransformations, HttpChannelConfig channelConfig) {
42 super(updateState, postCommand, sendHttpValue, stateTransformations, commandTransformations, channelConfig);
43 this.channelConfig = channelConfig;
47 protected @Nullable Command toCommand(String value) {
52 public String toString(Command command) {
53 String string = channelConfig.commandToFixedValue(command);
58 if (command instanceof PercentType) {
59 return ((PercentType) command).toString();
62 throw new IllegalArgumentException("Command type '" + command.toString() + "' not supported");
66 public State toState(String string) {
67 State newState = UnDefType.UNDEF;
69 if (string.equals(channelConfig.onValue)) {
70 newState = PercentType.HUNDRED;
71 } else if (string.equals(channelConfig.offValue)) {
72 newState = PercentType.ZERO;
73 } else if (string.equals(channelConfig.increaseValue) && state instanceof PercentType) {
74 BigDecimal newBrightness = ((PercentType) state).toBigDecimal().add(channelConfig.step);
75 if (HUNDRED.compareTo(newBrightness) < 0) {
76 newBrightness = HUNDRED;
78 newState = new PercentType(newBrightness);
79 } else if (string.equals(channelConfig.decreaseValue) && state instanceof PercentType) {
80 BigDecimal newBrightness = ((PercentType) state).toBigDecimal().subtract(channelConfig.step);
81 if (BigDecimal.ZERO.compareTo(newBrightness) > 0) {
82 newBrightness = BigDecimal.ZERO;
84 newState = new PercentType(newBrightness);
87 BigDecimal value = new BigDecimal(string);
88 if (value.compareTo(PercentType.HUNDRED.toBigDecimal()) > 0) {
89 value = PercentType.HUNDRED.toBigDecimal();
91 if (value.compareTo(PercentType.ZERO.toBigDecimal()) < 0) {
92 value = PercentType.ZERO.toBigDecimal();
94 newState = new PercentType(value);
95 } catch (IllegalArgumentException e) {