2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.shelly.internal.provider;
15 import static org.openhab.binding.shelly.internal.ShellyBindingConstants.*;
16 import static org.openhab.binding.shelly.internal.util.ShellyUtils.*;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.LinkedHashMap;
22 import java.util.Optional;
25 import javax.measure.Unit;
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;
54 * The {@link ShellyCHANNEL_DEFINITIONSDTO} defines channel information for dynamically created channels. Those will be
55 * added on the first thing status update
57 * @author Markus Michels - Initial contribution
60 @Component(service = ShellyChannelDefinitions.class)
61 public class ShellyChannelDefinitions {
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";
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;
94 public static final String PREFIX_GROUP = "group-type." + BINDING_ID + ".";
95 public static final String PREFIX_CHANNEL = "channel-type." + BINDING_ID + ".";
97 private static final ChannelMap CHANNEL_DEFINITIONS = new ChannelMap();
100 public ShellyChannelDefinitions(@Reference ShellyTranslationProvider translationProvider) {
101 ShellyTranslationProvider m = translationProvider;
103 // Device: Internal Temp
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))
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))
132 .add(new ShellyChannel(m, CHANNEL_GROUP_DIMMER_CONTROL, CHANNEL_BRIGHTNESS, "dimmerBrightness",
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"))
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))
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))
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))
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_VIBRATION, "sensorVibration", ITEMT_SWITCH))
179 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_TILT, "sensorTilt", ITEMT_ANGLE))
180 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_MOTION, "sensorMotion", ITEMT_SWITCH))
181 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_MOTION_TS, "motionTimestamp", ITEMT_DATETIME))
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_LAST_UPDATE, "lastUpdate", ITEMT_DATETIME))
191 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_INPUT, "inputState", ITEMT_SWITCH))
192 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_STATUS_EVENTTYPE, "lastEvent", ITEMT_STRING))
193 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_STATUS_EVENTCOUNT, "eventCount", ITEMT_NUMBER))
194 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_BUTTON_TRIGGER, "system:button", ITEMT_STRING))
195 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_LAST_UPDATE, "lastUpdate", ITEMT_DATETIME))
197 // Addon with external sensors
198 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENDOR_TEMP1, "sensorExtTemp", ITEMT_TEMP))
199 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENDOR_TEMP2, "sensorExtTemp", ITEMT_TEMP))
200 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENDOR_TEMP3, "sensorExtTemp", ITEMT_TEMP))
201 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENDOR_HUMIDITY, "sensorExtHum", ITEMT_PERCENT))
204 .add(new ShellyChannel(m, CHGR_BAT, CHANNEL_SENSOR_BAT_LEVEL, "system:battery-level", ITEMT_PERCENT))
205 .add(new ShellyChannel(m, CHGR_BAT, CHANNEL_SENSOR_BAT_LOW, "system:low-battery", ITEMT_SWITCH));
208 public static @Nullable ShellyChannel getDefinition(String channelName) throws IllegalArgumentException {
209 String group = substringBefore(channelName, "#");
210 String channel = substringAfter(channelName, "#");
212 if (group.contains(CHANNEL_GROUP_METER)) {
213 group = CHANNEL_GROUP_METER; // map meter1..n to meter
214 } else if (group.contains(CHANNEL_GROUP_RELAY_CONTROL)) {
215 group = CHANNEL_GROUP_RELAY_CONTROL; // map meter1..n to meter
216 } else if (group.contains(CHANNEL_GROUP_LIGHT_CHANNEL)) {
217 group = CHANNEL_GROUP_LIGHT_CHANNEL;
218 } else if (group.contains(CHANNEL_GROUP_STATUS)) {
219 group = CHANNEL_GROUP_STATUS; // map status1..n to meter
222 if (channel.startsWith(CHANNEL_INPUT)) {
223 channel = CHANNEL_INPUT;
224 } else if (channel.startsWith(CHANNEL_BUTTON_TRIGGER)) {
225 channel = CHANNEL_BUTTON_TRIGGER;
226 } else if (channel.startsWith(CHANNEL_STATUS_EVENTTYPE)) {
227 channel = CHANNEL_STATUS_EVENTTYPE;
228 } else if (channel.startsWith(CHANNEL_STATUS_EVENTCOUNT)) {
229 channel = CHANNEL_STATUS_EVENTCOUNT;
232 String channelId = group + "#" + channel;
233 return CHANNEL_DEFINITIONS.get(channelId);
237 * Auto-create relay channels depending on relay type/mode
239 * @return ArrayList<Channel> of channels to be added to the thing
241 public static Map<String, Channel> createDeviceChannels(final Thing thing, final ShellyDeviceProfile profile,
242 final ShellySettingsStatus status) {
243 Map<String, Channel> add = new LinkedHashMap<>();
245 addChannel(thing, add, profile.settings.name != null, CHGR_DEVST, CHANNEL_DEVST_NAME);
247 if (!profile.isSensor) {
248 // Only some devices report the internal device temp
249 addChannel(thing, add, (status.tmp != null) || (status.temperature != null), CHGR_DEVST,
250 CHANNEL_DEVST_ITEMP);
253 // If device has more than 1 meter the channel accumulatedWatts receives the accumulated value
254 boolean accuChannel = (((status.meters != null) && (status.meters.size() > 1) && !profile.isRoller
255 && !profile.isRGBW2) || ((status.emeters != null && status.emeters.size() > 1)));
256 addChannel(thing, add, accuChannel, CHGR_DEVST, CHANNEL_DEVST_ACCUWATTS);
257 addChannel(thing, add, accuChannel, CHGR_DEVST, CHANNEL_DEVST_ACCUTOTAL);
258 addChannel(thing, add, accuChannel && (status.emeters != null), CHGR_DEVST, CHANNEL_DEVST_ACCURETURNED);
259 addChannel(thing, add, true, CHGR_DEVST, CHANNEL_DEVST_UPDATE);
260 addChannel(thing, add, true, CHGR_DEVST, CHANNEL_DEVST_UPTIME);
261 addChannel(thing, add, true, CHGR_DEVST, CHANNEL_DEVST_HEARTBEAT);
263 if (profile.settings.ledPowerDisable != null) {
264 addChannel(thing, add, true, CHGR_DEVST, CHANNEL_LED_POWER_DISABLE);
266 if (profile.settings.ledStatusDisable != null) {
267 addChannel(thing, add, true, CHGR_DEVST, CHANNEL_LED_STATUS_DISABLE); // WiFi status LED
273 * Auto-create relay channels depending on relay type/mode
275 * @return ArrayList<Channel> of channels to be added to the thing
277 public static Map<String, Channel> createRelayChannels(final Thing thing, final ShellyDeviceProfile profile,
278 final ShellyStatusRelay relay, int idx) {
279 Map<String, Channel> add = new LinkedHashMap<>();
280 String group = profile.getControlGroup(idx);
282 ShellySettingsRelay rs = profile.settings.relays.get(idx);
283 addChannel(thing, add, rs.ison != null, group, CHANNEL_OUTPUT);
284 addChannel(thing, add, rs.name != null, group, CHANNEL_OUTPUT_NAME);
285 addChannel(thing, add, rs.autoOn != null, group, CHANNEL_TIMER_AUTOON);
286 addChannel(thing, add, rs.autoOff != null, group, CHANNEL_TIMER_AUTOOFF);
287 addChannel(thing, add, rs.hasTimer != null, group, CHANNEL_TIMER_ACTIVE);
289 // Shelly 1/1PM Addon
290 if (relay.extTemperature != null) {
291 addChannel(thing, add, relay.extTemperature.sensor1 != null, CHGR_SENSOR, CHANNEL_ESENDOR_TEMP1);
292 addChannel(thing, add, relay.extTemperature.sensor2 != null, CHGR_SENSOR, CHANNEL_ESENDOR_TEMP2);
293 addChannel(thing, add, relay.extTemperature.sensor3 != null, CHGR_SENSOR, CHANNEL_ESENDOR_TEMP3);
295 if (relay.extHumidity != null) {
296 addChannel(thing, add, relay.extHumidity.sensor1 != null, CHGR_SENSOR, CHANNEL_ESENDOR_HUMIDITY);
302 public static Map<String, Channel> createDimmerChannels(final Thing thing, final ShellyDeviceProfile profile,
303 final ShellySettingsStatus dstatus, int idx) {
304 Map<String, Channel> add = new LinkedHashMap<>();
305 String group = profile.getControlGroup(idx);
307 // Shelly Dimmer has an additional brightness channel
308 addChannel(thing, add, profile.isDimmer, group, CHANNEL_BRIGHTNESS);
310 ShellySettingsDimmer ds = profile.settings.dimmers.get(idx);
311 addChannel(thing, add, ds.autoOn != null, group, CHANNEL_TIMER_AUTOON);
312 addChannel(thing, add, ds.autoOff != null, group, CHANNEL_TIMER_AUTOOFF);
316 public static Map<String, Channel> createLightChannels(final Thing thing, final ShellyDeviceProfile profile,
317 final ShellyStatusLightChannel status, int idx) {
318 Map<String, Channel> add = new LinkedHashMap<>();
319 String group = profile.getControlGroup(idx);
321 ShellySettingsRgbwLight light = profile.settings.lights.get(idx);
322 // The is no brightness channel in color mode, so we need a power channel
323 addChannel(thing, add, profile.inColor, group, CHANNEL_LIGHT_POWER);
325 addChannel(thing, add, light.autoOn != null, group, CHANNEL_TIMER_AUTOON);
326 addChannel(thing, add, light.autoOff != null, group, CHANNEL_TIMER_AUTOOFF);
327 addChannel(thing, add, status.hasTimer != null, group, CHANNEL_TIMER_ACTIVE);
331 public static Map<String, Channel> createInputChannels(final Thing thing, final ShellyDeviceProfile profile,
332 final ShellySettingsStatus status, String group) {
333 Map<String, Channel> add = new LinkedHashMap<>();
334 if (status.inputs != null) {
335 // Create channels per input. For devices with more than 1 input (Dimmer, 1L) multiple channel sets are
336 // created by adding the index to the channel name
337 boolean multi = ((profile.numRelays == 1) || profile.isDimmer || profile.isRoller)
338 && (profile.numInputs >= 2);
339 for (int i = 0; i < profile.numInputs; i++) {
340 String suffix = multi ? String.valueOf(i + 1) : "";
341 ShellyInputState input = status.inputs.get(i);
342 addChannel(thing, add, true, group, CHANNEL_INPUT + suffix);
343 if (profile.inButtonMode(i)) {
344 addChannel(thing, add, input.event != null, group, CHANNEL_STATUS_EVENTTYPE + suffix);
345 addChannel(thing, add, input.eventCount != null, group, CHANNEL_STATUS_EVENTCOUNT + suffix);
347 addChannel(thing, add, true, group,
348 (!profile.isRoller ? CHANNEL_BUTTON_TRIGGER + suffix : CHANNEL_EVENT_TRIGGER));
350 } else if (status.input != null) {
351 // old RGBW2 firmware
352 addChannel(thing, add, true, group, CHANNEL_INPUT);
353 addChannel(thing, add, true, group, CHANNEL_BUTTON_TRIGGER);
358 public static Map<String, Channel> createRollerChannels(Thing thing, final ShellyControlRoller roller) {
359 Map<String, Channel> add = new LinkedHashMap<>();
360 addChannel(thing, add, true, CHGR_ROLLER, CHANNEL_ROL_CONTROL_CONTROL);
361 addChannel(thing, add, true, CHGR_ROLLER, CHANNEL_ROL_CONTROL_STATE);
362 addChannel(thing, add, true, CHGR_ROLLER, CHANNEL_EVENT_TRIGGER);
363 addChannel(thing, add, roller.currentPos != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_POS);
364 addChannel(thing, add, roller.stopReason != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_STOPR);
365 addChannel(thing, add, roller.safetySwitch != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_SAFETY);
367 ShellyBaseHandler handler = (ShellyBaseHandler) thing.getHandler();
368 if (handler != null) {
369 ShellySettingsGlobal settings = handler.getProfile().settings;
370 if (getBool(settings.favoritesEnabled) && (settings.favorites != null)) {
371 addChannel(thing, add, roller.currentPos != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_FAV);
377 public static Map<String, Channel> createMeterChannels(Thing thing, final ShellySettingsMeter meter, String group) {
378 Map<String, Channel> newChannels = new LinkedHashMap<>();
379 addChannel(thing, newChannels, meter.power != null, group, CHANNEL_METER_CURRENTWATTS);
380 addChannel(thing, newChannels, meter.total != null, group, CHANNEL_METER_TOTALKWH);
381 addChannel(thing, newChannels, (meter.counters != null) && (meter.counters[0] != null), group,
382 CHANNEL_METER_LASTMIN1);
383 addChannel(thing, newChannels, meter.timestamp != null, group, CHANNEL_LAST_UPDATE);
387 public static Map<String, Channel> createEMeterChannels(final Thing thing, final ShellySettingsEMeter emeter,
389 Map<String, Channel> newChannels = new LinkedHashMap<>();
390 addChannel(thing, newChannels, emeter.power != null, group, CHANNEL_METER_CURRENTWATTS);
391 addChannel(thing, newChannels, emeter.total != null, group, CHANNEL_METER_TOTALKWH);
392 addChannel(thing, newChannels, emeter.totalReturned != null, group, CHANNEL_EMETER_TOTALRET);
393 addChannel(thing, newChannels, emeter.reactive != null, group, CHANNEL_EMETER_REACTWATTS);
394 addChannel(thing, newChannels, emeter.voltage != null, group, CHANNEL_EMETER_VOLTAGE);
395 addChannel(thing, newChannels, emeter.current != null, group, CHANNEL_EMETER_CURRENT);
396 addChannel(thing, newChannels, emeter.pf != null, group, CHANNEL_EMETER_PFACTOR);
397 addChannel(thing, newChannels, true, group, CHANNEL_LAST_UPDATE);
401 public static Map<String, Channel> createSensorChannels(final Thing thing, final ShellyDeviceProfile profile,
402 final ShellyStatusSensor sdata) {
403 Map<String, Channel> newChannels = new LinkedHashMap<>();
406 addChannel(thing, newChannels, sdata.tmp != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_TEMP);
407 addChannel(thing, newChannels, sdata.hum != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_HUM);
408 addChannel(thing, newChannels, sdata.lux != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_LUX);
409 addChannel(thing, newChannels, sdata.lux != null && sdata.lux.illumination != null, CHANNEL_GROUP_SENSOR,
410 CHANNEL_SENSOR_ILLUM);
411 addChannel(thing, newChannels, sdata.flood != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_FLOOD);
412 addChannel(thing, newChannels, sdata.smoke != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_FLOOD);
413 addChannel(thing, newChannels, sdata.charger != null, CHGR_DEVST, CHANNEL_DEVST_CHARGER);
414 addChannel(thing, newChannels, sdata.motion != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_MOTION);
415 if (sdata.sensor != null) { // DW2 or Motion
416 addChannel(thing, newChannels, sdata.sensor.state != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_CONTACT); // DW/DW2
417 addChannel(thing, newChannels, sdata.sensor.motionTimestamp != null, CHANNEL_GROUP_SENSOR, // Motion
418 CHANNEL_SENSOR_MOTION_TS);
419 addChannel(thing, newChannels, sdata.sensor.vibration != null, CHANNEL_GROUP_SENSOR,
420 CHANNEL_SENSOR_VIBRATION);
422 if (sdata.accel != null) { // DW2
423 addChannel(thing, newChannels, sdata.accel.vibration != null, CHANNEL_GROUP_SENSOR,
424 CHANNEL_SENSOR_VIBRATION);
425 addChannel(thing, newChannels, sdata.accel.tilt != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_TILT);
429 if (sdata.gasSensor != null) {
430 addChannel(thing, newChannels, sdata.gasSensor.selfTestState != null, CHGR_DEVST, CHANNEL_DEVST_SELFTTEST);
431 addChannel(thing, newChannels, sdata.gasSensor.sensorState != null, CHANNEL_GROUP_SENSOR,
432 CHANNEL_SENSOR_SSTATE);
433 addChannel(thing, newChannels, sdata.concentration != null && sdata.concentration.ppm != null,
434 CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_PPM);
435 addChannel(thing, newChannels, sdata.valves != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_VALVE);
436 addChannel(thing, newChannels, sdata.gasSensor.sensorState != null, CHANNEL_GROUP_SENSOR,
437 CHANNEL_SENSOR_ALARM_STATE);
440 addChannel(thing, newChannels, sdata.adcs != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_VOLTAGE); // UNI
443 if (sdata.bat != null) {
444 addChannel(thing, newChannels, sdata.bat.value != null, CHANNEL_GROUP_BATTERY, CHANNEL_SENSOR_BAT_LEVEL);
445 addChannel(thing, newChannels, sdata.bat.value != null, CHANNEL_GROUP_BATTERY, CHANNEL_SENSOR_BAT_LOW);
448 addChannel(thing, newChannels, sdata.sensorError != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_ERROR);
449 addChannel(thing, newChannels, sdata.actReasons != null, CHGR_DEVST, CHANNEL_DEVST_WAKEUP);
450 addChannel(thing, newChannels, true, profile.isButton ? CHANNEL_GROUP_STATUS : CHANNEL_GROUP_SENSOR,
451 CHANNEL_LAST_UPDATE);
455 private static void addChannel(Thing thing, Map<String, Channel> newChannels, boolean supported, String group,
456 String channelName) throws IllegalArgumentException {
458 String channelId = group + "#" + channelName;
459 ChannelUID channelUID = new ChannelUID(thing.getUID(), channelId);
460 ShellyChannel channelDef = getDefinition(channelId);
461 if (channelDef != null) {
462 ChannelTypeUID channelTypeUID = channelDef.typeId.contains("system:")
463 ? new ChannelTypeUID(channelDef.typeId)
464 : new ChannelTypeUID(BINDING_ID, channelDef.typeId);
466 if (channelDef.typeId.equalsIgnoreCase("system:button")) {
467 channel = ChannelBuilder.create(channelUID, null).withKind(ChannelKind.TRIGGER)
468 .withType(channelTypeUID).build();
470 channel = ChannelBuilder.create(channelUID, channelDef.itemType).withType(channelTypeUID).build();
472 newChannels.put(channelId, channel);
477 public class ShellyChannel {
478 private final ShellyTranslationProvider messages;
479 public String group = "";
480 public String groupLabel = "";
481 public String groupDescription = "";
483 public String channel = "";
484 public String label = "";
485 public String description = "";
486 public String itemType = "";
487 public String typeId = "";
488 public String category = "";
489 public Set<String> tags = new HashSet<>();
490 public @Nullable Unit<?> unit;
491 public Optional<Integer> min = Optional.empty();
492 public Optional<Integer> max = Optional.empty();
493 public Optional<Integer> step = Optional.empty();
494 public Optional<String> pattern = Optional.empty();
496 public ShellyChannel(ShellyTranslationProvider messages, String group, String channel, String typeId,
497 String itemType, String... category) {
498 this.messages = messages;
500 this.channel = channel;
501 this.itemType = itemType;
502 this.typeId = typeId;
504 groupLabel = getText(PREFIX_GROUP + group + ".label");
505 groupDescription = getText(PREFIX_GROUP + group + ".description");
506 label = getText(PREFIX_CHANNEL + channel + ".label");
507 description = getText(PREFIX_CHANNEL + channel + ".description");
510 public String getChanneId() {
511 return group + "#" + channel;
514 public String getGroupLabel() {
515 return getGroupAttribute("group");
518 public String getGroupDescription() {
519 return getGroupAttribute("group");
522 public String getLabel() {
523 return getChannelAttribute("label");
526 public String getDescription() {
527 return getChannelAttribute("description");
530 public boolean getAdvanced() {
531 String attr = getChannelAttribute("advanced");
532 return attr.isEmpty() ? false : Boolean.valueOf(attr);
535 public boolean getReadyOnly() {
536 String attr = getChannelAttribute("advanced");
537 return attr.isEmpty() ? false : Boolean.valueOf(attr);
540 public String getCategory() {
541 return getChannelAttribute("category");
544 public String getMin() {
545 return getChannelAttribute("min");
548 public String getMax() {
549 return getChannelAttribute("max");
552 public String getStep() {
553 return getChannelAttribute("step");
556 public String getPattern() {
557 return getChannelAttribute("pattern");
560 public String getGroupAttribute(String attribute) {
561 String key = PREFIX_GROUP + group + "." + attribute;
562 String value = messages.getText(key);
563 return value != null && !value.equals(key) ? value : "";
566 public String getChannelAttribute(String attribute) {
567 String key = PREFIX_CHANNEL + channel + "." + attribute;
568 String value = messages.getText(key);
569 return value != null && !value.equals(key) ? value : "";
572 private String getText(String key) {
573 String text = messages.get(key);
574 return text != null ? text : "";
578 public static class ChannelMap {
579 private final Map<String, ShellyChannel> map = new HashMap<>();
581 private ChannelMap add(ShellyChannel def) {
582 map.put(def.getChanneId(), def);
586 public ShellyChannel get(String channelName) throws IllegalArgumentException {
587 ShellyChannel def = null;
588 if (channelName.contains("#")) {
589 def = map.get(channelName);
594 for (HashMap.Entry<String, ShellyChannel> entry : map.entrySet()) {
595 if (entry.getValue().channel.contains("#" + channelName)) {
596 def = entry.getValue();
602 throw new IllegalArgumentException("Channel definition for " + channelName + " not found!");