]> git.basschouten.com Git - openhab-addons.git/blob
5ad1b4568206725ae8300c38a89cfda55a3e18c6
[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, "deviceTemp", 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, "sensorTemp", ITEMT_TEMP))
217                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_HUM, "sensorHumidity", ITEMT_PERCENT))
218                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_LUX, "sensorLux", ITEMT_LUX))
219                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_ILLUM, "sensorIllumination", ITEMT_STRING))
220                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_VOLTAGE, "sensorADC", ITEMT_VOLT))
221                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_STATE, "sensorContact", ITEMT_CONTACT))
222                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_SSTATE, "sensorState", ITEMT_STRING))
223                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_TILT, "sensorTilt", ITEMT_ANGLE))
224                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_MOTION, "sensorMotion", ITEMT_SWITCH))
225                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_MOTION_TS, "motionTimestamp", ITEMT_DATETIME))
226                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_MOTION_ACT, "motionActive", ITEMT_SWITCH))
227                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_VIBRATION, "sensorVibration", ITEMT_SWITCH))
228                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_FLOOD, "sensorFlood", ITEMT_SWITCH))
229                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_SMOKE, "sensorSmoke", ITEMT_SWITCH))
230                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_MUTE, "sensorMute", ITEMT_SWITCH))
231                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_PPM, "sensorPPM", ITEMT_NUMBER))
232                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_VALVE, "sensorValve", ITEMT_STRING))
233                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_ALARM_STATE, "alarmState", ITEMT_STRING))
234                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_ERROR, "sensorError", ITEMT_STRING))
235                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_LAST_UPDATE, "lastUpdate", ITEMT_DATETIME))
236                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_SLEEPTIME, "sensorSleepTime", ITEMT_NUMBER))
237                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSE_KEY, "senseKey", ITEMT_STRING)) // Sense
238
239                 // Button/ix3
240                 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_INPUT, "inputState", ITEMT_SWITCH))
241                 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_STATUS_EVENTTYPE, "lastEvent", ITEMT_STRING))
242                 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_STATUS_EVENTCOUNT, "eventCount", ITEMT_NUMBER))
243                 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_BUTTON_TRIGGER, "system:button", ITEMT_STRING))
244                 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_LAST_UPDATE, "lastUpdate", ITEMT_DATETIME))
245
246                 // Addon with external sensors
247                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENSOR_TEMP1, "sensorExtTemp", ITEMT_TEMP))
248                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENSOR_TEMP2, "sensorExtTemp", ITEMT_TEMP))
249                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENSOR_TEMP3, "sensorExtTemp", ITEMT_TEMP))
250                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENSOR_HUMIDITY, "sensorExtHum", ITEMT_PERCENT))
251                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENSOR_VOLTAGE, "sensorExtVolt", ITEMT_VOLT))
252                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENSOR_INPUT1, "sensorContact", ITEMT_CONTACT))
253                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENSOR_DIGITALINPUT, "sensorExtDigitalInput",
254                         ITEMT_SWITCH))
255                 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENSOR_ANALOGINPUT, "sensorExtAnalogInput",
256                         ITEMT_PERCENT))
257
258                 // Battery
259                 .add(new ShellyChannel(m, CHGR_BAT, CHANNEL_SENSOR_BAT_LEVEL, "system:battery-level", ITEMT_PERCENT))
260                 .add(new ShellyChannel(m, CHGR_BAT, CHANNEL_SENSOR_BAT_LOW, "system:low-battery", ITEMT_SWITCH))
261
262                 // TRV
263                 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_POSITION, "sensorPosition", ITEMT_DIMMER))
264                 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_MODE, "controlMode", ITEMT_STRING))
265                 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_PROFILE, "controlProfile", ITEMT_STRING))
266                 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_SETTEMP, "targetTemp", ITEMT_TEMP))
267                 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_BCONTROL, "boostControl", ITEMT_SWITCH))
268                 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_BTIMER, "boostTimer", ITEMT_TIME))
269                 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_SCHEDULE, "controlSchedule", ITEMT_SWITCH));
270     }
271
272     public static @Nullable ShellyChannel getDefinition(String channelName) throws IllegalArgumentException {
273         String group = substringBefore(channelName, "#");
274         String channel = substringAfter(channelName, "#");
275
276         if (group.startsWith(CHANNEL_GROUP_METER)) {
277             group = CHANNEL_GROUP_METER; // map meter1..n to meter
278         } else if (group.startsWith(CHANNEL_GROUP_RELAY_CONTROL)) {
279             group = CHANNEL_GROUP_RELAY_CONTROL; // map meter1..n to meter
280         } else if (group.startsWith(CHANNEL_GROUP_LIGHT_CHANNEL)) {
281             group = CHANNEL_GROUP_LIGHT_CHANNEL;
282         } else if (group.startsWith(CHANNEL_GROUP_STATUS)) {
283             group = CHANNEL_GROUP_STATUS; // map status1..n to meter
284         }
285
286         if (channel.startsWith(CHANNEL_INPUT)) {
287             channel = CHANNEL_INPUT;
288         } else if (channel.startsWith(CHANNEL_BUTTON_TRIGGER)) {
289             channel = CHANNEL_BUTTON_TRIGGER;
290         } else if (channel.startsWith(CHANNEL_STATUS_EVENTTYPE)) {
291             channel = CHANNEL_STATUS_EVENTTYPE;
292         } else if (channel.startsWith(CHANNEL_STATUS_EVENTCOUNT)) {
293             channel = CHANNEL_STATUS_EVENTCOUNT;
294         }
295
296         String channelId = group + "#" + channel;
297         return CHANNEL_DEFINITIONS.get(channelId);
298     }
299
300     /**
301      * Auto-create relay channels depending on relay type/mode
302      *
303      * @return {@code ArrayList<Channel>} of channels to be added to the thing
304      */
305     public static Map<String, Channel> createDeviceChannels(final Thing thing, final ShellyDeviceProfile profile,
306             final ShellySettingsStatus status) {
307         Map<String, Channel> add = new LinkedHashMap<>();
308
309         addChannel(thing, add, profile.settings.name != null, CHGR_DEVST, CHANNEL_DEVST_NAME);
310         addChannel(thing, add, !profile.gateway.isEmpty() || profile.isBlu, CHGR_DEVST, CHANNEL_DEVST_GATEWAY);
311
312         if (!profile.isSensor && !profile.isIX
313                 && ((status.temperature != null && getDouble(status.temperature) != SHELLY_API_INVTEMP)
314                         || (status.tmp != null && getDouble(status.tmp.tC) != SHELLY_API_INVTEMP))) {
315             // Only some devices report the internal device temp
316             boolean hasTemp = !profile.isLight
317                     && (status.temperature != null || (status.tmp != null && !profile.isSensor));
318             if (hasTemp && profile.isGen2 && (profile.numMeters > 0 && !profile.hasRelays)) // Shely Plus PM Mini
319             {
320                 hasTemp = false;
321             }
322             addChannel(thing, add, hasTemp, CHGR_DEVST, CHANNEL_DEVST_ITEMP);
323         }
324         addChannel(thing, add, profile.settings.sleepTime != null, CHGR_SENSOR, CHANNEL_SENSOR_SLEEPTIME);
325
326         // If device has more than 1 meter the channel accumulatedWatts receives the accumulated value
327         boolean accuChannel = profile.hasRelays && profile.numMeters > 1 && !profile.isRoller && !profile.isRGBW2;
328         addChannel(thing, add, accuChannel, CHGR_DEVST, CHANNEL_DEVST_ACCUWATTS);
329         addChannel(thing, add, accuChannel, CHGR_DEVST, CHANNEL_DEVST_ACCUTOTAL);
330         addChannel(thing, add, accuChannel && (status.emeters != null), CHGR_DEVST, CHANNEL_DEVST_ACCURETURNED);
331         addChannel(thing, add, profile.is3EM, CHGR_DEVST, CHANNEL_DEVST_RESETTOTAL); // 3EM
332         addChannel(thing, add, status.voltage != null || profile.settings.supplyVoltage != null, CHGR_DEVST,
333                 CHANNEL_DEVST_VOLTAGE);
334         addChannel(thing, add,
335                 profile.status.uptime != null && (!profile.hasBattery || profile.isMotion || profile.isTRV), CHGR_DEVST,
336                 CHANNEL_DEVST_UPTIME);
337         addChannel(thing, add, true, CHGR_DEVST, CHANNEL_DEVST_UPDATE);
338         addChannel(thing, add, true, CHGR_DEVST, CHANNEL_DEVST_HEARTBEAT);
339         addChannel(thing, add, profile.settings.ledPowerDisable != null, CHGR_DEVST, CHANNEL_LED_POWER_DISABLE);
340         addChannel(thing, add, profile.settings.ledStatusDisable != null, CHGR_DEVST, CHANNEL_LED_STATUS_DISABLE); // WiFi
341         addChannel(thing, add, profile.settings.calibrated != null, CHGR_DEVST, CHANNEL_DEVST_CALIBRATED);
342
343         return add;
344     }
345
346     /**
347      * Auto-create relay channels depending on relay type/mode
348      *
349      * @return {@code ArrayList<Channel>} of channels to be added to the thing
350      */
351     public static Map<String, Channel> createRelayChannels(final Thing thing, final ShellyDeviceProfile profile,
352             final ShellySettingsRelay rstatus, int idx) {
353         Map<String, Channel> add = new LinkedHashMap<>();
354         String group = profile.getControlGroup(idx);
355
356         if (profile.settings.relays != null) {
357             ShellySettingsRelay rs = profile.settings.relays.get(idx);
358             addChannel(thing, add, rs.ison != null, group, CHANNEL_OUTPUT);
359             addChannel(thing, add, rs.name != null, group, CHANNEL_OUTPUT_NAME);
360
361             boolean timer = rs.hasTimer != null || rstatus.hasTimer != null; // Dimmer 1/2 have
362             addChannel(thing, add, timer, group, CHANNEL_TIMER_ACTIVE);
363             addChannel(thing, add, rs.autoOn != null, group, CHANNEL_TIMER_AUTOON);
364             addChannel(thing, add, rs.autoOff != null, group, CHANNEL_TIMER_AUTOOFF);
365         }
366
367         // Shelly 1/1PM and Plus 1/1PM Addon
368         boolean addon = profile.settings.extSwitch != null && profile.settings.extSwitch.input0 != null
369                 && idx == getInteger(profile.settings.extSwitch.input0.relayNum);
370         if (addon) {
371             addChannel(thing, add, addon, CHGR_SENSOR,
372                     CHANNEL_ESENSOR_INPUT + (profile.settings.extSwitch.input0.relayNum + 1));
373         }
374         if (profile.status.extTemperature != null) {
375             addChannel(thing, add, profile.status.extTemperature.sensor1 != null, CHGR_SENSOR, CHANNEL_ESENSOR_TEMP1);
376             addChannel(thing, add, profile.status.extTemperature.sensor2 != null, CHGR_SENSOR, CHANNEL_ESENSOR_TEMP2);
377             addChannel(thing, add, profile.status.extTemperature.sensor3 != null, CHGR_SENSOR, CHANNEL_ESENSOR_TEMP3);
378             addChannel(thing, add, profile.status.extTemperature.sensor4 != null, CHGR_SENSOR, CHANNEL_ESENSOR_TEMP4);
379             addChannel(thing, add, profile.status.extTemperature.sensor5 != null, CHGR_SENSOR, CHANNEL_ESENSOR_TEMP5);
380         }
381         addChannel(thing, add, profile.status.extHumidity != null && profile.status.extHumidity.sensor1 != null,
382                 CHGR_SENSOR, CHANNEL_ESENSOR_HUMIDITY);
383         addChannel(thing, add, profile.status.extVoltage != null, CHGR_SENSOR, CHANNEL_ESENSOR_VOLTAGE);
384         addChannel(thing, add, profile.status.extDigitalInput != null, CHGR_SENSOR, CHANNEL_ESENSOR_DIGITALINPUT);
385         addChannel(thing, add, profile.status.extAnalogInput != null, CHGR_SENSOR, CHANNEL_ESENSOR_ANALOGINPUT);
386
387         return add;
388     }
389
390     public static Map<String, Channel> createDimmerChannels(final Thing thing, final ShellyDeviceProfile profile,
391             final ShellySettingsStatus dstatus, int idx) {
392         Map<String, Channel> add = new LinkedHashMap<>();
393         String group = profile.getControlGroup(idx);
394
395         // Shelly Dimmer has an additional brightness channel
396         addChannel(thing, add, profile.isDimmer, group, CHANNEL_BRIGHTNESS);
397
398         if (profile.settings.dimmers != null) {
399             ShellySettingsDimmer ds = profile.settings.dimmers.get(idx);
400             addChannel(thing, add, ds.name != null, group, CHANNEL_OUTPUT_NAME);
401             addChannel(thing, add, ds.autoOn != null, group, CHANNEL_TIMER_AUTOON);
402             addChannel(thing, add, ds.autoOff != null, group, CHANNEL_TIMER_AUTOOFF);
403             ShellyShortLightStatus dss = dstatus.dimmers.get(idx);
404             addChannel(thing, add, dss != null && dss.hasTimer != null, group, CHANNEL_TIMER_ACTIVE);
405         }
406         return add;
407     }
408
409     public static Map<String, Channel> createLightChannels(final Thing thing, final ShellyDeviceProfile profile,
410             final ShellyStatusLightChannel status, int idx) {
411         Map<String, Channel> add = new LinkedHashMap<>();
412         String group = profile.getControlGroup(idx);
413
414         if (profile.settings.lights != null) {
415             ShellySettingsRgbwLight light = profile.settings.lights.get(idx);
416             String whiteGroup = profile.isRGBW2 ? group : CHANNEL_GROUP_WHITE_CONTROL;
417             // Create power channel in color mode and brightness channel in white mode
418             addChannel(thing, add, profile.inColor, group, CHANNEL_LIGHT_POWER);
419             addChannel(thing, add, light.autoOn != null, group, CHANNEL_TIMER_AUTOON);
420             addChannel(thing, add, light.autoOff != null, group, CHANNEL_TIMER_AUTOOFF);
421             addChannel(thing, add, status.hasTimer != null, group, CHANNEL_TIMER_ACTIVE);
422             addChannel(thing, add, light.brightness != null, whiteGroup, CHANNEL_BRIGHTNESS);
423             addChannel(thing, add, light.temp != null, whiteGroup, CHANNEL_COLOR_TEMP);
424         }
425
426         return add;
427     }
428
429     public static Map<String, Channel> createInputChannels(final Thing thing, final ShellyDeviceProfile profile,
430             final ShellySettingsStatus status) {
431         Map<String, Channel> add = new LinkedHashMap<>();
432         if (status.inputs != null) {
433             // Create channels per input. For devices with more than 1 input (Dimmer, 1L) multiple channel sets are
434             // created by adding the index to the channel name
435             for (int i = 0; i < profile.numInputs; i++) {
436                 String group = profile.getInputGroup(i);
437                 String suffix = profile.getInputSuffix(i); // multi ? String.valueOf(i + 1) : "";
438                 addChannel(thing, add, !profile.isButton, group, CHANNEL_INPUT + suffix);
439                 addChannel(thing, add, true, group,
440                         (!profile.isRoller ? CHANNEL_BUTTON_TRIGGER + suffix : CHANNEL_EVENT_TRIGGER));
441                 if (profile.inButtonMode(i)) {
442                     ShellyInputState input = status.inputs.get(i);
443                     addChannel(thing, add, input.event != null, group, CHANNEL_STATUS_EVENTTYPE + suffix);
444                     addChannel(thing, add, input.eventCount != null, group, CHANNEL_STATUS_EVENTCOUNT + suffix);
445                 }
446             }
447         } else if (status.input != null) {
448             // old RGBW2 firmware
449             String group = profile.getInputGroup(0);
450             addChannel(thing, add, true, group, CHANNEL_INPUT);
451             addChannel(thing, add, true, group, CHANNEL_BUTTON_TRIGGER);
452         }
453         return add;
454     }
455
456     public static Map<String, Channel> createRollerChannels(Thing thing, final ShellyRollerStatus roller) {
457         Map<String, Channel> add = new LinkedHashMap<>();
458         addChannel(thing, add, true, CHGR_ROLLER, CHANNEL_ROL_CONTROL_CONTROL);
459         addChannel(thing, add, true, CHGR_ROLLER, CHANNEL_ROL_CONTROL_STATE);
460         addChannel(thing, add, true, CHGR_ROLLER, CHANNEL_EVENT_TRIGGER);
461         addChannel(thing, add, roller.currentPos != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_POS);
462         addChannel(thing, add, roller.stopReason != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_STOPR);
463         addChannel(thing, add, roller.safetySwitch != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_SAFETY);
464
465         ShellyThingInterface handler = (ShellyThingInterface) thing.getHandler();
466         if (handler != null) {
467             ShellySettingsGlobal settings = handler.getProfile().settings;
468             if (getBool(settings.favoritesEnabled) && settings.favorites != null) {
469                 addChannel(thing, add, roller.currentPos != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_FAV);
470             }
471         }
472         return add;
473     }
474
475     public static Map<String, Channel> createMeterChannels(Thing thing, final ShellySettingsMeter meter, String group) {
476         Map<String, Channel> newChannels = new LinkedHashMap<>();
477         addChannel(thing, newChannels, meter.power != null, group, CHANNEL_METER_CURRENTWATTS);
478         addChannel(thing, newChannels, meter.total != null, group, CHANNEL_METER_TOTALKWH);
479         addChannel(thing, newChannels, meter.counters != null && meter.counters[0] != null, group,
480                 CHANNEL_METER_LASTMIN1);
481         addChannel(thing, newChannels, meter.timestamp != null, group, CHANNEL_LAST_UPDATE);
482         return newChannels;
483     }
484
485     public static Map<String, Channel> createEMeterChannels(final Thing thing, final ShellyDeviceProfile profile,
486             final ShellySettingsEMeter emeter, String group) {
487         Map<String, Channel> newChannels = new LinkedHashMap<>();
488         addChannel(thing, newChannels, emeter.power != null, group, CHANNEL_METER_CURRENTWATTS);
489         addChannel(thing, newChannels, emeter.total != null, group, CHANNEL_METER_TOTALKWH);
490         addChannel(thing, newChannels, emeter.totalReturned != null, group, CHANNEL_EMETER_TOTALRET);
491         addChannel(thing, newChannels, emeter.reactive != null, group, CHANNEL_EMETER_REACTWATTS);
492         addChannel(thing, newChannels, emeter.voltage != null, group, CHANNEL_EMETER_VOLTAGE);
493         addChannel(thing, newChannels, emeter.current != null, group, CHANNEL_EMETER_CURRENT);
494         addChannel(thing, newChannels, emeter.pf != null, group, CHANNEL_EMETER_PFACTOR); // EM has no PF. but power
495         addChannel(thing, newChannels, true, group, CHANNEL_LAST_UPDATE);
496         ShellyThingInterface handler = (ShellyThingInterface) thing.getHandler();
497         if (handler != null) {
498             addChannel(thing, newChannels, handler.getProfile().isEM50, group, CHANNEL_DEVST_RESETTOTAL); // 3EM
499         }
500         return newChannels;
501     }
502
503     public static Map<String, Channel> createEMNCurrentChannels(final Thing thing, ShellyEMNCurrentSettings settings,
504             ShellyEMNCurrentStatus status) {
505         String group = CHANNEL_GROUP_NMETER;
506         Map<String, Channel> newChannels = new LinkedHashMap<>();
507         addChannel(thing, newChannels, status.current != null, group, CHANNEL_NMETER_CURRENT);
508         addChannel(thing, newChannels, status.ixsum != null, group, CHANNEL_NMETER_IXSUM);
509         addChannel(thing, newChannels, status.mismatch != null, group, CHANNEL_NMETER_MISMATCH);
510         addChannel(thing, newChannels, settings.mismatchThreshold != null, group, CHANNEL_NMETER_MTRESHHOLD);
511         return newChannels;
512     }
513
514     public static Map<String, Channel> createSensorChannels(final Thing thing, final ShellyDeviceProfile profile,
515             final ShellyStatusSensor sdata) {
516         Map<String, Channel> newChannels = new LinkedHashMap<>();
517
518         // Sensor data
519         addChannel(thing, newChannels, sdata.tmp != null || sdata.thermostats != null, CHANNEL_GROUP_SENSOR,
520                 CHANNEL_SENSOR_TEMP);
521         addChannel(thing, newChannels, sdata.hum != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_HUM);
522         addChannel(thing, newChannels, sdata.lux != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_LUX);
523         addChannel(thing, newChannels, sdata.lux != null && sdata.lux.illumination != null, CHANNEL_GROUP_SENSOR,
524                 CHANNEL_SENSOR_ILLUM);
525         addChannel(thing, newChannels, sdata.flood != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_FLOOD);
526         addChannel(thing, newChannels, sdata.smoke != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_SMOKE);
527         addChannel(thing, newChannels, sdata.mute != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_MUTE);
528         addChannel(thing, newChannels, profile.settings.externalPower != null || sdata.charger != null, CHGR_DEVST,
529                 CHANNEL_DEVST_CHARGER);
530         addChannel(thing, newChannels, sdata.motion != null || (sdata.sensor != null && sdata.sensor.motion != null),
531                 CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_MOTION);
532         if (sdata.sensor != null) { // DW, Sense or Motion
533             addChannel(thing, newChannels, sdata.sensor.state != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_STATE); // DW/DW2
534             addChannel(thing, newChannels, sdata.sensor.motionActive != null, CHANNEL_GROUP_SENSOR, // Motion
535                     CHANNEL_SENSOR_MOTION_ACT);
536             addChannel(thing, newChannels, sdata.sensor.motionTimestamp != null, CHANNEL_GROUP_SENSOR, // Motion
537                     CHANNEL_SENSOR_MOTION_TS);
538             addChannel(thing, newChannels, sdata.sensor.vibration != null, CHANNEL_GROUP_SENSOR,
539                     CHANNEL_SENSOR_VIBRATION);
540         }
541         // Create tilt for DW/DW2, for BLU DW create channel even tilt is currently not reported
542         if (sdata.accel != null || (profile.isBlu && profile.isDW && sdata.lux != null)) {
543             addChannel(thing, newChannels, sdata.accel.tilt != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_TILT);
544         }
545
546         // Gas
547         if (sdata.gasSensor != null) {
548             addChannel(thing, newChannels, sdata.gasSensor.selfTestState != null, CHGR_DEVST, CHANNEL_DEVST_SELFTTEST);
549             addChannel(thing, newChannels, sdata.gasSensor.sensorState != null, CHANNEL_GROUP_SENSOR,
550                     CHANNEL_SENSOR_SSTATE);
551             addChannel(thing, newChannels, sdata.concentration != null && sdata.concentration.ppm != null,
552                     CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_PPM);
553             addChannel(thing, newChannels, sdata.valves != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_VALVE);
554             addChannel(thing, newChannels, sdata.gasSensor.sensorState != null, CHANNEL_GROUP_SENSOR,
555                     CHANNEL_SENSOR_ALARM_STATE);
556         }
557
558         // Sense
559         addChannel(thing, newChannels, profile.isSense, CHANNEL_GROUP_SENSOR, CHANNEL_SENSE_KEY);
560
561         // UNI
562         addChannel(thing, newChannels, sdata.adcs != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_VOLTAGE);
563
564         // TRV
565         if (profile.isTRV) {
566             addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_SETTEMP);
567             addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_BCONTROL);
568             addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_BTIMER);
569             addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_POSITION);
570             addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_MODE);
571             addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_PROFILE);
572             addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_SCHEDULE);
573             addChannel(thing, newChannels, true, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_STATE);
574         }
575
576         // Battery
577         if (sdata.bat != null) {
578             addChannel(thing, newChannels, sdata.bat.value != null, CHANNEL_GROUP_BATTERY, CHANNEL_SENSOR_BAT_LEVEL);
579             addChannel(thing, newChannels, sdata.bat.value != null, CHANNEL_GROUP_BATTERY, CHANNEL_SENSOR_BAT_LOW);
580         }
581
582         addChannel(thing, newChannels, sdata.sensorError != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_ERROR);
583         addChannel(thing, newChannels, sdata.actReasons != null, CHGR_DEVST, CHANNEL_DEVST_WAKEUP);
584         addChannel(thing, newChannels, true, profile.isButton ? CHANNEL_GROUP_STATUS : CHANNEL_GROUP_SENSOR,
585                 CHANNEL_LAST_UPDATE);
586         return newChannels;
587     }
588
589     public ChannelTypeUID getChannelTypeUID(String channelId) {
590         ShellyChannel channelDef = getDefinition(channelId);
591         if (channelDef != null) {
592             return new ChannelTypeUID(BINDING_ID, channelDef.typeId);
593         }
594         throw new IllegalArgumentException("Invalid channelId:" + channelId);
595     }
596
597     private static void addChannel(Thing thing, Map<String, Channel> newChannels, boolean supported, String group,
598             String channelName) throws IllegalArgumentException {
599         if (supported) {
600             String channelId = group + "#" + channelName;
601             ChannelUID channelUID = new ChannelUID(thing.getUID(), channelId);
602             ShellyChannel channelDef = getDefinition(channelId);
603             if (channelDef != null) {
604                 ChannelTypeUID channelTypeUID = channelDef.typeId.contains("system:")
605                         ? new ChannelTypeUID(channelDef.typeId)
606                         : new ChannelTypeUID(BINDING_ID, channelDef.typeId);
607                 ChannelBuilder builder;
608                 if ("system:button".equalsIgnoreCase(channelDef.typeId)) {
609                     builder = ChannelBuilder.create(channelUID, null).withKind(ChannelKind.TRIGGER);
610                 } else {
611                     builder = ChannelBuilder.create(channelUID, channelDef.itemType);
612                 }
613                 if (!channelDef.label.isEmpty()) {
614                     char grseq = lastChar(group);
615                     char chseq = lastChar(channelName);
616                     char sequence = isDigit(chseq) ? chseq : grseq;
617                     String label = !isDigit(sequence) ? channelDef.label : channelDef.label + " " + sequence;
618                     builder.withLabel(label);
619                 }
620                 if (!channelDef.description.isEmpty()) {
621                     builder.withDescription(channelDef.description);
622                 }
623                 newChannels.put(channelId, builder.withType(channelTypeUID).build());
624             }
625         }
626     }
627
628     public List<StateOption> getStateOptions(ChannelTypeUID uid) {
629         List<StateOption> options = new ArrayList<>();
630         for (OptionEntry oe : stateOptions) {
631             if (oe.uid.equals(uid)) {
632                 options.add(new StateOption(oe.key, oe.value));
633             }
634         }
635         return options;
636     }
637
638     public void addStateOption(String channelId, String key, String value) {
639         ChannelTypeUID uid = getChannelTypeUID(channelId);
640         stateOptions.addIfAbsent(new OptionEntry(uid, key, value));
641     }
642
643     public void clearStateOptions(String channelId) {
644         ChannelTypeUID uid = getChannelTypeUID(channelId);
645         for (OptionEntry oe : stateOptions) {
646             if (oe.uid.equals(uid)) {
647                 stateOptions.remove(oe);
648             }
649         }
650     }
651
652     public class ShellyChannel {
653         private final ShellyTranslationProvider messages;
654         public String group = "";
655         public String groupLabel = "";
656         public String groupDescription = "";
657
658         public String channel = "";
659         public String label = "";
660         public String description = "";
661         public String itemType = "";
662         public String typeId = "";
663         public String category = "";
664         public Set<String> tags = new HashSet<>();
665         public @Nullable Unit<?> unit;
666         public Optional<Integer> min = Optional.empty();
667         public Optional<Integer> max = Optional.empty();
668         public Optional<Integer> step = Optional.empty();
669         public Optional<String> pattern = Optional.empty();
670
671         public ShellyChannel(ShellyTranslationProvider messages, String group, String channel, String typeId,
672                 String itemType, String... category) {
673             this.messages = messages;
674             this.group = group;
675             this.channel = channel;
676             this.itemType = itemType;
677             this.typeId = typeId;
678
679             groupLabel = getText(PREFIX_GROUP + group + ".label");
680             if (groupLabel.startsWith(PREFIX_GROUP)) {
681                 groupLabel = "";
682             }
683             groupDescription = getText(PREFIX_GROUP + group + ".description");
684             if (groupDescription.startsWith(PREFIX_GROUP)) {
685                 groupDescription = "";
686             }
687             label = getText(PREFIX_CHANNEL + typeId.replace(':', '.') + ".label");
688             if (label.startsWith(PREFIX_CHANNEL)) {
689                 label = "";
690             }
691             description = getText(PREFIX_CHANNEL + typeId + ".description");
692             if (description.startsWith(PREFIX_CHANNEL)) {
693                 description = ""; // no resource found
694             }
695         }
696
697         public String getChanneId() {
698             return group + "#" + channel;
699         }
700
701         public String getGroupLabel() {
702             return getGroupAttribute("group");
703         }
704
705         public String getGroupDescription() {
706             return getGroupAttribute("group");
707         }
708
709         public String getLabel() {
710             return getChannelAttribute("label");
711         }
712
713         public String getDescription() {
714             return getChannelAttribute("description");
715         }
716
717         public boolean getAdvanced() {
718             String attr = getChannelAttribute("advanced");
719             return attr.isEmpty() ? false : Boolean.valueOf(attr);
720         }
721
722         public boolean getReadyOnly() {
723             String attr = getChannelAttribute("readOnly");
724             return attr.isEmpty() ? false : Boolean.valueOf(attr);
725         }
726
727         public String getCategory() {
728             return getChannelAttribute("category");
729         }
730
731         public String getMin() {
732             return getChannelAttribute("min");
733         }
734
735         public String getMax() {
736             return getChannelAttribute("max");
737         }
738
739         public String getStep() {
740             return getChannelAttribute("step");
741         }
742
743         public String getPattern() {
744             return getChannelAttribute("pattern");
745         }
746
747         public String getGroupAttribute(String attribute) {
748             String key = PREFIX_GROUP + group + "." + attribute;
749             String value = messages.getText(key);
750             return !value.equals(key) ? value : "";
751         }
752
753         public String getChannelAttribute(String attribute) {
754             String key = PREFIX_CHANNEL + channel + "." + attribute;
755             String value = messages.getText(key);
756             return !value.equals(key) ? value : "";
757         }
758
759         private String getText(String key) {
760             return messages.get(key);
761         }
762     }
763
764     public static class ChannelMap {
765         private final Map<String, ShellyChannel> map = new HashMap<>();
766
767         private ChannelMap add(ShellyChannel def) {
768             map.put(def.getChanneId(), def);
769             return this;
770         }
771
772         public ShellyChannel get(String channelName) throws IllegalArgumentException {
773             ShellyChannel def = null;
774             if (channelName.contains("#")) {
775                 def = map.get(channelName);
776                 if (def != null) {
777                     return def;
778                 }
779             }
780             for (HashMap.Entry<String, ShellyChannel> entry : map.entrySet()) {
781                 if (entry.getValue().channel.contains("#" + channelName)) {
782                     def = entry.getValue();
783                     break;
784                 }
785             }
786
787             if (def == null) {
788                 throw new IllegalArgumentException("Channel definition for " + channelName + " not found!");
789             }
790
791             return def;
792         }
793     }
794 }