2 * Copyright (c) 2010-2023 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.api1.Shelly1ApiJsonDTO.SHELLY_API_INVTEMP;
17 import static org.openhab.binding.shelly.internal.util.ShellyUtils.*;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.LinkedHashMap;
23 import java.util.List;
25 import java.util.Optional;
27 import java.util.concurrent.CopyOnWriteArrayList;
29 import javax.measure.Unit;
31 import org.eclipse.jdt.annotation.NonNullByDefault;
32 import org.eclipse.jdt.annotation.Nullable;
33 import org.openhab.binding.shelly.internal.api.ShellyDeviceProfile;
34 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyInputState;
35 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyRollerStatus;
36 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsDimmer;
37 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsEMeter;
38 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsGlobal;
39 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsMeter;
40 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsRelay;
41 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsRgbwLight;
42 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsStatus;
43 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyShortLightStatus;
44 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyStatusLightChannel;
45 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyStatusSensor;
46 import org.openhab.binding.shelly.internal.handler.ShellyThingInterface;
47 import org.openhab.core.thing.Channel;
48 import org.openhab.core.thing.ChannelUID;
49 import org.openhab.core.thing.Thing;
50 import org.openhab.core.thing.binding.builder.ChannelBuilder;
51 import org.openhab.core.thing.type.ChannelKind;
52 import org.openhab.core.thing.type.ChannelTypeUID;
53 import org.openhab.core.types.StateOption;
54 import org.osgi.service.component.annotations.Activate;
55 import org.osgi.service.component.annotations.Component;
56 import org.osgi.service.component.annotations.Reference;
59 * The {@link ShellyCHANNEL_DEFINITIONSDTO} defines channel information for dynamically created channels. Those will be
60 * added on the first thing status update
62 * @author Markus Michels - Initial contribution
65 @Component(service = ShellyChannelDefinitions.class)
66 public class ShellyChannelDefinitions {
68 public static final String ITEMT_STRING = "String";
69 public static final String ITEMT_NUMBER = "Number";
70 public static final String ITEMT_SWITCH = "Switch";
71 public static final String ITEMT_CONTACT = "Contact";
72 public static final String ITEMT_ROLLER = "Rollershutter";
73 public static final String ITEMT_DIMMER = "Dimmer";
74 public static final String ITEMT_LOCATION = "Location";
75 public static final String ITEMT_DATETIME = "DateTime";
76 public static final String ITEMT_TEMP = "Number:Temperature";
77 public static final String ITEMT_LUX = "Number:Illuminance";
78 public static final String ITEMT_POWER = "Number:Power";
79 public static final String ITEMT_ENERGY = "Number:Energy";
80 public static final String ITEMT_VOLT = "Number:ElectricPotential";
81 public static final String ITEMT_AMP = "Number:ElectricPotential";
82 public static final String ITEMT_ANGLE = "Number:Angle";
83 public static final String ITEMT_DISTANCE = "Number:Length";
84 public static final String ITEMT_SPEED = "Number:Speed";
85 public static final String ITEMT_VOLUME = "Number:Volume";
86 public static final String ITEMT_TIME = "Number:Time";
87 public static final String ITEMT_PERCENT = "Number:Dimensionless";
89 // shortcuts to avoid line breaks (make code more readable)
90 private static final String CHGR_DEVST = CHANNEL_GROUP_DEV_STATUS;
91 private static final String CHGR_RELAY = CHANNEL_GROUP_RELAY_CONTROL;
92 private static final String CHGR_ROLLER = CHANNEL_GROUP_ROL_CONTROL;
93 private static final String CHGR_LIGHT = CHANNEL_GROUP_LIGHT_CONTROL;
94 private static final String CHGR_LIGHTCH = CHANNEL_GROUP_LIGHT_CHANNEL;
95 private static final String CHGR_STATUS = CHANNEL_GROUP_STATUS;
96 private static final String CHGR_METER = CHANNEL_GROUP_METER;
97 private static final String CHGR_SENSOR = CHANNEL_GROUP_SENSOR;
98 private static final String CHGR_CONTROL = CHANNEL_GROUP_CONTROL;
99 private static final String CHGR_BAT = CHANNEL_GROUP_BATTERY;
101 public static final String PREFIX_GROUP = "group-type." + BINDING_ID + ".";
102 public static final String PREFIX_CHANNEL = "channel-type." + BINDING_ID + ".";
104 public class OptionEntry {
105 public ChannelTypeUID uid;
109 public OptionEntry(ChannelTypeUID uid, String key, String value) {
116 private final CopyOnWriteArrayList<OptionEntry> stateOptions = new CopyOnWriteArrayList<>();
118 private static final ChannelMap CHANNEL_DEFINITIONS = new ChannelMap();
121 public ShellyChannelDefinitions(@Reference ShellyTranslationProvider translationProvider) {
122 ShellyTranslationProvider m = translationProvider;
127 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_NAME, "deviceName", ITEMT_STRING))
128 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_GATEWAY, "gatewayDevice", ITEMT_STRING))
129 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_ITEMP, "deviceTemp", ITEMT_TEMP))
130 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_WAKEUP, "sensorWakeup", ITEMT_STRING))
131 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_ACCUWATTS, "meterAccuWatts", ITEMT_POWER))
132 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_ACCUTOTAL, "meterAccuTotal", ITEMT_POWER))
133 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_ACCURETURNED, "meterAccuReturned", ITEMT_POWER))
134 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_VOLTAGE, "supplyVoltage", ITEMT_VOLT))
135 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_CHARGER, "charger", ITEMT_SWITCH))
136 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_LED_STATUS_DISABLE, "ledStatusDisable", ITEMT_SWITCH))
137 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_LED_POWER_DISABLE, "ledPowerDisable", ITEMT_SWITCH))
138 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_SELFTTEST, "selfTest", ITEMT_STRING))
139 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_UPTIME, "uptime", ITEMT_NUMBER))
140 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_HEARTBEAT, "heartBeat", ITEMT_DATETIME))
141 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_UPDATE, "updateAvailable", ITEMT_SWITCH))
142 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_CALIBRATED, "calibrated", ITEMT_SWITCH))
145 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_OUTPUT_NAME, "outputName", ITEMT_STRING))
146 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_OUTPUT, "system:power", ITEMT_SWITCH))
147 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_INPUT, "inputState", ITEMT_SWITCH))
148 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_BUTTON_TRIGGER, "system:button", ITEMT_STRING))
149 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_STATUS_EVENTTYPE, "lastEvent", ITEMT_STRING))
150 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_STATUS_EVENTCOUNT, "eventCount", ITEMT_NUMBER))
151 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_TIMER_AUTOON, "timerAutoOn", ITEMT_TIME))
152 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_TIMER_AUTOOFF, "timerAutoOff", ITEMT_TIME))
153 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_TIMER_ACTIVE, "timerActive", ITEMT_SWITCH))
156 .add(new ShellyChannel(m, CHANNEL_GROUP_DIMMER_CONTROL, CHANNEL_BRIGHTNESS, "dimmerBrightness",
160 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_CONTROL, "rollerShutter", ITEMT_ROLLER))
161 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_POS, "rollerPosition", ITEMT_DIMMER))
162 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_FAV, "rollerFavorite", ITEMT_NUMBER))
163 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_STATE, "rollerState", ITEMT_STRING))
164 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_STOPR, "rollerStop", ITEMT_STRING))
165 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_SAFETY, "rollerSafety", ITEMT_SWITCH))
166 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_INPUT, "inputState", ITEMT_SWITCH))
167 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_STATUS_EVENTTYPE, "lastEvent", ITEMT_STRING))
168 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_STATUS_EVENTCOUNT, "eventCount", ITEMT_NUMBER))
169 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_EVENT_TRIGGER, "system:button", "system:button"))
172 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_INPUT, "inputState", ITEMT_SWITCH))
173 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_BUTTON_TRIGGER, "system:button", ITEMT_STRING))
174 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_STATUS_EVENTTYPE, "lastEvent", ITEMT_STRING))
175 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_STATUS_EVENTCOUNT, "eventCount", ITEMT_NUMBER))
176 .add(new ShellyChannel(m, CHANNEL_GROUP_WHITE_CONTROL, CHANNEL_BRIGHTNESS, "whiteBrightness",
178 .add(new ShellyChannel(m, CHANNEL_GROUP_WHITE_CONTROL, CHANNEL_COLOR_TEMP, "whiteTemp", ITEMT_DIMMER))
181 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_LIGHT_POWER, "system:power", ITEMT_SWITCH))
182 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_TIMER_AUTOON, "timerAutoOn", ITEMT_TIME))
183 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_TIMER_AUTOOFF, "timerAutoOff", ITEMT_TIME))
184 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_TIMER_ACTIVE, "timerActive", ITEMT_SWITCH))
186 .add(new ShellyChannel(m, CHGR_LIGHTCH, CHANNEL_BRIGHTNESS, "whiteBrightness", ITEMT_DIMMER))
187 .add(new ShellyChannel(m, CHGR_LIGHTCH, CHANNEL_TIMER_AUTOON, "timerAutoOn", ITEMT_TIME))
188 .add(new ShellyChannel(m, CHGR_LIGHTCH, CHANNEL_TIMER_AUTOOFF, "timerAutoOff", ITEMT_TIME))
189 .add(new ShellyChannel(m, CHGR_LIGHTCH, CHANNEL_TIMER_ACTIVE, "timerActive", ITEMT_SWITCH))
192 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_METER_CURRENTWATTS, "meterWatts", ITEMT_POWER))
193 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_METER_TOTALKWH, "meterTotal", ITEMT_ENERGY))
194 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_METER_LASTMIN1, "lastPower1", ITEMT_ENERGY))
195 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_LAST_UPDATE, "lastUpdate", ITEMT_DATETIME))
198 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_TOTALRET, "meterReturned", ITEMT_ENERGY))
199 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_REACTWATTS, "meterReactive", ITEMT_POWER))
200 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_VOLTAGE, "meterVoltage", ITEMT_VOLT))
201 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_CURRENT, "meterCurrent", ITEMT_AMP))
202 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_PFACTOR, "meterPowerFactor", ITEMT_NUMBER))
203 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_RESETTOTAL, "meterResetTotals", ITEMT_SWITCH))
206 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_TEMP, "sensorTemp", ITEMT_TEMP))
207 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_HUM, "sensorHumidity", ITEMT_PERCENT))
208 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_LUX, "sensorLux", ITEMT_LUX))
209 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_ILLUM, "sensorIllumination", ITEMT_STRING))
210 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_VOLTAGE, "sensorADC", ITEMT_VOLT))
211 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_STATE, "sensorContact", ITEMT_CONTACT))
212 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_OPEN, "sensorOpen", ITEMT_CONTACT))
213 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_SSTATE, "sensorState", ITEMT_STRING))
214 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_TILT, "sensorTilt", ITEMT_ANGLE))
215 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_MOTION, "sensorMotion", ITEMT_SWITCH))
216 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_MOTION_TS, "motionTimestamp", ITEMT_DATETIME))
217 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_MOTION_ACT, "motionActive", ITEMT_SWITCH))
218 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_VIBRATION, "vibration", ITEMT_SWITCH))
219 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_FLOOD, "sensorFlood", ITEMT_SWITCH))
220 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_SMOKE, "sensorSmoke", ITEMT_SWITCH))
221 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_MUTE, "sensorMute", ITEMT_SWITCH))
222 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_PPM, "sensorPPM", ITEMT_NUMBER))
223 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_VALVE, "sensorValve", ITEMT_STRING))
224 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_ALARM_STATE, "alarmState", ITEMT_STRING))
225 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_ERROR, "sensorError", ITEMT_STRING))
226 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_LAST_UPDATE, "lastUpdate", ITEMT_DATETIME))
227 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_SLEEPTIME, "sensorSleepTime", ITEMT_NUMBER))
228 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSE_KEY, "senseKey", ITEMT_STRING)) // Sense
231 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_INPUT, "inputState", ITEMT_SWITCH))
232 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_STATUS_EVENTTYPE, "lastEvent", ITEMT_STRING))
233 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_STATUS_EVENTCOUNT, "eventCount", ITEMT_NUMBER))
234 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_BUTTON_TRIGGER, "system:button", ITEMT_STRING))
235 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_LAST_UPDATE, "lastUpdate", ITEMT_DATETIME))
237 // Addon with external sensors
238 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENSOR_TEMP1, "sensorExtTemp", ITEMT_TEMP))
239 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENSOR_TEMP2, "sensorExtTemp", ITEMT_TEMP))
240 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENSOR_TEMP3, "sensorExtTemp", ITEMT_TEMP))
241 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENSOR_HUMIDITY, "sensorExtHum", ITEMT_PERCENT))
242 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENSOR_VOLTAGE, "sensorExtVolt", ITEMT_VOLT))
243 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENSOR_INPUT1, "sensorContact", ITEMT_CONTACT))
244 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENSOR_DIGITALINPUT, "sensorExtDigitalInput",
246 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENSOR_ANALOGINPUT, "sensorExtAnalogInput",
250 .add(new ShellyChannel(m, CHGR_BAT, CHANNEL_SENSOR_BAT_LEVEL, "system:battery-level", ITEMT_PERCENT))
251 .add(new ShellyChannel(m, CHGR_BAT, CHANNEL_SENSOR_BAT_LOW, "system:low-battery", ITEMT_SWITCH))
254 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_POSITION, "sensorPosition", ITEMT_DIMMER))
255 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_MODE, "controlMode", ITEMT_STRING))
256 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_PROFILE, "controlProfile", ITEMT_STRING))
257 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_SETTEMP, "targetTemp", ITEMT_TEMP))
258 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_BCONTROL, "boostControl", ITEMT_SWITCH))
259 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_BTIMER, "boostTimer", ITEMT_TIME))
260 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_SCHEDULE, "controlSchedule", ITEMT_SWITCH));
263 public static @Nullable ShellyChannel getDefinition(String channelName) throws IllegalArgumentException {
264 String group = substringBefore(channelName, "#");
265 String channel = substringAfter(channelName, "#");
267 if (group.contains(CHANNEL_GROUP_METER)) {
268 group = CHANNEL_GROUP_METER; // map meter1..n to meter
269 } else if (group.contains(CHANNEL_GROUP_RELAY_CONTROL)) {
270 group = CHANNEL_GROUP_RELAY_CONTROL; // map meter1..n to meter
271 } else if (group.contains(CHANNEL_GROUP_LIGHT_CHANNEL)) {
272 group = CHANNEL_GROUP_LIGHT_CHANNEL;
273 } else if (group.contains(CHANNEL_GROUP_STATUS)) {
274 group = CHANNEL_GROUP_STATUS; // map status1..n to meter
277 if (channel.startsWith(CHANNEL_INPUT)) {
278 channel = CHANNEL_INPUT;
279 } else if (channel.startsWith(CHANNEL_BUTTON_TRIGGER)) {
280 channel = CHANNEL_BUTTON_TRIGGER;
281 } else if (channel.startsWith(CHANNEL_STATUS_EVENTTYPE)) {
282 channel = CHANNEL_STATUS_EVENTTYPE;
283 } else if (channel.startsWith(CHANNEL_STATUS_EVENTCOUNT)) {
284 channel = CHANNEL_STATUS_EVENTCOUNT;
287 String channelId = group + "#" + channel;
288 return CHANNEL_DEFINITIONS.get(channelId);
292 * Auto-create relay channels depending on relay type/mode
294 * @return ArrayList<Channel> of channels to be added to the thing
296 public static Map<String, Channel> createDeviceChannels(final Thing thing, final ShellyDeviceProfile profile,
297 final ShellySettingsStatus status) {
298 Map<String, Channel> add = new LinkedHashMap<>();
300 addChannel(thing, add, profile.settings.name != null, CHGR_DEVST, CHANNEL_DEVST_NAME);
301 addChannel(thing, add, !profile.gateway.isEmpty() || profile.isBlu, CHGR_DEVST, CHANNEL_DEVST_GATEWAY);
303 if (!profile.isSensor && !profile.isIX
304 && ((status.temperature != null && getDouble(status.temperature) != SHELLY_API_INVTEMP)
305 || (status.tmp != null && getDouble(status.tmp.tC) != SHELLY_API_INVTEMP))) {
306 // Only some devices report the internal device temp
307 addChannel(thing, add, status.tmp != null || status.temperature != null, CHGR_DEVST, CHANNEL_DEVST_ITEMP);
309 addChannel(thing, add, profile.settings.sleepTime != null, CHGR_SENSOR, CHANNEL_SENSOR_SLEEPTIME);
311 // If device has more than 1 meter the channel accumulatedWatts receives the accumulated value
312 boolean accuChannel = (((status.meters != null) && (status.meters.size() > 1) && !profile.isRoller
313 && !profile.isRGBW2) || ((status.emeters != null && status.emeters.size() > 1)));
314 addChannel(thing, add, accuChannel, CHGR_DEVST, CHANNEL_DEVST_ACCUWATTS);
315 addChannel(thing, add, accuChannel, CHGR_DEVST, CHANNEL_DEVST_ACCUTOTAL);
316 addChannel(thing, add, accuChannel && (status.emeters != null), CHGR_DEVST, CHANNEL_DEVST_ACCURETURNED);
317 addChannel(thing, add, status.voltage != null || profile.settings.supplyVoltage != null, CHGR_DEVST,
318 CHANNEL_DEVST_VOLTAGE);
319 addChannel(thing, add,
320 profile.status.uptime != null && (!profile.hasBattery || profile.isMotion || profile.isTRV), CHGR_DEVST,
321 CHANNEL_DEVST_UPTIME);
322 addChannel(thing, add, true, CHGR_DEVST, CHANNEL_DEVST_UPDATE);
323 addChannel(thing, add, true, CHGR_DEVST, CHANNEL_DEVST_HEARTBEAT);
324 addChannel(thing, add, profile.settings.ledPowerDisable != null, CHGR_DEVST, CHANNEL_LED_POWER_DISABLE);
325 addChannel(thing, add, profile.settings.ledStatusDisable != null, CHGR_DEVST, CHANNEL_LED_STATUS_DISABLE); // WiFi
326 addChannel(thing, add, profile.settings.calibrated != null, CHGR_DEVST, CHANNEL_DEVST_CALIBRATED);
332 * Auto-create relay channels depending on relay type/mode
334 * @return ArrayList<Channel> of channels to be added to the thing
336 public static Map<String, Channel> createRelayChannels(final Thing thing, final ShellyDeviceProfile profile,
337 final ShellySettingsRelay rstatus, int idx) {
338 Map<String, Channel> add = new LinkedHashMap<>();
339 String group = profile.getControlGroup(idx);
341 if (profile.settings.relays != null) {
342 ShellySettingsRelay rs = profile.settings.relays.get(idx);
343 addChannel(thing, add, rs.ison != null, group, CHANNEL_OUTPUT);
344 addChannel(thing, add, rs.name != null, group, CHANNEL_OUTPUT_NAME);
346 boolean timer = rs.hasTimer != null || rstatus.hasTimer != null; // Dimmer 1/2 have
347 addChannel(thing, add, timer, group, CHANNEL_TIMER_ACTIVE);
348 addChannel(thing, add, rs.autoOn != null, group, CHANNEL_TIMER_AUTOON);
349 addChannel(thing, add, rs.autoOff != null, group, CHANNEL_TIMER_AUTOOFF);
352 // Shelly 1/1PM and Plus 1/1PM Addon
353 if (profile.settings.extSwitch != null && profile.settings.extSwitch.input0 != null
354 && idx == getInteger(profile.settings.extSwitch.input0.relayNum)) {
355 addChannel(thing, add, true, CHGR_SENSOR, CHANNEL_ESENSOR_INPUT + (idx + 1));
357 if (profile.status.extTemperature != null) {
358 addChannel(thing, add, profile.status.extTemperature.sensor1 != null, CHGR_SENSOR, CHANNEL_ESENSOR_TEMP1);
359 addChannel(thing, add, profile.status.extTemperature.sensor2 != null, CHGR_SENSOR, CHANNEL_ESENSOR_TEMP2);
360 addChannel(thing, add, profile.status.extTemperature.sensor3 != null, CHGR_SENSOR, CHANNEL_ESENSOR_TEMP3);
361 addChannel(thing, add, profile.status.extTemperature.sensor4 != null, CHGR_SENSOR, CHANNEL_ESENSOR_TEMP4);
362 addChannel(thing, add, profile.status.extTemperature.sensor5 != null, CHGR_SENSOR, CHANNEL_ESENSOR_TEMP5);
364 addChannel(thing, add, profile.status.extHumidity != null, CHGR_SENSOR, CHANNEL_ESENSOR_HUMIDITY);
365 addChannel(thing, add, profile.status.extVoltage != null, CHGR_SENSOR, CHANNEL_ESENSOR_VOLTAGE);
366 addChannel(thing, add, profile.status.extDigitalInput != null, CHGR_SENSOR, CHANNEL_ESENSOR_DIGITALINPUT);
367 addChannel(thing, add, profile.status.extAnalogInput != null, CHGR_SENSOR, CHANNEL_ESENSOR_ANALOGINPUT);
372 public static Map<String, Channel> createDimmerChannels(final Thing thing, final ShellyDeviceProfile profile,
373 final ShellySettingsStatus dstatus, int idx) {
374 Map<String, Channel> add = new LinkedHashMap<>();
375 String group = profile.getControlGroup(idx);
377 // Shelly Dimmer has an additional brightness channel
378 addChannel(thing, add, profile.isDimmer, group, CHANNEL_BRIGHTNESS);
380 if (profile.settings.dimmers != null) {
381 ShellySettingsDimmer ds = profile.settings.dimmers.get(idx);
382 addChannel(thing, add, ds.autoOn != null, group, CHANNEL_TIMER_AUTOON);
383 addChannel(thing, add, ds.autoOff != null, group, CHANNEL_TIMER_AUTOOFF);
384 ShellyShortLightStatus dss = dstatus.dimmers.get(idx);
385 addChannel(thing, add, dss != null && dss.hasTimer != null, group, CHANNEL_TIMER_ACTIVE);
390 public static Map<String, Channel> createLightChannels(final Thing thing, final ShellyDeviceProfile profile,
391 final ShellyStatusLightChannel status, int idx) {
392 Map<String, Channel> add = new LinkedHashMap<>();
393 String group = profile.getControlGroup(idx);
395 if (profile.settings.lights != null) {
396 ShellySettingsRgbwLight light = profile.settings.lights.get(idx);
397 String whiteGroup = profile.isRGBW2 ? group : CHANNEL_GROUP_WHITE_CONTROL;
398 // Create power channel in color mode and brightness channel in white mode
399 addChannel(thing, add, profile.inColor, group, CHANNEL_LIGHT_POWER);
400 addChannel(thing, add, light.autoOn != null, group, CHANNEL_TIMER_AUTOON);
401 addChannel(thing, add, light.autoOff != null, group, CHANNEL_TIMER_AUTOOFF);
402 addChannel(thing, add, status.hasTimer != null, group, CHANNEL_TIMER_ACTIVE);
403 addChannel(thing, add, light.brightness != null, whiteGroup, CHANNEL_BRIGHTNESS);
404 addChannel(thing, add, light.temp != null, whiteGroup, CHANNEL_COLOR_TEMP);
410 public static Map<String, Channel> createInputChannels(final Thing thing, final ShellyDeviceProfile profile,
411 final ShellySettingsStatus status) {
412 Map<String, Channel> add = new LinkedHashMap<>();
413 if (status.inputs != null) {
414 // Create channels per input. For devices with more than 1 input (Dimmer, 1L) multiple channel sets are
415 // created by adding the index to the channel name
416 for (int i = 0; i < profile.numInputs; i++) {
417 String group = profile.getInputGroup(i);
418 String suffix = profile.getInputSuffix(i); // multi ? String.valueOf(i + 1) : "";
419 addChannel(thing, add, !profile.isButton, group, CHANNEL_INPUT + suffix);
420 addChannel(thing, add, true, group,
421 (!profile.isRoller ? CHANNEL_BUTTON_TRIGGER + suffix : CHANNEL_EVENT_TRIGGER));
422 if (profile.inButtonMode(i)) {
423 ShellyInputState input = status.inputs.get(i);
424 addChannel(thing, add, input.event != null, group, CHANNEL_STATUS_EVENTTYPE + suffix);
425 addChannel(thing, add, input.eventCount != null, group, CHANNEL_STATUS_EVENTCOUNT + suffix);
428 } else if (status.input != null) {
429 // old RGBW2 firmware
430 String group = profile.getInputGroup(0);
431 addChannel(thing, add, true, group, CHANNEL_INPUT);
432 addChannel(thing, add, true, group, CHANNEL_BUTTON_TRIGGER);
437 public static Map<String, Channel> createRollerChannels(Thing thing, final ShellyRollerStatus roller) {
438 Map<String, Channel> add = new LinkedHashMap<>();
439 addChannel(thing, add, true, CHGR_ROLLER, CHANNEL_ROL_CONTROL_CONTROL);
440 addChannel(thing, add, true, CHGR_ROLLER, CHANNEL_ROL_CONTROL_STATE);
441 addChannel(thing, add, true, CHGR_ROLLER, CHANNEL_EVENT_TRIGGER);
442 addChannel(thing, add, roller.currentPos != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_POS);
443 addChannel(thing, add, roller.stopReason != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_STOPR);
444 addChannel(thing, add, roller.safetySwitch != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_SAFETY);
446 ShellyThingInterface handler = (ShellyThingInterface) thing.getHandler();
447 if (handler != null) {
448 ShellySettingsGlobal settings = handler.getProfile().settings;
449 if (getBool(settings.favoritesEnabled) && settings.favorites != null) {
450 addChannel(thing, add, roller.currentPos != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_FAV);
456 public static Map<String, Channel> createMeterChannels(Thing thing, final ShellySettingsMeter meter, String group) {
457 Map<String, Channel> newChannels = new LinkedHashMap<>();
458 addChannel(thing, newChannels, meter.power != null, group, CHANNEL_METER_CURRENTWATTS);
459 addChannel(thing, newChannels, meter.total != null, group, CHANNEL_METER_TOTALKWH);
460 addChannel(thing, newChannels, meter.counters != null && meter.counters[0] != null, group,
461 CHANNEL_METER_LASTMIN1);
462 addChannel(thing, newChannels, meter.timestamp != null, group, CHANNEL_LAST_UPDATE);
466 public static Map<String, Channel> createEMeterChannels(final Thing thing, final ShellyDeviceProfile profile,
467 final ShellySettingsEMeter emeter, String group) {
468 Map<String, Channel> newChannels = new LinkedHashMap<>();
469 addChannel(thing, newChannels, emeter.power != null, group, CHANNEL_METER_CURRENTWATTS);
470 addChannel(thing, newChannels, emeter.total != null, group, CHANNEL_METER_TOTALKWH);
471 addChannel(thing, newChannels, emeter.totalReturned != null, group, CHANNEL_EMETER_TOTALRET);
472 addChannel(thing, newChannels, emeter.reactive != null, group, CHANNEL_EMETER_REACTWATTS);
473 addChannel(thing, newChannels, emeter.voltage != null, group, CHANNEL_EMETER_VOLTAGE);
474 addChannel(thing, newChannels, emeter.current != null, group, CHANNEL_EMETER_CURRENT);
475 addChannel(thing, newChannels, emeter.pf != null, group, CHANNEL_EMETER_PFACTOR); // EM has no PF. but power
476 addChannel(thing, newChannels, emeter.total != null && profile.numMeters > 1, group, CHANNEL_EMETER_RESETTOTAL); // 3EM
477 addChannel(thing, newChannels, true, group, CHANNEL_LAST_UPDATE);
481 public static Map<String, Channel> createSensorChannels(final Thing thing, final ShellyDeviceProfile profile,
482 final ShellyStatusSensor sdata) {
483 Map<String, Channel> newChannels = new LinkedHashMap<>();
486 addChannel(thing, newChannels, sdata.tmp != null || sdata.thermostats != null, CHANNEL_GROUP_SENSOR,
487 CHANNEL_SENSOR_TEMP);
488 addChannel(thing, newChannels, sdata.hum != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_HUM);
489 addChannel(thing, newChannels, sdata.lux != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_LUX);
490 addChannel(thing, newChannels, sdata.lux != null && sdata.lux.illumination != null, CHANNEL_GROUP_SENSOR,
491 CHANNEL_SENSOR_ILLUM);
492 addChannel(thing, newChannels, sdata.flood != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_FLOOD);
493 addChannel(thing, newChannels, sdata.smoke != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_SMOKE);
494 addChannel(thing, newChannels, sdata.mute != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_MUTE);
495 addChannel(thing, newChannels, profile.settings.externalPower != null || sdata.charger != null, CHGR_DEVST,
496 CHANNEL_DEVST_CHARGER);
497 addChannel(thing, newChannels, sdata.motion != null || (sdata.sensor != null && sdata.sensor.motion != null),
498 CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_MOTION);
499 if (sdata.sensor != null) { // DW, Sense or Motion
500 addChannel(thing, newChannels, sdata.sensor.state != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_STATE); // DW/DW2
501 addChannel(thing, newChannels, sdata.sensor.motionActive != null, CHANNEL_GROUP_SENSOR, // Motion
502 CHANNEL_SENSOR_MOTION_ACT);
503 addChannel(thing, newChannels, sdata.sensor.motionTimestamp != null, CHANNEL_GROUP_SENSOR, // Motion
504 CHANNEL_SENSOR_MOTION_TS);
505 addChannel(thing, newChannels, sdata.sensor.vibration != null, CHANNEL_GROUP_SENSOR,
506 CHANNEL_SENSOR_VIBRATION);
508 if (sdata.accel != null) { // DW2
509 addChannel(thing, newChannels, sdata.accel.tilt != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_TILT);
513 if (sdata.gasSensor != null) {
514 addChannel(thing, newChannels, sdata.gasSensor.selfTestState != null, CHGR_DEVST, CHANNEL_DEVST_SELFTTEST);
515 addChannel(thing, newChannels, sdata.gasSensor.sensorState != null, CHANNEL_GROUP_SENSOR,
516 CHANNEL_SENSOR_SSTATE);
517 addChannel(thing, newChannels, sdata.concentration != null && sdata.concentration.ppm != null,
518 CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_PPM);
519 addChannel(thing, newChannels, sdata.valves != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_VALVE);
520 addChannel(thing, newChannels, sdata.gasSensor.sensorState != null, CHANNEL_GROUP_SENSOR,
521 CHANNEL_SENSOR_ALARM_STATE);
525 addChannel(thing, newChannels, profile.isSense, CHANNEL_GROUP_SENSOR, CHANNEL_SENSE_KEY);
528 addChannel(thing, newChannels, sdata.adcs != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_VOLTAGE);
532 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_SETTEMP);
533 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_BCONTROL);
534 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_BTIMER);
535 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_POSITION);
536 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_MODE);
537 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_PROFILE);
538 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_SCHEDULE);
539 addChannel(thing, newChannels, true, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_STATE);
540 addChannel(thing, newChannels, true, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_OPEN);
544 if (sdata.bat != null) {
545 addChannel(thing, newChannels, sdata.bat.value != null, CHANNEL_GROUP_BATTERY, CHANNEL_SENSOR_BAT_LEVEL);
546 addChannel(thing, newChannels, sdata.bat.value != null, CHANNEL_GROUP_BATTERY, CHANNEL_SENSOR_BAT_LOW);
549 addChannel(thing, newChannels, sdata.sensorError != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_ERROR);
550 addChannel(thing, newChannels, sdata.actReasons != null, CHGR_DEVST, CHANNEL_DEVST_WAKEUP);
551 addChannel(thing, newChannels, true, profile.isButton ? CHANNEL_GROUP_STATUS : CHANNEL_GROUP_SENSOR,
552 CHANNEL_LAST_UPDATE);
556 public ChannelTypeUID getChannelTypeUID(String channelId) {
557 ShellyChannel channelDef = getDefinition(channelId);
558 if (channelDef != null) {
559 return new ChannelTypeUID(BINDING_ID, channelDef.typeId);
561 throw new IllegalArgumentException("Invalid channelId:" + channelId);
564 private static void addChannel(Thing thing, Map<String, Channel> newChannels, boolean supported, String group,
565 String channelName) throws IllegalArgumentException {
567 String channelId = group + "#" + channelName;
568 ChannelUID channelUID = new ChannelUID(thing.getUID(), channelId);
569 ShellyChannel channelDef = getDefinition(channelId);
570 if (channelDef != null) {
571 ChannelTypeUID channelTypeUID = channelDef.typeId.contains("system:")
572 ? new ChannelTypeUID(channelDef.typeId)
573 : new ChannelTypeUID(BINDING_ID, channelDef.typeId);
574 ChannelBuilder builder;
575 if (channelDef.typeId.equalsIgnoreCase("system:button")) {
576 builder = ChannelBuilder.create(channelUID, null).withKind(ChannelKind.TRIGGER);
578 builder = ChannelBuilder.create(channelUID, channelDef.itemType);
580 if (!channelDef.label.isEmpty()) {
581 char grseq = lastChar(group);
582 char chseq = lastChar(channelName);
583 char sequence = isDigit(chseq) ? chseq : grseq;
584 String label = !isDigit(sequence) ? channelDef.label : channelDef.label + " " + sequence;
585 builder.withLabel(label);
587 if (!channelDef.description.isEmpty()) {
588 builder.withDescription(channelDef.description);
590 newChannels.put(channelId, builder.withType(channelTypeUID).build());
595 public List<StateOption> getStateOptions(ChannelTypeUID uid) {
596 List<StateOption> options = new ArrayList<>();
597 for (OptionEntry oe : stateOptions) {
598 if (oe.uid.equals(uid)) {
599 options.add(new StateOption(oe.key, oe.value));
605 public void addStateOption(String channelId, String key, String value) {
606 ChannelTypeUID uid = getChannelTypeUID(channelId);
607 stateOptions.addIfAbsent(new OptionEntry(uid, key, value));
610 public void clearStateOptions(String channelId) {
611 ChannelTypeUID uid = getChannelTypeUID(channelId);
612 for (OptionEntry oe : stateOptions) {
613 if (oe.uid.equals(uid)) {
614 stateOptions.remove(oe);
619 public class ShellyChannel {
620 private final ShellyTranslationProvider messages;
621 public String group = "";
622 public String groupLabel = "";
623 public String groupDescription = "";
625 public String channel = "";
626 public String label = "";
627 public String description = "";
628 public String itemType = "";
629 public String typeId = "";
630 public String category = "";
631 public Set<String> tags = new HashSet<>();
632 public @Nullable Unit<?> unit;
633 public Optional<Integer> min = Optional.empty();
634 public Optional<Integer> max = Optional.empty();
635 public Optional<Integer> step = Optional.empty();
636 public Optional<String> pattern = Optional.empty();
638 public ShellyChannel(ShellyTranslationProvider messages, String group, String channel, String typeId,
639 String itemType, String... category) {
640 this.messages = messages;
642 this.channel = channel;
643 this.itemType = itemType;
644 this.typeId = typeId;
646 groupLabel = getText(PREFIX_GROUP + group + ".label");
647 if (groupLabel.contains(PREFIX_GROUP)) {
650 groupDescription = getText(PREFIX_GROUP + group + ".description");
651 if (groupDescription.contains(PREFIX_GROUP)) {
652 groupDescription = "";
654 label = getText(PREFIX_CHANNEL + typeId.replace(':', '.') + ".label");
655 if (label.contains(PREFIX_CHANNEL)) {
658 description = getText(PREFIX_CHANNEL + typeId + ".description");
659 if (description.contains(PREFIX_CHANNEL)) {
664 public String getChanneId() {
665 return group + "#" + channel;
668 public String getGroupLabel() {
669 return getGroupAttribute("group");
672 public String getGroupDescription() {
673 return getGroupAttribute("group");
676 public String getLabel() {
677 return getChannelAttribute("label");
680 public String getDescription() {
681 return getChannelAttribute("description");
684 public boolean getAdvanced() {
685 String attr = getChannelAttribute("advanced");
686 return attr.isEmpty() ? false : Boolean.valueOf(attr);
689 public boolean getReadyOnly() {
690 String attr = getChannelAttribute("readOnly");
691 return attr.isEmpty() ? false : Boolean.valueOf(attr);
694 public String getCategory() {
695 return getChannelAttribute("category");
698 public String getMin() {
699 return getChannelAttribute("min");
702 public String getMax() {
703 return getChannelAttribute("max");
706 public String getStep() {
707 return getChannelAttribute("step");
710 public String getPattern() {
711 return getChannelAttribute("pattern");
714 public String getGroupAttribute(String attribute) {
715 String key = PREFIX_GROUP + group + "." + attribute;
716 String value = messages.getText(key);
717 return !value.equals(key) ? value : "";
720 public String getChannelAttribute(String attribute) {
721 String key = PREFIX_CHANNEL + channel + "." + attribute;
722 String value = messages.getText(key);
723 return !value.equals(key) ? value : "";
726 private String getText(String key) {
727 return messages.get(key);
731 public static class ChannelMap {
732 private final Map<String, ShellyChannel> map = new HashMap<>();
734 private ChannelMap add(ShellyChannel def) {
735 map.put(def.getChanneId(), def);
739 public ShellyChannel get(String channelName) throws IllegalArgumentException {
740 ShellyChannel def = null;
741 if (channelName.contains("#")) {
742 def = map.get(channelName);
747 for (HashMap.Entry<String, ShellyChannel> entry : map.entrySet()) {
748 if (entry.getValue().channel.contains("#" + channelName)) {
749 def = entry.getValue();
755 throw new IllegalArgumentException("Channel definition for " + channelName + " not found!");