2 * Copyright (c) 2010-2022 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.mqtt.generic.values;
15 import java.math.BigDecimal;
16 import java.util.Locale;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
20 import javax.ws.rs.NotSupportedException;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.mqtt.generic.mapping.ColorMode;
25 import org.openhab.core.library.CoreItemFactory;
26 import org.openhab.core.library.types.HSBType;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.library.types.PercentType;
29 import org.openhab.core.library.types.StringType;
30 import org.openhab.core.types.Command;
31 import org.openhab.core.types.UnDefType;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Implements a color value.
39 * Accepts user updates from a HSBType, OnOffType and StringType.
41 * Accepts MQTT state updates as OnOffType and a
42 * StringType with comma separated HSB ("h,s,b"), RGB ("r,g,b"), CIE xyY ("x,y,Y") and on, off strings.
43 * On, Off strings can be customized.
45 * @author David Graeff - Initial contribution
46 * @author Aitor Iturrioz - Add CIE xyY colors support
49 public class ColorValue extends Value {
50 private final Logger logger = LoggerFactory.getLogger(ColorValue.class);
52 private final ColorMode colorMode;
53 private final String onValue;
54 private final String offValue;
55 private final int onBrightness;
58 * Creates a non initialized color value.
60 * @param colorMode The color mode: HSB, RGB or XYY.
61 * @param onValue The ON value string. This will be compared to MQTT messages.
62 * @param offValue The OFF value string. This will be compared to MQTT messages.
63 * @param onBrightness When receiving a ON command, the brightness percentage is set to this value
65 public ColorValue(ColorMode colorMode, @Nullable String onValue, @Nullable String offValue, int onBrightness) {
66 super(CoreItemFactory.COLOR,
67 Stream.of(OnOffType.class, PercentType.class, StringType.class).collect(Collectors.toList()));
69 if (onBrightness > 100) {
70 throw new IllegalArgumentException("Brightness parameter must be <= 100");
73 this.colorMode = colorMode;
74 this.onValue = onValue == null ? "ON" : onValue;
75 this.offValue = offValue == null ? "OFF" : offValue;
76 this.onBrightness = onBrightness;
80 * Updates the color state.
83 public void update(Command command) throws IllegalArgumentException {
84 HSBType oldvalue = (state == UnDefType.UNDEF) ? new HSBType() : (HSBType) state;
85 if (command instanceof HSBType) {
86 state = (HSBType) command;
87 } else if (command instanceof OnOffType) {
88 OnOffType boolValue = ((OnOffType) command);
89 PercentType minOn = new PercentType(Math.max(oldvalue.getBrightness().intValue(), onBrightness));
90 state = new HSBType(oldvalue.getHue(), oldvalue.getSaturation(),
91 boolValue == OnOffType.ON ? minOn : new PercentType(0));
92 } else if (command instanceof PercentType) {
93 state = new HSBType(oldvalue.getHue(), oldvalue.getSaturation(), (PercentType) command);
95 final String updatedValue = command.toString();
96 if (onValue.equals(updatedValue)) {
97 PercentType minOn = new PercentType(Math.max(oldvalue.getBrightness().intValue(), onBrightness));
98 state = new HSBType(oldvalue.getHue(), oldvalue.getSaturation(), minOn);
99 } else if (offValue.equals(updatedValue)) {
100 state = new HSBType(oldvalue.getHue(), oldvalue.getSaturation(), new PercentType(0));
102 String[] split = updatedValue.split(",");
103 if (split.length != 3) {
104 throw new IllegalArgumentException(updatedValue + " is not a valid string syntax");
106 switch (this.colorMode) {
108 state = new HSBType(updatedValue);
111 state = HSBType.fromRGB(Integer.parseInt(split[0]), Integer.parseInt(split[1]),
112 Integer.parseInt(split[2]));
115 HSBType temp_state = HSBType.fromXY(Float.parseFloat(split[0]), Float.parseFloat(split[1]));
116 state = new HSBType(temp_state.getHue(), temp_state.getSaturation(), new PercentType(split[2]));
119 logger.warn("Non supported color mode");
125 private static BigDecimal factor = new BigDecimal(2.5);
128 * Converts the color state to a string.
130 * @return Returns the color value depending on the color mode: as a HSB/HSV string (hue,saturation,brightness ->
131 * "60,100,100"), as an RGB string (red,green,blue -> "255,255,0") or as a xyY string
132 * ("0.419321,0.505255,100.00").
135 public String getMQTTpublishValue(@Nullable String pattern) {
136 if (state == UnDefType.UNDEF) {
140 String formatPattern = pattern;
141 if (formatPattern == null || "%s".equals(formatPattern)) {
142 if (this.colorMode == ColorMode.XYY) {
143 formatPattern = "%1$f,%2$f,%3$.2f";
145 formatPattern = "%1$d,%2$d,%3$d";
149 HSBType hsb_state = (HSBType) state;
151 switch (this.colorMode) {
153 return String.format(formatPattern, hsb_state.getHue().intValue(), hsb_state.getSaturation().intValue(),
154 hsb_state.getBrightness().intValue());
156 PercentType[] rgb = hsb_state.toRGB();
157 return String.format(formatPattern, rgb[0].toBigDecimal().multiply(factor).intValue(),
158 rgb[1].toBigDecimal().multiply(factor).intValue(),
159 rgb[2].toBigDecimal().multiply(factor).intValue());
161 PercentType[] xyY = hsb_state.toXY();
162 return String.format(Locale.ROOT, formatPattern, xyY[0].floatValue() / 100.0f,
163 xyY[1].floatValue() / 100.0f, hsb_state.getBrightness().floatValue());
165 throw new NotSupportedException(String.format("Non supported color mode: {}", this.colorMode));