]> git.basschouten.com Git - openhab-addons.git/blob
a189c297662addf2c3e77a0da69afc1a9bfe36bd
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mqtt.generic.values;
14
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;
20
21 /**
22  * A factory t
23  *
24  * @author David Graeff - Initial contribution
25  */
26 @NonNullByDefault
27 public class ValueFactory {
28
29     /**
30      * Creates a new channel state value.
31      *
32      * @param config The channel configuration
33      * @param channelTypeID The channel type, for instance TEXT_CHANNEL.
34      */
35     public static Value createValueState(ChannelConfig config, String channelTypeID) throws IllegalArgumentException {
36         Value value;
37         switch (channelTypeID) {
38             case MqttBindingConstants.STRING:
39                 value = config.allowedStates.isBlank() ? new TextValue()
40                         : new TextValue(config.allowedStates.split(","));
41                 break;
42             case MqttBindingConstants.DATETIME:
43                 value = new DateTimeValue();
44                 break;
45             case MqttBindingConstants.IMAGE:
46                 value = new ImageValue();
47                 break;
48             case MqttBindingConstants.LOCATION:
49                 value = new LocationValue();
50                 break;
51             case MqttBindingConstants.NUMBER:
52                 value = new NumberValue(config.min, config.max, config.step, UnitUtils.parseUnit(config.unit));
53                 break;
54             case MqttBindingConstants.DIMMER:
55                 value = new PercentageValue(config.min, config.max, config.step, config.on, config.off);
56                 break;
57             case MqttBindingConstants.COLOR_HSB:
58                 value = new ColorValue(ColorMode.HSB, config.on, config.off, config.onBrightness);
59                 break;
60             case MqttBindingConstants.COLOR_RGB:
61                 value = new ColorValue(ColorMode.RGB, config.on, config.off, config.onBrightness);
62                 break;
63             case MqttBindingConstants.COLOR:
64                 ColorMode colorMode;
65                 try {
66                     colorMode = ColorMode.valueOf(config.colorMode);
67                 } catch (IllegalArgumentException exception) {
68                     throw new IllegalArgumentException("Invalid color mode: " + config.colorMode, exception);
69                 }
70                 value = new ColorValue(colorMode, config.on, config.off, config.onBrightness);
71                 break;
72             case MqttBindingConstants.SWITCH:
73                 value = new OnOffValue(config.on, config.off);
74                 break;
75             case MqttBindingConstants.CONTACT:
76                 value = new OpenCloseValue(config.on, config.off);
77                 break;
78             case MqttBindingConstants.ROLLERSHUTTER:
79                 value = new RollershutterValue(config.on, config.off, config.stop);
80                 break;
81             case MqttBindingConstants.TRIGGER:
82                 config.trigger = true;
83                 value = new TextValue();
84                 break;
85             default:
86                 throw new IllegalArgumentException("ChannelTypeUID not recognised: " + channelTypeID);
87         }
88         return value;
89     }
90 }