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