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