2 * Copyright (c) 2010-2024 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.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.mqtt.generic.ChannelConfig;
17 import org.openhab.binding.mqtt.generic.internal.MqttBindingConstants;
18 import org.openhab.binding.mqtt.generic.mapping.ColorMode;
19 import org.openhab.core.types.util.UnitUtils;
24 * @author David Graeff - Initial contribution
27 public class ValueFactory {
30 * Creates a new channel state value.
32 * @param config The channel configuration
33 * @param channelTypeID The channel type, for instance TEXT_CHANNEL.
35 public static Value createValueState(ChannelConfig config, String channelTypeID) throws IllegalArgumentException {
37 switch (channelTypeID) {
38 case MqttBindingConstants.STRING:
39 value = config.allowedStates.isBlank() ? new TextValue()
40 : new TextValue(config.allowedStates.split(","));
42 case MqttBindingConstants.DATETIME:
43 value = new DateTimeValue();
45 case MqttBindingConstants.IMAGE:
46 value = new ImageValue();
48 case MqttBindingConstants.LOCATION:
49 value = new LocationValue();
51 case MqttBindingConstants.NUMBER:
52 value = new NumberValue(config.min, config.max, config.step, UnitUtils.parseUnit(config.unit));
54 case MqttBindingConstants.DIMMER:
55 value = new PercentageValue(config.min, config.max, config.step, config.on, config.off);
57 case MqttBindingConstants.COLOR_HSB:
58 value = new ColorValue(ColorMode.HSB, config.on, config.off, config.onBrightness);
60 case MqttBindingConstants.COLOR_RGB:
61 value = new ColorValue(ColorMode.RGB, config.on, config.off, config.onBrightness);
63 case MqttBindingConstants.COLOR:
66 colorMode = ColorMode.valueOf(config.colorMode);
67 } catch (IllegalArgumentException exception) {
68 throw new IllegalArgumentException("Invalid color mode: " + config.colorMode, exception);
70 value = new ColorValue(colorMode, config.on, config.off, config.onBrightness);
72 case MqttBindingConstants.SWITCH:
73 value = new OnOffValue(config.on, config.off);
75 case MqttBindingConstants.CONTACT:
76 value = new OpenCloseValue(config.on, config.off);
78 case MqttBindingConstants.ROLLERSHUTTER:
79 value = new RollershutterValue(config.on, config.off, config.stop, config.onState, config.offState,
80 config.invert, config.transformExtentsToString);
82 case MqttBindingConstants.TRIGGER:
83 config.trigger = true;
84 value = new TextValue();
87 throw new IllegalArgumentException("ChannelTypeUID not recognised: " + channelTypeID);