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