]> git.basschouten.com Git - openhab-addons.git/blob
b177e918e95e7dbdd65254eb26bc7e724ea7c24e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.shelly.internal.provider;
14
15 import static org.openhab.binding.shelly.internal.ShellyBindingConstants.*;
16 import static org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.SHELLY_API_INVTEMP;
17 import static org.openhab.binding.shelly.internal.util.ShellyUtils.*;
18
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.LinkedHashMap;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Optional;
26 import java.util.Set;
27 import java.util.concurrent.CopyOnWriteArrayList;
28
29 import javax.measure.Unit;
30
31 import org.eclipse.jdt.annotation.NonNullByDefault;
32 import org.eclipse.jdt.annotation.Nullable;
33 import org.openhab.binding.shelly.internal.api.ShellyDeviceProfile;
34 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyInputState;
35 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyRollerStatus;
36 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsDimmer;
37 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsEMeter;
38 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsGlobal;
39 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsMeter;
40 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsRelay;
41 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsRgbwLight;
42 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsStatus;
43 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyShortLightStatus;
44 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyStatusLightChannel;
45 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyStatusSensor;
46 import org.openhab.binding.shelly.internal.handler.ShellyThingInterface;
47 import org.openhab.core.thing.Channel;
48 import org.openhab.core.thing.ChannelUID;
49 import org.openhab.core.thing.Thing;
50 import org.openhab.core.thing.binding.builder.ChannelBuilder;
51 import org.openhab.core.thing.type.ChannelKind;
52 import org.openhab.core.thing.type.ChannelTypeUID;
53 import org.openhab.core.types.StateOption;
54 import org.osgi.service.component.annotations.Activate;
55 import org.osgi.service.component.annotations.Component;
56 import org.osgi.service.component.annotations.Reference;
57
58 /**
59  * The {@link ShellyCHANNEL_DEFINITIONSDTO} defines channel information for dynamically created channels. Those will be
60  * added on the first thing status update
61  *
62  * @author Markus Michels - Initial contribution
63  */
64 @NonNullByDefault
65 @Component(service = ShellyChannelDefinitions.class)
66 public class ShellyChannelDefinitions {
67
68     public static final String ITEMT_STRING = "String";
69     public static final String ITEMT_NUMBER = "Number";
70     public static final String ITEMT_SWITCH = "Switch";
71     public static final String ITEMT_CONTACT = "Contact";
72     public static final String ITEMT_ROLLER = "Rollershutter";
73     public static final String ITEMT_DIMMER = "Dimmer";
74     public static final String ITEMT_LOCATION = "Location";
75     public static final String ITEMT_DATETIME = "DateTime";
76     public static final String ITEMT_TEMP = "Number:Temperature";
77     public static final String ITEMT_LUX = "Number:Illuminance";
78     public static final String ITEMT_POWER = "Number:Power";
79     public static final String ITEMT_ENERGY = "Number:Energy";
80     public static final String ITEMT_VOLT = "Number:ElectricPotential";
81     public static final String ITEMT_AMP = "Number:ElectricCurrent";
82     public static final String ITEMT_ANGLE = "Number:Angle";
83     public static final String ITEMT_DISTANCE = "Number:Length";
84     public static final String ITEMT_SPEED = "Number:Speed";
85     public static final String ITEMT_VOLUME = "Number:Volume";
86     public static final String ITEMT_TIME = "Number:Time";
87     public static final String ITEMT_PERCENT = "Number:Dimensionless";
88
89     // shortcuts to avoid line breaks (make code more readable)
90     private static final String CHGR_DEVST = CHANNEL_GROUP_DEV_STATUS;
91     private static final String CHGR_RELAY = CHANNEL_GROUP_RELAY_CONTROL;
92     private static final String CHGR_ROLLER = CHANNEL_GROUP_ROL_CONTROL;
93     private static final String CHGR_LIGHT = CHANNEL_GROUP_LIGHT_CONTROL;
94     private static final String CHGR_LIGHTCH = CHANNEL_GROUP_LIGHT_CHANNEL;
95     private static final String CHGR_STATUS = CHANNEL_GROUP_STATUS;
96     private static final String CHGR_METER = CHANNEL_GROUP_METER;
97     private static final String CHGR_SENSOR = CHANNEL_GROUP_SENSOR;
98     private static final String CHGR_CONTROL = CHANNEL_GROUP_CONTROL;
99     private static final String CHGR_BAT = CHANNEL_GROUP_BATTERY;
100
101     public static final String PREFIX_GROUP = "group-type." + BINDING_ID + ".";
102     public static final String PREFIX_CHANNEL = "channel-type." + BINDING_ID + ".";
103
104     public class OptionEntry {
105         public ChannelTypeUID uid;
106         public String key;
107         public String value;
108
109         public OptionEntry(ChannelTypeUID uid, String key, String value) {
110             this.uid = uid;
111             this.key = key;
112             this.value = value;
113         }
114     }
115
116     private final CopyOnWriteArrayList<OptionEntry> stateOptions = new CopyOnWriteArrayList<>();
117
118     private static final ChannelMap CHANNEL_DEFINITIONS = new ChannelMap();
119
120     @Activate
121     public ShellyChannelDefinitions(@Reference ShellyTranslationProvider translationProvider) {
122         ShellyTranslationProvider m = translationProvider;
123
124         // Device
125         CHANNEL_DEFINITIONS
126                 // Device
127                 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_NAME, "deviceName", ITEMT_STRING))
128                 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_GATEWAY, "gatewayDevice", ITEMT_STRING))
129                 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_ITEMP, "deviceTemp", ITEMT_TEMP))
130                 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_WAKEUP, "sensorWakeup", ITEMT_STRING))
131                 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_ACCUWATTS, "meterAccuWatts", ITEMT_POWER))
132                 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_ACCUTOTAL, "meterAccuTotal", ITEMT_ENERGY))
133                 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_ACCURETURNED, "meterAccuReturned", ITEMT_ENERGY))
134                 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_RESETTOTAL, "meterResetTotals", ITEMT_SWITCH))
135                 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_VOLTAGE, "supplyVoltage", ITEMT_VOLT))
136                 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_CHARGER, "charger", ITEMT_SWITCH))
137                 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_LED_STATUS_DISABLE, "ledStatusDisable", ITEMT_SWITCH))
138                 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_LED_POWER_DISABLE, "ledPowerDisable", ITEMT_SWITCH))
139                 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_SELFTTEST, "selfTest", ITEMT_STRING))
140                 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_UPTIME, "uptime", ITEMT_NUMBER))
141                 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_HEARTBEAT, "heartBeat", ITEMT_DATETIME))
142                 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_UPDATE, "updateAvailable", ITEMT_SWITCH))
143                 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_CALIBRATED, "calibrated", ITEMT_SWITCH))
144
145                 // Relay
146                 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_OUTPUT_NAME, "outputName", ITEMT_STRING))
147                 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_OUTPUT, "system:power", ITEMT_SWITCH))
148                 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_INPUT, "inputState", ITEMT_SWITCH))
149                 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_BUTTON_TRIGGER, "system:button", ITEMT_STRING))
150                 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_STATUS_EVENTTYPE, "lastEvent", ITEMT_STRING))
151                 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_STATUS_EVENTCOUNT, "eventCount", ITEMT_NUMBER))
152                 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_TIMER_AUTOON, "timerAutoOn", ITEMT_TIME))
153                 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_TIMER_AUTOOFF, "timerAutoOff", ITEMT_TIME))
154                 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_TIMER_ACTIVE, "timerActive", ITEMT_SWITCH))
155
156                 // Dimmer
157                 .add(new ShellyChannel(m, CHANNEL_GROUP_DIMMER_CONTROL, CHANNEL_BRIGHTNESS, "dimmerBrightness",
158                         ITEMT_DIMMER))
159
160                 // Roller
161                 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_CONTROL, "rollerShutter", ITEMT_ROLLER))
162                 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_POS, "rollerPosition", ITEMT_DIMMER))
163                 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_FAV, "rollerFavorite", ITEMT_NUMBER))
164                 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_STATE, "rollerState", ITEMT_STRING))
165                 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_STOPR, "rollerStop", ITEMT_STRING))
166                 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_SAFETY, "rollerSafety", ITEMT_SWITCH))
167                 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_INPUT, "inputState", ITEMT_SWITCH))
168                 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_STATUS_EVENTTYPE, "lastEvent", ITEMT_STRING))
169                 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_STATUS_EVENTCOUNT, "eventCount", ITEMT_NUMBER))
170                 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_EVENT_TRIGGER, "system:button", "system:button"))
171
172                 // Bulb/Duo/Vintage
173                 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_INPUT, "inputState", ITEMT_SWITCH))
174                 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_BUTTON_TRIGGER, "system:button", ITEMT_STRING))
175                 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_STATUS_EVENTTYPE, "lastEvent", ITEMT_STRING))
176                 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_STATUS_EVENTCOUNT, "eventCount", ITEMT_NUMBER))
177                 .add(new ShellyChannel(m, CHANNEL_GROUP_WHITE_CONTROL, CHANNEL_BRIGHTNESS, "whiteBrightness",
178                         ITEMT_DIMMER))
179                 .add(new ShellyChannel(m, CHANNEL_GROUP_WHITE_CONTROL, CHANNEL_COLOR_TEMP, "whiteTemp", ITEMT_DIMMER))
180
181                 // RGBW2-color
182                 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_LIGHT_POWER, "system:power", ITEMT_SWITCH))
183                 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_TIMER_AUTOON, "timerAutoOn", ITEMT_TIME))
184                 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_TIMER_AUTOOFF, "timerAutoOff", ITEMT_TIME))
185                 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_TIMER_ACTIVE, "timerActive", ITEMT_SWITCH))
186                 // RGBW2-white
187                 .add(new ShellyChannel(m, CHGR_LIGHTCH, CHANNEL_BRIGHTNESS, "whiteBrightness", ITEMT_DIMMER))
188                 .add(new ShellyChannel(m, CHGR_LIGHTCH, CHANNEL_TIMER_AUTOON, "timerAutoOn", ITEMT_TIME))
189                 .add(new ShellyChannel(m, CHGR_LIGHTCH, CHANNEL_TIMER_AUTOOFF, "timerAutoOff", ITEMT_TIME))
190                 .add(new ShellyChannel(m, CHGR_LIGHTCH, CHANNEL_TIMER_ACTIVE, "timerActive", ITEMT_SWITCH))
191
192                 // Power Meter
193                 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_METER_CURRENTWATTS, "meterWatts", ITEMT_POWER))
194                 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_METER_TOTALKWH, "meterTotal", ITEMT_ENERGY))
195                 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_METER_LASTMIN1, "lastPower1", ITEMT_POWER))
196                 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_LAST_UPDATE, "lastUpdate", ITEMT_DATETIME))
197
198                 // EMeter
199                 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_TOTALRET, "meterReturned", ITEMT_ENERGY))
200                 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_REACTWATTS, "meterReactive", ITEMT_POWER))
201                 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_VOLTAGE, "meterVoltage", ITEMT_VOLT))
202                 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_CURRENT, "meterCurrent", ITEMT_AMP))
203                 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_PFACTOR, "meterPowerFactor", ITEMT_NUMBER))
204
205                 // Sensors
206                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_TEMP, "sensorTemp", ITEMT_TEMP))
207                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_HUM, "sensorHumidity", ITEMT_PERCENT))
208                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_LUX, "sensorLux", ITEMT_LUX))
209                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_ILLUM, "sensorIllumination", ITEMT_STRING))
210                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_VOLTAGE, "sensorADC", ITEMT_VOLT))
211                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_STATE, "sensorContact", ITEMT_CONTACT))
212                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_OPEN, "sensorOpen", ITEMT_CONTACT))
213                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_SSTATE, "sensorState", ITEMT_STRING))
214                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_TILT, "sensorTilt", ITEMT_ANGLE))
215                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_MOTION, "sensorMotion", ITEMT_SWITCH))
216                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_MOTION_TS, "motionTimestamp", ITEMT_DATETIME))
217                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_MOTION_ACT, "motionActive", ITEMT_SWITCH))
218                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_VIBRATION, "sensorVibration", ITEMT_SWITCH))
219                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_FLOOD, "sensorFlood", ITEMT_SWITCH))
220                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_SMOKE, "sensorSmoke", ITEMT_SWITCH))
221                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_MUTE, "sensorMute", ITEMT_SWITCH))
222                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_PPM, "sensorPPM", ITEMT_NUMBER))
223                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_VALVE, "sensorValve", ITEMT_STRING))
224                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_ALARM_STATE, "alarmState", ITEMT_STRING))
225                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_ERROR, "sensorError", ITEMT_STRING))
226                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_LAST_UPDATE, "lastUpdate", ITEMT_DATETIME))
227                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_SLEEPTIME, "sensorSleepTime", ITEMT_NUMBER))
228                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSE_KEY, "senseKey", ITEMT_STRING)) // Sense
229
230                 // Button/ix3
231                 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_INPUT, "inputState", ITEMT_SWITCH))
232                 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_STATUS_EVENTTYPE, "lastEvent", ITEMT_STRING))
233                 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_STATUS_EVENTCOUNT, "eventCount", ITEMT_NUMBER))
234                 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_BUTTON_TRIGGER, "system:button", ITEMT_STRING))
235                 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_LAST_UPDATE, "lastUpdate", ITEMT_DATETIME))
236
237                 // Addon with external sensors
238                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENSOR_TEMP1, "sensorExtTemp", ITEMT_TEMP))
239                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENSOR_TEMP2, "sensorExtTemp", ITEMT_TEMP))
240                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENSOR_TEMP3, "sensorExtTemp", ITEMT_TEMP))
241                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENSOR_HUMIDITY, "sensorExtHum", ITEMT_PERCENT))
242                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENSOR_VOLTAGE, "sensorExtVolt", ITEMT_VOLT))
243                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENSOR_INPUT1, "sensorContact", ITEMT_CONTACT))
244                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENSOR_DIGITALINPUT, "sensorExtDigitalInput",
245                         ITEMT_SWITCH))
246                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENSOR_ANALOGINPUT, "sensorExtAnalogInput",
247                         ITEMT_PERCENT))
248
249                 // Battery
250                 .add(new ShellyChannel(m, CHGR_BAT, CHANNEL_SENSOR_BAT_LEVEL, "system:battery-level", ITEMT_PERCENT))
251                 .add(new ShellyChannel(m, CHGR_BAT, CHANNEL_SENSOR_BAT_LOW, "system:low-battery", ITEMT_SWITCH))
252
253                 // TRV
254                 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_POSITION, "sensorPosition", ITEMT_DIMMER))
255                 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_MODE, "controlMode", ITEMT_STRING))
256                 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_PROFILE, "controlProfile", ITEMT_STRING))
257                 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_SETTEMP, "targetTemp", ITEMT_TEMP))
258                 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_BCONTROL, "boostControl", ITEMT_SWITCH))
259                 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_BTIMER, "boostTimer", ITEMT_TIME))
260                 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_SCHEDULE, "controlSchedule", ITEMT_SWITCH));
261     }
262
263     public static @Nullable ShellyChannel getDefinition(String channelName) throws IllegalArgumentException {
264         String group = substringBefore(channelName, "#");
265         String channel = substringAfter(channelName, "#");
266
267         if (group.contains(CHANNEL_GROUP_METER)) {
268             group = CHANNEL_GROUP_METER; // map meter1..n to meter
269         } else if (group.contains(CHANNEL_GROUP_RELAY_CONTROL)) {
270             group = CHANNEL_GROUP_RELAY_CONTROL; // map meter1..n to meter
271         } else if (group.contains(CHANNEL_GROUP_LIGHT_CHANNEL)) {
272             group = CHANNEL_GROUP_LIGHT_CHANNEL;
273         } else if (group.contains(CHANNEL_GROUP_STATUS)) {
274             group = CHANNEL_GROUP_STATUS; // map status1..n to meter
275         }
276
277         if (channel.startsWith(CHANNEL_INPUT)) {
278             channel = CHANNEL_INPUT;
279         } else if (channel.startsWith(CHANNEL_BUTTON_TRIGGER)) {
280             channel = CHANNEL_BUTTON_TRIGGER;
281         } else if (channel.startsWith(CHANNEL_STATUS_EVENTTYPE)) {
282             channel = CHANNEL_STATUS_EVENTTYPE;
283         } else if (channel.startsWith(CHANNEL_STATUS_EVENTCOUNT)) {
284             channel = CHANNEL_STATUS_EVENTCOUNT;
285         }
286
287         String channelId = group + "#" + channel;
288         return CHANNEL_DEFINITIONS.get(channelId);
289     }
290
291     /**
292      * Auto-create relay channels depending on relay type/mode
293      *
294      * @return ArrayList<Channel> of channels to be added to the thing
295      */
296     public static Map<String, Channel> createDeviceChannels(final Thing thing, final ShellyDeviceProfile profile,
297             final ShellySettingsStatus status) {
298         Map<String, Channel> add = new LinkedHashMap<>();
299
300         addChannel(thing, add, profile.settings.name != null, CHGR_DEVST, CHANNEL_DEVST_NAME);
301         addChannel(thing, add, !profile.gateway.isEmpty() || profile.isBlu, CHGR_DEVST, CHANNEL_DEVST_GATEWAY);
302
303         if (!profile.isSensor && !profile.isIX
304                 && ((status.temperature != null && getDouble(status.temperature) != SHELLY_API_INVTEMP)
305                         || (status.tmp != null && getDouble(status.tmp.tC) != SHELLY_API_INVTEMP))) {
306             // Only some devices report the internal device temp
307             addChannel(thing, add,
308                     !profile.isLight && (status.temperature != null || (status.tmp != null && !profile.isSensor)),
309                     CHGR_DEVST, CHANNEL_DEVST_ITEMP);
310         }
311         addChannel(thing, add, profile.settings.sleepTime != null, CHGR_SENSOR, CHANNEL_SENSOR_SLEEPTIME);
312
313         // If device has more than 1 meter the channel accumulatedWatts receives the accumulated value
314         boolean accuChannel = profile.numMeters > 1 && !profile.isRoller && !profile.isRGBW2;
315         addChannel(thing, add, accuChannel, CHGR_DEVST, CHANNEL_DEVST_ACCUWATTS);
316         addChannel(thing, add, accuChannel, CHGR_DEVST, CHANNEL_DEVST_ACCUTOTAL);
317         addChannel(thing, add, accuChannel && (status.emeters != null), CHGR_DEVST, CHANNEL_DEVST_ACCURETURNED);
318         addChannel(thing, add, profile.is3EM || profile.isEM50, CHGR_DEVST, CHANNEL_DEVST_RESETTOTAL); // 3EM
319         addChannel(thing, add, status.voltage != null || profile.settings.supplyVoltage != null, CHGR_DEVST,
320                 CHANNEL_DEVST_VOLTAGE);
321         addChannel(thing, add,
322                 profile.status.uptime != null && (!profile.hasBattery || profile.isMotion || profile.isTRV), CHGR_DEVST,
323                 CHANNEL_DEVST_UPTIME);
324         addChannel(thing, add, true, CHGR_DEVST, CHANNEL_DEVST_UPDATE);
325         addChannel(thing, add, true, CHGR_DEVST, CHANNEL_DEVST_HEARTBEAT);
326         addChannel(thing, add, profile.settings.ledPowerDisable != null, CHGR_DEVST, CHANNEL_LED_POWER_DISABLE);
327         addChannel(thing, add, profile.settings.ledStatusDisable != null, CHGR_DEVST, CHANNEL_LED_STATUS_DISABLE); // WiFi
328         addChannel(thing, add, profile.settings.calibrated != null, CHGR_DEVST, CHANNEL_DEVST_CALIBRATED);
329
330         return add;
331     }
332
333     /**
334      * Auto-create relay channels depending on relay type/mode
335      *
336      * @return ArrayList<Channel> of channels to be added to the thing
337      */
338     public static Map<String, Channel> createRelayChannels(final Thing thing, final ShellyDeviceProfile profile,
339             final ShellySettingsRelay rstatus, int idx) {
340         Map<String, Channel> add = new LinkedHashMap<>();
341         String group = profile.getControlGroup(idx);
342
343         if (profile.settings.relays != null) {
344             ShellySettingsRelay rs = profile.settings.relays.get(idx);
345             addChannel(thing, add, rs.ison != null, group, CHANNEL_OUTPUT);
346             addChannel(thing, add, rs.name != null, group, CHANNEL_OUTPUT_NAME);
347
348             boolean timer = rs.hasTimer != null || rstatus.hasTimer != null; // Dimmer 1/2 have
349             addChannel(thing, add, timer, group, CHANNEL_TIMER_ACTIVE);
350             addChannel(thing, add, rs.autoOn != null, group, CHANNEL_TIMER_AUTOON);
351             addChannel(thing, add, rs.autoOff != null, group, CHANNEL_TIMER_AUTOOFF);
352         }
353
354         // Shelly 1/1PM and Plus 1/1PM Addon
355         boolean addon = profile.settings.extSwitch != null && profile.settings.extSwitch.input0 != null
356                 && idx == getInteger(profile.settings.extSwitch.input0.relayNum);
357         if (addon) {
358             addChannel(thing, add, addon, CHGR_SENSOR,
359                     CHANNEL_ESENSOR_INPUT + (profile.settings.extSwitch.input0.relayNum + 1));
360         }
361         if (profile.status.extTemperature != null) {
362             addChannel(thing, add, profile.status.extTemperature.sensor1 != null, CHGR_SENSOR, CHANNEL_ESENSOR_TEMP1);
363             addChannel(thing, add, profile.status.extTemperature.sensor2 != null, CHGR_SENSOR, CHANNEL_ESENSOR_TEMP2);
364             addChannel(thing, add, profile.status.extTemperature.sensor3 != null, CHGR_SENSOR, CHANNEL_ESENSOR_TEMP3);
365             addChannel(thing, add, profile.status.extTemperature.sensor4 != null, CHGR_SENSOR, CHANNEL_ESENSOR_TEMP4);
366             addChannel(thing, add, profile.status.extTemperature.sensor5 != null, CHGR_SENSOR, CHANNEL_ESENSOR_TEMP5);
367         }
368         addChannel(thing, add, profile.status.extHumidity != null && profile.status.extHumidity.sensor1 != null,
369                 CHGR_SENSOR, CHANNEL_ESENSOR_HUMIDITY);
370         addChannel(thing, add, profile.status.extVoltage != null, CHGR_SENSOR, CHANNEL_ESENSOR_VOLTAGE);
371         addChannel(thing, add, profile.status.extDigitalInput != null, CHGR_SENSOR, CHANNEL_ESENSOR_DIGITALINPUT);
372         addChannel(thing, add, profile.status.extAnalogInput != null, CHGR_SENSOR, CHANNEL_ESENSOR_ANALOGINPUT);
373
374         return add;
375     }
376
377     public static Map<String, Channel> createDimmerChannels(final Thing thing, final ShellyDeviceProfile profile,
378             final ShellySettingsStatus dstatus, int idx) {
379         Map<String, Channel> add = new LinkedHashMap<>();
380         String group = profile.getControlGroup(idx);
381
382         // Shelly Dimmer has an additional brightness channel
383         addChannel(thing, add, profile.isDimmer, group, CHANNEL_BRIGHTNESS);
384
385         if (profile.settings.dimmers != null) {
386             ShellySettingsDimmer ds = profile.settings.dimmers.get(idx);
387             addChannel(thing, add, ds.name != null, group, CHANNEL_OUTPUT_NAME);
388             addChannel(thing, add, ds.autoOn != null, group, CHANNEL_TIMER_AUTOON);
389             addChannel(thing, add, ds.autoOff != null, group, CHANNEL_TIMER_AUTOOFF);
390             ShellyShortLightStatus dss = dstatus.dimmers.get(idx);
391             addChannel(thing, add, dss != null && dss.hasTimer != null, group, CHANNEL_TIMER_ACTIVE);
392         }
393         return add;
394     }
395
396     public static Map<String, Channel> createLightChannels(final Thing thing, final ShellyDeviceProfile profile,
397             final ShellyStatusLightChannel status, int idx) {
398         Map<String, Channel> add = new LinkedHashMap<>();
399         String group = profile.getControlGroup(idx);
400
401         if (profile.settings.lights != null) {
402             ShellySettingsRgbwLight light = profile.settings.lights.get(idx);
403             String whiteGroup = profile.isRGBW2 ? group : CHANNEL_GROUP_WHITE_CONTROL;
404             // Create power channel in color mode and brightness channel in white mode
405             addChannel(thing, add, profile.inColor, group, CHANNEL_LIGHT_POWER);
406             addChannel(thing, add, light.autoOn != null, group, CHANNEL_TIMER_AUTOON);
407             addChannel(thing, add, light.autoOff != null, group, CHANNEL_TIMER_AUTOOFF);
408             addChannel(thing, add, status.hasTimer != null, group, CHANNEL_TIMER_ACTIVE);
409             addChannel(thing, add, light.brightness != null, whiteGroup, CHANNEL_BRIGHTNESS);
410             addChannel(thing, add, light.temp != null, whiteGroup, CHANNEL_COLOR_TEMP);
411         }
412
413         return add;
414     }
415
416     public static Map<String, Channel> createInputChannels(final Thing thing, final ShellyDeviceProfile profile,
417             final ShellySettingsStatus status) {
418         Map<String, Channel> add = new LinkedHashMap<>();
419         if (status.inputs != null) {
420             // Create channels per input. For devices with more than 1 input (Dimmer, 1L) multiple channel sets are
421             // created by adding the index to the channel name
422             for (int i = 0; i < profile.numInputs; i++) {
423                 String group = profile.getInputGroup(i);
424                 String suffix = profile.getInputSuffix(i); // multi ? String.valueOf(i + 1) : "";
425                 addChannel(thing, add, !profile.isButton, group, CHANNEL_INPUT + suffix);
426                 addChannel(thing, add, true, group,
427                         (!profile.isRoller ? CHANNEL_BUTTON_TRIGGER + suffix : CHANNEL_EVENT_TRIGGER));
428                 if (profile.inButtonMode(i)) {
429                     ShellyInputState input = status.inputs.get(i);
430                     addChannel(thing, add, input.event != null, group, CHANNEL_STATUS_EVENTTYPE + suffix);
431                     addChannel(thing, add, input.eventCount != null, group, CHANNEL_STATUS_EVENTCOUNT + suffix);
432                 }
433             }
434         } else if (status.input != null) {
435             // old RGBW2 firmware
436             String group = profile.getInputGroup(0);
437             addChannel(thing, add, true, group, CHANNEL_INPUT);
438             addChannel(thing, add, true, group, CHANNEL_BUTTON_TRIGGER);
439         }
440         return add;
441     }
442
443     public static Map<String, Channel> createRollerChannels(Thing thing, final ShellyRollerStatus roller) {
444         Map<String, Channel> add = new LinkedHashMap<>();
445         addChannel(thing, add, true, CHGR_ROLLER, CHANNEL_ROL_CONTROL_CONTROL);
446         addChannel(thing, add, true, CHGR_ROLLER, CHANNEL_ROL_CONTROL_STATE);
447         addChannel(thing, add, true, CHGR_ROLLER, CHANNEL_EVENT_TRIGGER);
448         addChannel(thing, add, roller.currentPos != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_POS);
449         addChannel(thing, add, roller.stopReason != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_STOPR);
450         addChannel(thing, add, roller.safetySwitch != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_SAFETY);
451
452         ShellyThingInterface handler = (ShellyThingInterface) thing.getHandler();
453         if (handler != null) {
454             ShellySettingsGlobal settings = handler.getProfile().settings;
455             if (getBool(settings.favoritesEnabled) && settings.favorites != null) {
456                 addChannel(thing, add, roller.currentPos != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_FAV);
457             }
458         }
459         return add;
460     }
461
462     public static Map<String, Channel> createMeterChannels(Thing thing, final ShellySettingsMeter meter, String group) {
463         Map<String, Channel> newChannels = new LinkedHashMap<>();
464         addChannel(thing, newChannels, meter.power != null, group, CHANNEL_METER_CURRENTWATTS);
465         addChannel(thing, newChannels, meter.total != null, group, CHANNEL_METER_TOTALKWH);
466         addChannel(thing, newChannels, meter.counters != null && meter.counters[0] != null, group,
467                 CHANNEL_METER_LASTMIN1);
468         addChannel(thing, newChannels, meter.timestamp != null, group, CHANNEL_LAST_UPDATE);
469         return newChannels;
470     }
471
472     public static Map<String, Channel> createEMeterChannels(final Thing thing, final ShellyDeviceProfile profile,
473             final ShellySettingsEMeter emeter, String group) {
474         Map<String, Channel> newChannels = new LinkedHashMap<>();
475         addChannel(thing, newChannels, emeter.power != null, group, CHANNEL_METER_CURRENTWATTS);
476         addChannel(thing, newChannels, emeter.total != null, group, CHANNEL_METER_TOTALKWH);
477         addChannel(thing, newChannels, emeter.totalReturned != null, group, CHANNEL_EMETER_TOTALRET);
478         addChannel(thing, newChannels, emeter.reactive != null, group, CHANNEL_EMETER_REACTWATTS);
479         addChannel(thing, newChannels, emeter.voltage != null, group, CHANNEL_EMETER_VOLTAGE);
480         addChannel(thing, newChannels, emeter.current != null, group, CHANNEL_EMETER_CURRENT);
481         addChannel(thing, newChannels, emeter.pf != null, group, CHANNEL_EMETER_PFACTOR); // EM has no PF. but power
482         addChannel(thing, newChannels, true, group, CHANNEL_LAST_UPDATE);
483         return newChannels;
484     }
485
486     public static Map<String, Channel> createSensorChannels(final Thing thing, final ShellyDeviceProfile profile,
487             final ShellyStatusSensor sdata) {
488         Map<String, Channel> newChannels = new LinkedHashMap<>();
489
490         // Sensor data
491         addChannel(thing, newChannels, sdata.tmp != null || sdata.thermostats != null, CHANNEL_GROUP_SENSOR,
492                 CHANNEL_SENSOR_TEMP);
493         addChannel(thing, newChannels, sdata.hum != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_HUM);
494         addChannel(thing, newChannels, sdata.lux != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_LUX);
495         addChannel(thing, newChannels, sdata.lux != null && sdata.lux.illumination != null, CHANNEL_GROUP_SENSOR,
496                 CHANNEL_SENSOR_ILLUM);
497         addChannel(thing, newChannels, sdata.flood != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_FLOOD);
498         addChannel(thing, newChannels, sdata.smoke != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_SMOKE);
499         addChannel(thing, newChannels, sdata.mute != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_MUTE);
500         addChannel(thing, newChannels, profile.settings.externalPower != null || sdata.charger != null, CHGR_DEVST,
501                 CHANNEL_DEVST_CHARGER);
502         addChannel(thing, newChannels, sdata.motion != null || (sdata.sensor != null && sdata.sensor.motion != null),
503                 CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_MOTION);
504         if (sdata.sensor != null) { // DW, Sense or Motion
505             addChannel(thing, newChannels, sdata.sensor.state != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_STATE); // DW/DW2
506             addChannel(thing, newChannels, sdata.sensor.motionActive != null, CHANNEL_GROUP_SENSOR, // Motion
507                     CHANNEL_SENSOR_MOTION_ACT);
508             addChannel(thing, newChannels, sdata.sensor.motionTimestamp != null, CHANNEL_GROUP_SENSOR, // Motion
509                     CHANNEL_SENSOR_MOTION_TS);
510             addChannel(thing, newChannels, sdata.sensor.vibration != null, CHANNEL_GROUP_SENSOR,
511                     CHANNEL_SENSOR_VIBRATION);
512         }
513         // Create tilt for DW/DW2, for BLU DW create channel even tilt is currently not reported
514         if (sdata.accel != null || (profile.isBlu && sdata.lux != null)) {
515             addChannel(thing, newChannels, sdata.accel.tilt != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_TILT);
516         }
517
518         // Gas
519         if (sdata.gasSensor != null) {
520             addChannel(thing, newChannels, sdata.gasSensor.selfTestState != null, CHGR_DEVST, CHANNEL_DEVST_SELFTTEST);
521             addChannel(thing, newChannels, sdata.gasSensor.sensorState != null, CHANNEL_GROUP_SENSOR,
522                     CHANNEL_SENSOR_SSTATE);
523             addChannel(thing, newChannels, sdata.concentration != null && sdata.concentration.ppm != null,
524                     CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_PPM);
525             addChannel(thing, newChannels, sdata.valves != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_VALVE);
526             addChannel(thing, newChannels, sdata.gasSensor.sensorState != null, CHANNEL_GROUP_SENSOR,
527                     CHANNEL_SENSOR_ALARM_STATE);
528         }
529
530         // Sense
531         addChannel(thing, newChannels, profile.isSense, CHANNEL_GROUP_SENSOR, CHANNEL_SENSE_KEY);
532
533         // UNI
534         addChannel(thing, newChannels, sdata.adcs != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_VOLTAGE);
535
536         // TRV
537         if (profile.isTRV) {
538             addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_SETTEMP);
539             addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_BCONTROL);
540             addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_BTIMER);
541             addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_POSITION);
542             addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_MODE);
543             addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_PROFILE);
544             addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_SCHEDULE);
545             addChannel(thing, newChannels, true, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_STATE);
546             addChannel(thing, newChannels, true, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_OPEN);
547         }
548
549         // Battery
550         if (sdata.bat != null) {
551             addChannel(thing, newChannels, sdata.bat.value != null, CHANNEL_GROUP_BATTERY, CHANNEL_SENSOR_BAT_LEVEL);
552             addChannel(thing, newChannels, sdata.bat.value != null, CHANNEL_GROUP_BATTERY, CHANNEL_SENSOR_BAT_LOW);
553         }
554
555         addChannel(thing, newChannels, sdata.sensorError != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_ERROR);
556         addChannel(thing, newChannels, sdata.actReasons != null, CHGR_DEVST, CHANNEL_DEVST_WAKEUP);
557         addChannel(thing, newChannels, true, profile.isButton ? CHANNEL_GROUP_STATUS : CHANNEL_GROUP_SENSOR,
558                 CHANNEL_LAST_UPDATE);
559         return newChannels;
560     }
561
562     public ChannelTypeUID getChannelTypeUID(String channelId) {
563         ShellyChannel channelDef = getDefinition(channelId);
564         if (channelDef != null) {
565             return new ChannelTypeUID(BINDING_ID, channelDef.typeId);
566         }
567         throw new IllegalArgumentException("Invalid channelId:" + channelId);
568     }
569
570     private static void addChannel(Thing thing, Map<String, Channel> newChannels, boolean supported, String group,
571             String channelName) throws IllegalArgumentException {
572         if (supported) {
573             String channelId = group + "#" + channelName;
574             ChannelUID channelUID = new ChannelUID(thing.getUID(), channelId);
575             ShellyChannel channelDef = getDefinition(channelId);
576             if (channelDef != null) {
577                 ChannelTypeUID channelTypeUID = channelDef.typeId.contains("system:")
578                         ? new ChannelTypeUID(channelDef.typeId)
579                         : new ChannelTypeUID(BINDING_ID, channelDef.typeId);
580                 ChannelBuilder builder;
581                 if (channelDef.typeId.equalsIgnoreCase("system:button")) {
582                     builder = ChannelBuilder.create(channelUID, null).withKind(ChannelKind.TRIGGER);
583                 } else {
584                     builder = ChannelBuilder.create(channelUID, channelDef.itemType);
585                 }
586                 if (!channelDef.label.isEmpty()) {
587                     char grseq = lastChar(group);
588                     char chseq = lastChar(channelName);
589                     char sequence = isDigit(chseq) ? chseq : grseq;
590                     String label = !isDigit(sequence) ? channelDef.label : channelDef.label + " " + sequence;
591                     builder.withLabel(label);
592                 }
593                 if (!channelDef.description.isEmpty()) {
594                     builder.withDescription(channelDef.description);
595                 }
596                 newChannels.put(channelId, builder.withType(channelTypeUID).build());
597             }
598         }
599     }
600
601     public List<StateOption> getStateOptions(ChannelTypeUID uid) {
602         List<StateOption> options = new ArrayList<>();
603         for (OptionEntry oe : stateOptions) {
604             if (oe.uid.equals(uid)) {
605                 options.add(new StateOption(oe.key, oe.value));
606             }
607         }
608         return options;
609     }
610
611     public void addStateOption(String channelId, String key, String value) {
612         ChannelTypeUID uid = getChannelTypeUID(channelId);
613         stateOptions.addIfAbsent(new OptionEntry(uid, key, value));
614     }
615
616     public void clearStateOptions(String channelId) {
617         ChannelTypeUID uid = getChannelTypeUID(channelId);
618         for (OptionEntry oe : stateOptions) {
619             if (oe.uid.equals(uid)) {
620                 stateOptions.remove(oe);
621             }
622         }
623     }
624
625     public class ShellyChannel {
626         private final ShellyTranslationProvider messages;
627         public String group = "";
628         public String groupLabel = "";
629         public String groupDescription = "";
630
631         public String channel = "";
632         public String label = "";
633         public String description = "";
634         public String itemType = "";
635         public String typeId = "";
636         public String category = "";
637         public Set<String> tags = new HashSet<>();
638         public @Nullable Unit<?> unit;
639         public Optional<Integer> min = Optional.empty();
640         public Optional<Integer> max = Optional.empty();
641         public Optional<Integer> step = Optional.empty();
642         public Optional<String> pattern = Optional.empty();
643
644         public ShellyChannel(ShellyTranslationProvider messages, String group, String channel, String typeId,
645                 String itemType, String... category) {
646             this.messages = messages;
647             this.group = group;
648             this.channel = channel;
649             this.itemType = itemType;
650             this.typeId = typeId;
651
652             groupLabel = getText(PREFIX_GROUP + group + ".label");
653             if (groupLabel.contains(PREFIX_GROUP)) {
654                 groupLabel = "";
655             }
656             groupDescription = getText(PREFIX_GROUP + group + ".description");
657             if (groupDescription.contains(PREFIX_GROUP)) {
658                 groupDescription = "";
659             }
660             label = getText(PREFIX_CHANNEL + typeId.replace(':', '.') + ".label");
661             if (label.contains(PREFIX_CHANNEL)) {
662                 label = "";
663             }
664             description = getText(PREFIX_CHANNEL + typeId + ".description");
665             if (description.contains(PREFIX_CHANNEL)) {
666                 description = "";
667             }
668         }
669
670         public String getChanneId() {
671             return group + "#" + channel;
672         }
673
674         public String getGroupLabel() {
675             return getGroupAttribute("group");
676         }
677
678         public String getGroupDescription() {
679             return getGroupAttribute("group");
680         }
681
682         public String getLabel() {
683             return getChannelAttribute("label");
684         }
685
686         public String getDescription() {
687             return getChannelAttribute("description");
688         }
689
690         public boolean getAdvanced() {
691             String attr = getChannelAttribute("advanced");
692             return attr.isEmpty() ? false : Boolean.valueOf(attr);
693         }
694
695         public boolean getReadyOnly() {
696             String attr = getChannelAttribute("readOnly");
697             return attr.isEmpty() ? false : Boolean.valueOf(attr);
698         }
699
700         public String getCategory() {
701             return getChannelAttribute("category");
702         }
703
704         public String getMin() {
705             return getChannelAttribute("min");
706         }
707
708         public String getMax() {
709             return getChannelAttribute("max");
710         }
711
712         public String getStep() {
713             return getChannelAttribute("step");
714         }
715
716         public String getPattern() {
717             return getChannelAttribute("pattern");
718         }
719
720         public String getGroupAttribute(String attribute) {
721             String key = PREFIX_GROUP + group + "." + attribute;
722             String value = messages.getText(key);
723             return !value.equals(key) ? value : "";
724         }
725
726         public String getChannelAttribute(String attribute) {
727             String key = PREFIX_CHANNEL + channel + "." + attribute;
728             String value = messages.getText(key);
729             return !value.equals(key) ? value : "";
730         }
731
732         private String getText(String key) {
733             return messages.get(key);
734         }
735     }
736
737     public static class ChannelMap {
738         private final Map<String, ShellyChannel> map = new HashMap<>();
739
740         private ChannelMap add(ShellyChannel def) {
741             map.put(def.getChanneId(), def);
742             return this;
743         }
744
745         public ShellyChannel get(String channelName) throws IllegalArgumentException {
746             ShellyChannel def = null;
747             if (channelName.contains("#")) {
748                 def = map.get(channelName);
749                 if (def != null) {
750                     return def;
751                 }
752             }
753             for (HashMap.Entry<String, ShellyChannel> entry : map.entrySet()) {
754                 if (entry.getValue().channel.contains("#" + channelName)) {
755                     def = entry.getValue();
756                     break;
757                 }
758             }
759
760             if (def == null) {
761                 throw new IllegalArgumentException("Channel definition for " + channelName + " not found!");
762             }
763
764             return def;
765         }
766     }
767 }