2 * Copyright (c) 2010-2020 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 org.apache.commons.lang.StringUtils;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.openhab.binding.mqtt.generic.ChannelConfig;
18 import org.openhab.binding.mqtt.generic.internal.MqttBindingConstants;
19 import org.openhab.binding.mqtt.generic.mapping.ColorMode;
24 * @author David Graeff - Initial contribution
27 public class ValueFactory {
29 * Creates a new channel state value.
31 * @param config The channel configuration
32 * @param channelTypeID The channel type, for instance TEXT_CHANNEL.
34 public static Value createValueState(ChannelConfig config, String channelTypeID) throws IllegalArgumentException {
36 switch (channelTypeID) {
37 case MqttBindingConstants.STRING:
38 value = StringUtils.isBlank(config.allowedStates) ? new TextValue()
39 : new TextValue(config.allowedStates.split(","));
41 case MqttBindingConstants.DATETIME:
42 value = new DateTimeValue();
44 case MqttBindingConstants.IMAGE:
45 value = new ImageValue();
47 case MqttBindingConstants.LOCATION:
48 value = new LocationValue();
50 case MqttBindingConstants.NUMBER:
51 value = new NumberValue(config.min, config.max, config.step, config.unit);
53 case MqttBindingConstants.DIMMER:
54 value = new PercentageValue(config.min, config.max, config.step, config.on, config.off);
56 case MqttBindingConstants.COLOR_HSB:
57 value = new ColorValue(ColorMode.HSB, config.on, config.off, config.onBrightness);
59 case MqttBindingConstants.COLOR_RGB:
60 value = new ColorValue(ColorMode.RGB, config.on, config.off, config.onBrightness);
62 case MqttBindingConstants.COLOR:
63 value = new ColorValue(ColorMode.valueOf(config.colorMode), config.on, config.off, config.onBrightness);
65 case MqttBindingConstants.SWITCH:
66 value = new OnOffValue(config.on, config.off);
68 case MqttBindingConstants.CONTACT:
69 value = new OpenCloseValue(config.on, config.off);
71 case MqttBindingConstants.ROLLERSHUTTER:
72 value = new RollershutterValue(config.on, config.off, config.stop);
74 case MqttBindingConstants.TRIGGER:
75 config.trigger = true;
76 value = new TextValue();
79 throw new IllegalArgumentException("ChannelTypeUID not recognised: " + channelTypeID);