2 * Copyright (c) 2010-2022 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_ITEMP, "deviceTemp", ITEMT_TEMP))
129 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_WAKEUP, "sensorWakeup", ITEMT_STRING))
130 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_ACCUWATTS, "meterAccuWatts", ITEMT_POWER))
131 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_ACCUTOTAL, "meterAccuTotal", ITEMT_POWER))
132 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_ACCURETURNED, "meterAccuReturned", ITEMT_POWER))
133 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_VOLTAGE, "supplyVoltage", ITEMT_VOLT))
134 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_CHARGER, "charger", ITEMT_SWITCH))
135 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_LED_STATUS_DISABLE, "ledStatusDisable", ITEMT_SWITCH))
136 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_LED_POWER_DISABLE, "ledPowerDisable", ITEMT_SWITCH))
137 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_SELFTTEST, "selfTest", ITEMT_STRING))
138 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_UPTIME, "uptime", ITEMT_NUMBER))
139 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_HEARTBEAT, "heartBeat", ITEMT_DATETIME))
140 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_UPDATE, "updateAvailable", ITEMT_SWITCH))
141 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_CALIBRATED, "calibrated", ITEMT_SWITCH))
144 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_OUTPUT_NAME, "outputName", ITEMT_STRING))
145 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_OUTPUT, "system:power", ITEMT_SWITCH))
146 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_INPUT, "inputState", ITEMT_SWITCH))
147 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_BUTTON_TRIGGER, "system:button", ITEMT_STRING))
148 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_STATUS_EVENTTYPE, "lastEvent", ITEMT_STRING))
149 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_STATUS_EVENTCOUNT, "eventCount", ITEMT_NUMBER))
150 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_TIMER_AUTOON, "timerAutoOn", ITEMT_TIME))
151 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_TIMER_AUTOOFF, "timerAutoOff", ITEMT_TIME))
152 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_TIMER_ACTIVE, "timerActive", ITEMT_SWITCH))
155 .add(new ShellyChannel(m, CHANNEL_GROUP_DIMMER_CONTROL, CHANNEL_BRIGHTNESS, "dimmerBrightness",
159 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_CONTROL, "rollerShutter", ITEMT_ROLLER))
160 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_POS, "rollerPosition", ITEMT_DIMMER))
161 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_FAV, "rollerFavorite", ITEMT_NUMBER))
162 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_STATE, "rollerState", ITEMT_STRING))
163 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_STOPR, "rollerStop", ITEMT_STRING))
164 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_SAFETY, "rollerSafety", ITEMT_SWITCH))
165 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_INPUT, "inputState", ITEMT_SWITCH))
166 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_STATUS_EVENTTYPE, "lastEvent", ITEMT_STRING))
167 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_STATUS_EVENTCOUNT, "eventCount", ITEMT_NUMBER))
168 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_EVENT_TRIGGER, "system:button", "system:button"))
171 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_INPUT, "inputState", ITEMT_SWITCH))
172 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_BUTTON_TRIGGER, "system:button", ITEMT_STRING))
173 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_STATUS_EVENTTYPE, "lastEvent", ITEMT_STRING))
174 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_STATUS_EVENTCOUNT, "eventCount", ITEMT_NUMBER))
175 .add(new ShellyChannel(m, CHANNEL_GROUP_WHITE_CONTROL, CHANNEL_BRIGHTNESS, "whiteBrightness",
177 .add(new ShellyChannel(m, CHANNEL_GROUP_WHITE_CONTROL, CHANNEL_COLOR_TEMP, "whiteTemp", ITEMT_DIMMER))
180 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_LIGHT_POWER, "system:power", ITEMT_SWITCH))
181 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_TIMER_AUTOON, "timerAutoOn", ITEMT_TIME))
182 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_TIMER_AUTOOFF, "timerAutoOff", ITEMT_TIME))
183 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_TIMER_ACTIVE, "timerActive", ITEMT_SWITCH))
185 .add(new ShellyChannel(m, CHGR_LIGHTCH, CHANNEL_BRIGHTNESS, "whiteBrightness", ITEMT_DIMMER))
186 .add(new ShellyChannel(m, CHGR_LIGHTCH, CHANNEL_TIMER_AUTOON, "timerAutoOn", ITEMT_TIME))
187 .add(new ShellyChannel(m, CHGR_LIGHTCH, CHANNEL_TIMER_AUTOOFF, "timerAutoOff", ITEMT_TIME))
188 .add(new ShellyChannel(m, CHGR_LIGHTCH, CHANNEL_TIMER_ACTIVE, "timerActive", ITEMT_SWITCH))
191 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_METER_CURRENTWATTS, "meterWatts", ITEMT_POWER))
192 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_METER_TOTALKWH, "meterTotal", ITEMT_ENERGY))
193 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_METER_LASTMIN1, "lastPower1", ITEMT_ENERGY))
194 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_LAST_UPDATE, "lastUpdate", ITEMT_DATETIME))
197 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_TOTALRET, "meterReturned", ITEMT_ENERGY))
198 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_REACTWATTS, "meterReactive", ITEMT_POWER))
199 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_VOLTAGE, "meterVoltage", ITEMT_VOLT))
200 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_CURRENT, "meterCurrent", ITEMT_AMP))
201 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_PFACTOR, "meterPowerFactor", ITEMT_NUMBER))
204 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_TEMP, "sensorTemp", ITEMT_TEMP))
205 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_HUM, "sensorHumidity", ITEMT_PERCENT))
206 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_LUX, "sensorLux", ITEMT_LUX))
207 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_ILLUM, "sensorIllumination", ITEMT_STRING))
208 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_VOLTAGE, "sensorADC", ITEMT_VOLT))
209 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_STATE, "sensorContact", ITEMT_CONTACT))
210 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_SSTATE, "sensorState", ITEMT_STRING))
211 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_TILT, "sensorTilt", ITEMT_ANGLE))
212 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_MOTION, "sensorMotion", ITEMT_SWITCH))
213 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_MOTION_TS, "motionTimestamp", ITEMT_DATETIME))
214 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_MOTION_ACT, "motionActive", ITEMT_SWITCH))
215 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_VIBRATION, "vibration", ITEMT_SWITCH))
216 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_FLOOD, "sensorFlood", ITEMT_SWITCH))
217 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_SMOKE, "sensorSmoke", ITEMT_SWITCH))
218 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_PPM, "sensorPPM", ITEMT_NUMBER))
219 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_VALVE, "sensorValve", ITEMT_STRING))
220 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_ALARM_STATE, "alarmState", ITEMT_STRING))
221 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_ERROR, "sensorError", ITEMT_STRING))
222 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_LAST_UPDATE, "lastUpdate", ITEMT_DATETIME))
223 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_SLEEPTIME, "sensorSleepTime", ITEMT_NUMBER))
224 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSE_KEY, "senseKey", ITEMT_STRING)) // Sense
227 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_INPUT, "inputState", ITEMT_SWITCH))
228 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_STATUS_EVENTTYPE, "lastEvent", ITEMT_STRING))
229 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_STATUS_EVENTCOUNT, "eventCount", ITEMT_NUMBER))
230 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_BUTTON_TRIGGER, "system:button", ITEMT_STRING))
231 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_LAST_UPDATE, "lastUpdate", ITEMT_DATETIME))
233 // Addon with external sensors
234 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENDOR_TEMP1, "sensorExtTemp", ITEMT_TEMP))
235 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENDOR_TEMP2, "sensorExtTemp", ITEMT_TEMP))
236 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENDOR_TEMP3, "sensorExtTemp", ITEMT_TEMP))
237 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENDOR_HUMIDITY, "sensorExtHum", ITEMT_PERCENT))
240 .add(new ShellyChannel(m, CHGR_BAT, CHANNEL_SENSOR_BAT_LEVEL, "system:battery-level", ITEMT_PERCENT))
241 .add(new ShellyChannel(m, CHGR_BAT, CHANNEL_SENSOR_BAT_LOW, "system:low-battery", ITEMT_SWITCH))
244 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_POSITION, "sensorPosition", ITEMT_DIMMER))
245 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_MODE, "controlMode", ITEMT_STRING))
246 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_PROFILE, "controlProfile", ITEMT_STRING))
247 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_SETTEMP, "targetTemp", ITEMT_TEMP))
248 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_BCONTROL, "boostControl", ITEMT_SWITCH))
249 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_BTIMER, "boostTimer", ITEMT_TIME))
250 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_SCHEDULE, "controlSchedule", ITEMT_SWITCH));
253 public static @Nullable ShellyChannel getDefinition(String channelName) throws IllegalArgumentException {
254 String group = substringBefore(channelName, "#");
255 String channel = substringAfter(channelName, "#");
257 if (group.contains(CHANNEL_GROUP_METER)) {
258 group = CHANNEL_GROUP_METER; // map meter1..n to meter
259 } else if (group.contains(CHANNEL_GROUP_RELAY_CONTROL)) {
260 group = CHANNEL_GROUP_RELAY_CONTROL; // map meter1..n to meter
261 } else if (group.contains(CHANNEL_GROUP_LIGHT_CHANNEL)) {
262 group = CHANNEL_GROUP_LIGHT_CHANNEL;
263 } else if (group.contains(CHANNEL_GROUP_STATUS)) {
264 group = CHANNEL_GROUP_STATUS; // map status1..n to meter
267 if (channel.startsWith(CHANNEL_INPUT)) {
268 channel = CHANNEL_INPUT;
269 } else if (channel.startsWith(CHANNEL_BUTTON_TRIGGER)) {
270 channel = CHANNEL_BUTTON_TRIGGER;
271 } else if (channel.startsWith(CHANNEL_STATUS_EVENTTYPE)) {
272 channel = CHANNEL_STATUS_EVENTTYPE;
273 } else if (channel.startsWith(CHANNEL_STATUS_EVENTCOUNT)) {
274 channel = CHANNEL_STATUS_EVENTCOUNT;
277 String channelId = group + "#" + channel;
278 return CHANNEL_DEFINITIONS.get(channelId);
282 * Auto-create relay channels depending on relay type/mode
284 * @return ArrayList<Channel> of channels to be added to the thing
286 public static Map<String, Channel> createDeviceChannels(final Thing thing, final ShellyDeviceProfile profile,
287 final ShellySettingsStatus status) {
288 Map<String, Channel> add = new LinkedHashMap<>();
290 addChannel(thing, add, profile.settings.name != null, CHGR_DEVST, CHANNEL_DEVST_NAME);
292 if (!profile.isSensor && !profile.isIX && status.temperature != null
293 && status.temperature != SHELLY_API_INVTEMP) {
294 // Only some devices report the internal device temp
295 addChannel(thing, add, status.tmp != null || status.temperature != null, CHGR_DEVST, CHANNEL_DEVST_ITEMP);
297 addChannel(thing, add, profile.settings.sleepTime != null, CHGR_SENSOR, CHANNEL_SENSOR_SLEEPTIME);
299 // If device has more than 1 meter the channel accumulatedWatts receives the accumulated value
300 boolean accuChannel = (((status.meters != null) && (status.meters.size() > 1) && !profile.isRoller
301 && !profile.isRGBW2) || ((status.emeters != null && status.emeters.size() > 1)));
302 addChannel(thing, add, accuChannel, CHGR_DEVST, CHANNEL_DEVST_ACCUWATTS);
303 addChannel(thing, add, accuChannel, CHGR_DEVST, CHANNEL_DEVST_ACCUTOTAL);
304 addChannel(thing, add, accuChannel && (status.emeters != null), CHGR_DEVST, CHANNEL_DEVST_ACCURETURNED);
305 addChannel(thing, add, status.voltage != null || profile.settings.supplyVoltage != null, CHGR_DEVST,
306 CHANNEL_DEVST_VOLTAGE);
307 addChannel(thing, add,
308 profile.status.uptime != null && (!profile.hasBattery || profile.isMotion || profile.isTRV), CHGR_DEVST,
309 CHANNEL_DEVST_UPTIME);
310 addChannel(thing, add, true, CHGR_DEVST, CHANNEL_DEVST_UPDATE);
311 addChannel(thing, add, true, CHGR_DEVST, CHANNEL_DEVST_HEARTBEAT);
312 addChannel(thing, add, profile.settings.ledPowerDisable != null, CHGR_DEVST, CHANNEL_LED_POWER_DISABLE);
313 addChannel(thing, add, profile.settings.ledStatusDisable != null, CHGR_DEVST, CHANNEL_LED_STATUS_DISABLE); // WiFi
314 addChannel(thing, add, profile.settings.calibrated != null, CHGR_DEVST, CHANNEL_DEVST_CALIBRATED);
320 * Auto-create relay channels depending on relay type/mode
322 * @return ArrayList<Channel> of channels to be added to the thing
324 public static Map<String, Channel> createRelayChannels(final Thing thing, final ShellyDeviceProfile profile,
325 final ShellySettingsRelay rstatus, int idx) {
326 Map<String, Channel> add = new LinkedHashMap<>();
327 String group = profile.getControlGroup(idx);
329 if (profile.settings.relays != null) {
330 ShellySettingsRelay rs = profile.settings.relays.get(idx);
331 addChannel(thing, add, rs.ison != null, group, CHANNEL_OUTPUT);
332 addChannel(thing, add, rs.name != null, group, CHANNEL_OUTPUT_NAME);
334 boolean timer = rs.hasTimer != null || rstatus.hasTimer != null; // Dimmer 1/2 have
335 addChannel(thing, add, timer, group, CHANNEL_TIMER_ACTIVE);
336 addChannel(thing, add, rs.autoOn != null, group, CHANNEL_TIMER_AUTOON);
337 addChannel(thing, add, rs.autoOff != null, group, CHANNEL_TIMER_AUTOOFF);
340 // Shelly 1/1PM Addon
341 if (profile.status.extTemperature != null) {
342 addChannel(thing, add, profile.status.extTemperature.sensor1 != null, CHGR_SENSOR, CHANNEL_ESENDOR_TEMP1);
343 addChannel(thing, add, profile.status.extTemperature.sensor2 != null, CHGR_SENSOR, CHANNEL_ESENDOR_TEMP2);
344 addChannel(thing, add, profile.status.extTemperature.sensor3 != null, CHGR_SENSOR, CHANNEL_ESENDOR_TEMP3);
346 if (profile.settings.extHumidity != null) {
347 addChannel(thing, add, profile.settings.extHumidity.sensor1 != null, CHGR_SENSOR, CHANNEL_ESENDOR_HUMIDITY);
353 public static Map<String, Channel> createDimmerChannels(final Thing thing, final ShellyDeviceProfile profile,
354 final ShellySettingsStatus dstatus, int idx) {
355 Map<String, Channel> add = new LinkedHashMap<>();
356 String group = profile.getControlGroup(idx);
358 // Shelly Dimmer has an additional brightness channel
359 addChannel(thing, add, profile.isDimmer, group, CHANNEL_BRIGHTNESS);
361 if (profile.settings.dimmers != null) {
362 ShellySettingsDimmer ds = profile.settings.dimmers.get(idx);
363 addChannel(thing, add, ds.autoOn != null, group, CHANNEL_TIMER_AUTOON);
364 addChannel(thing, add, ds.autoOff != null, group, CHANNEL_TIMER_AUTOOFF);
365 ShellyShortLightStatus dss = dstatus.dimmers.get(idx);
366 addChannel(thing, add, dss != null && dss.hasTimer != null, group, CHANNEL_TIMER_ACTIVE);
371 public static Map<String, Channel> createLightChannels(final Thing thing, final ShellyDeviceProfile profile,
372 final ShellyStatusLightChannel status, int idx) {
373 Map<String, Channel> add = new LinkedHashMap<>();
374 String group = profile.getControlGroup(idx);
376 if (profile.settings.lights != null) {
377 ShellySettingsRgbwLight light = profile.settings.lights.get(idx);
378 String whiteGroup = profile.isRGBW2 ? group : CHANNEL_GROUP_WHITE_CONTROL;
379 // Create power channel in color mode and brightness channel in white mode
380 addChannel(thing, add, profile.inColor, group, CHANNEL_LIGHT_POWER);
381 addChannel(thing, add, light.autoOn != null, group, CHANNEL_TIMER_AUTOON);
382 addChannel(thing, add, light.autoOff != null, group, CHANNEL_TIMER_AUTOOFF);
383 addChannel(thing, add, status.hasTimer != null, group, CHANNEL_TIMER_ACTIVE);
384 addChannel(thing, add, light.brightness != null, whiteGroup, CHANNEL_BRIGHTNESS);
385 addChannel(thing, add, light.temp != null, whiteGroup, CHANNEL_COLOR_TEMP);
391 public static Map<String, Channel> createInputChannels(final Thing thing, final ShellyDeviceProfile profile,
392 final ShellySettingsStatus status) {
393 Map<String, Channel> add = new LinkedHashMap<>();
394 if (status.inputs != null) {
395 // Create channels per input. For devices with more than 1 input (Dimmer, 1L) multiple channel sets are
396 // created by adding the index to the channel name
397 for (int i = 0; i < profile.numInputs; i++) {
398 String group = profile.getInputGroup(i);
399 String suffix = profile.getInputSuffix(i); // multi ? String.valueOf(i + 1) : "";
400 addChannel(thing, add, true, group, CHANNEL_INPUT + suffix);
401 addChannel(thing, add, true, group,
402 (!profile.isRoller ? CHANNEL_BUTTON_TRIGGER + suffix : CHANNEL_EVENT_TRIGGER));
403 if (profile.inButtonMode(i)) {
404 ShellyInputState input = status.inputs.get(i);
405 addChannel(thing, add, input.event != null, group, CHANNEL_STATUS_EVENTTYPE + suffix);
406 addChannel(thing, add, input.eventCount != null, group, CHANNEL_STATUS_EVENTCOUNT + suffix);
409 } else if (status.input != null) {
410 // old RGBW2 firmware
411 String group = profile.getInputGroup(0);
412 addChannel(thing, add, true, group, CHANNEL_INPUT);
413 addChannel(thing, add, true, group, CHANNEL_BUTTON_TRIGGER);
418 public static Map<String, Channel> createRollerChannels(Thing thing, final ShellyRollerStatus roller) {
419 Map<String, Channel> add = new LinkedHashMap<>();
420 addChannel(thing, add, true, CHGR_ROLLER, CHANNEL_ROL_CONTROL_CONTROL);
421 addChannel(thing, add, true, CHGR_ROLLER, CHANNEL_ROL_CONTROL_STATE);
422 addChannel(thing, add, true, CHGR_ROLLER, CHANNEL_EVENT_TRIGGER);
423 addChannel(thing, add, roller.currentPos != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_POS);
424 addChannel(thing, add, roller.stopReason != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_STOPR);
425 addChannel(thing, add, roller.safetySwitch != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_SAFETY);
427 ShellyThingInterface handler = (ShellyThingInterface) thing.getHandler();
428 if (handler != null) {
429 ShellySettingsGlobal settings = handler.getProfile().settings;
430 if (getBool(settings.favoritesEnabled) && settings.favorites != null) {
431 addChannel(thing, add, roller.currentPos != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_FAV);
437 public static Map<String, Channel> createMeterChannels(Thing thing, final ShellySettingsMeter meter, String group) {
438 Map<String, Channel> newChannels = new LinkedHashMap<>();
439 addChannel(thing, newChannels, meter.power != null, group, CHANNEL_METER_CURRENTWATTS);
440 addChannel(thing, newChannels, meter.total != null, group, CHANNEL_METER_TOTALKWH);
441 addChannel(thing, newChannels, meter.counters != null && meter.counters[0] != null, group,
442 CHANNEL_METER_LASTMIN1);
443 addChannel(thing, newChannels, meter.timestamp != null, group, CHANNEL_LAST_UPDATE);
447 public static Map<String, Channel> createEMeterChannels(final Thing thing, final ShellySettingsEMeter emeter,
449 Map<String, Channel> newChannels = new LinkedHashMap<>();
450 addChannel(thing, newChannels, emeter.power != null, group, CHANNEL_METER_CURRENTWATTS);
451 addChannel(thing, newChannels, emeter.total != null, group, CHANNEL_METER_TOTALKWH);
452 addChannel(thing, newChannels, emeter.totalReturned != null, group, CHANNEL_EMETER_TOTALRET);
453 addChannel(thing, newChannels, emeter.reactive != null, group, CHANNEL_EMETER_REACTWATTS);
454 addChannel(thing, newChannels, emeter.voltage != null, group, CHANNEL_EMETER_VOLTAGE);
455 addChannel(thing, newChannels, emeter.current != null, group, CHANNEL_EMETER_CURRENT);
456 addChannel(thing, newChannels, emeter.pf != null, group, CHANNEL_EMETER_PFACTOR); // EM has no PF. but power
458 addChannel(thing, newChannels, true, group, CHANNEL_LAST_UPDATE);
462 public static Map<String, Channel> createSensorChannels(final Thing thing, final ShellyDeviceProfile profile,
463 final ShellyStatusSensor sdata) {
464 Map<String, Channel> newChannels = new LinkedHashMap<>();
467 addChannel(thing, newChannels, sdata.tmp != null || sdata.thermostats != null, CHANNEL_GROUP_SENSOR,
468 CHANNEL_SENSOR_TEMP);
469 addChannel(thing, newChannels, sdata.hum != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_HUM);
470 addChannel(thing, newChannels, sdata.lux != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_LUX);
471 addChannel(thing, newChannels, sdata.lux != null && sdata.lux.illumination != null, CHANNEL_GROUP_SENSOR,
472 CHANNEL_SENSOR_ILLUM);
473 addChannel(thing, newChannels, sdata.flood != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_FLOOD);
474 addChannel(thing, newChannels, sdata.smoke != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_FLOOD);
475 addChannel(thing, newChannels, profile.settings.externalPower != null || sdata.charger != null, CHGR_DEVST,
476 CHANNEL_DEVST_CHARGER);
477 addChannel(thing, newChannels, sdata.motion != null || (sdata.sensor != null && sdata.sensor.motion != null),
478 CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_MOTION);
479 if (sdata.sensor != null) { // DW, Sense or Motion
480 addChannel(thing, newChannels, sdata.sensor.state != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_STATE); // DW/DW2
481 addChannel(thing, newChannels, sdata.sensor.motionActive != null, CHANNEL_GROUP_SENSOR, // Motion
482 CHANNEL_SENSOR_MOTION_ACT);
483 addChannel(thing, newChannels, sdata.sensor.motionTimestamp != null, CHANNEL_GROUP_SENSOR, // Motion
484 CHANNEL_SENSOR_MOTION_TS);
485 addChannel(thing, newChannels, sdata.sensor.vibration != null, CHANNEL_GROUP_SENSOR,
486 CHANNEL_SENSOR_VIBRATION);
488 if (sdata.accel != null) { // DW2
489 addChannel(thing, newChannels, sdata.accel.tilt != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_TILT);
493 if (sdata.gasSensor != null) {
494 addChannel(thing, newChannels, sdata.gasSensor.selfTestState != null, CHGR_DEVST, CHANNEL_DEVST_SELFTTEST);
495 addChannel(thing, newChannels, sdata.gasSensor.sensorState != null, CHANNEL_GROUP_SENSOR,
496 CHANNEL_SENSOR_SSTATE);
497 addChannel(thing, newChannels, sdata.concentration != null && sdata.concentration.ppm != null,
498 CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_PPM);
499 addChannel(thing, newChannels, sdata.valves != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_VALVE);
500 addChannel(thing, newChannels, sdata.gasSensor.sensorState != null, CHANNEL_GROUP_SENSOR,
501 CHANNEL_SENSOR_ALARM_STATE);
505 addChannel(thing, newChannels, profile.isSense, CHANNEL_GROUP_SENSOR, CHANNEL_SENSE_KEY);
508 addChannel(thing, newChannels, sdata.adcs != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_VOLTAGE);
512 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_SETTEMP);
513 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_BCONTROL);
514 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_BTIMER);
515 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_POSITION);
516 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_MODE);
517 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_PROFILE);
518 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_SCHEDULE);
519 addChannel(thing, newChannels, true, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_STATE);
523 if (sdata.bat != null) {
524 addChannel(thing, newChannels, sdata.bat.value != null, CHANNEL_GROUP_BATTERY, CHANNEL_SENSOR_BAT_LEVEL);
525 addChannel(thing, newChannels, sdata.bat.value != null, CHANNEL_GROUP_BATTERY, CHANNEL_SENSOR_BAT_LOW);
528 addChannel(thing, newChannels, sdata.sensorError != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_ERROR);
529 addChannel(thing, newChannels, sdata.actReasons != null, CHGR_DEVST, CHANNEL_DEVST_WAKEUP);
530 addChannel(thing, newChannels, true, profile.isButton ? CHANNEL_GROUP_STATUS : CHANNEL_GROUP_SENSOR,
531 CHANNEL_LAST_UPDATE);
535 public ChannelTypeUID getChannelTypeUID(String channelId) {
536 ShellyChannel channelDef = getDefinition(channelId);
537 return new ChannelTypeUID(BINDING_ID, channelDef.typeId);
540 private static void addChannel(Thing thing, Map<String, Channel> newChannels, boolean supported, String group,
541 String channelName) throws IllegalArgumentException {
543 String channelId = group + "#" + channelName;
544 ChannelUID channelUID = new ChannelUID(thing.getUID(), channelId);
545 ShellyChannel channelDef = getDefinition(channelId);
546 if (channelDef != null) {
547 ChannelTypeUID channelTypeUID = channelDef.typeId.contains("system:")
548 ? new ChannelTypeUID(channelDef.typeId)
549 : new ChannelTypeUID(BINDING_ID, channelDef.typeId);
550 ChannelBuilder builder;
551 if (channelDef.typeId.equalsIgnoreCase("system:button")) {
552 builder = ChannelBuilder.create(channelUID, null).withKind(ChannelKind.TRIGGER);
554 builder = ChannelBuilder.create(channelUID, channelDef.itemType);
556 if (!channelDef.label.isEmpty()) {
557 char grseq = lastChar(group);
558 char chseq = lastChar(channelName);
559 char sequence = isDigit(chseq) ? chseq : grseq;
560 String label = !isDigit(sequence) ? channelDef.label : channelDef.label + " " + sequence;
561 builder.withLabel(label);
563 if (!channelDef.description.isEmpty()) {
564 builder.withDescription(channelDef.description);
566 newChannels.put(channelId, builder.withType(channelTypeUID).build());
571 public List<StateOption> getStateOptions(ChannelTypeUID uid) {
572 List<StateOption> options = new ArrayList<>();
573 for (OptionEntry oe : stateOptions) {
574 if (oe.uid.equals(uid)) {
575 options.add(new StateOption(oe.key, oe.value));
581 public void addStateOption(String channelId, String key, String value) {
582 ChannelTypeUID uid = getChannelTypeUID(channelId);
583 stateOptions.addIfAbsent(new OptionEntry(uid, key, value));
586 public void clearStateOptions(String channelId) {
587 ChannelTypeUID uid = getChannelTypeUID(channelId);
588 for (OptionEntry oe : stateOptions) {
589 if (oe.uid.equals(uid)) {
590 stateOptions.remove(oe);
595 public class ShellyChannel {
596 private final ShellyTranslationProvider messages;
597 public String group = "";
598 public String groupLabel = "";
599 public String groupDescription = "";
601 public String channel = "";
602 public String label = "";
603 public String description = "";
604 public String itemType = "";
605 public String typeId = "";
606 public String category = "";
607 public Set<String> tags = new HashSet<>();
608 public @Nullable Unit<?> unit;
609 public Optional<Integer> min = Optional.empty();
610 public Optional<Integer> max = Optional.empty();
611 public Optional<Integer> step = Optional.empty();
612 public Optional<String> pattern = Optional.empty();
614 public ShellyChannel(ShellyTranslationProvider messages, String group, String channel, String typeId,
615 String itemType, String... category) {
616 this.messages = messages;
618 this.channel = channel;
619 this.itemType = itemType;
620 this.typeId = typeId;
622 groupLabel = getText(PREFIX_GROUP + group + ".label");
623 if (groupLabel.contains(PREFIX_GROUP)) {
626 groupDescription = getText(PREFIX_GROUP + group + ".description");
627 if (groupDescription.contains(PREFIX_GROUP)) {
628 groupDescription = "";
630 label = getText(PREFIX_CHANNEL + typeId.replace(':', '.') + ".label");
631 if (label.contains(PREFIX_CHANNEL)) {
634 description = getText(PREFIX_CHANNEL + typeId + ".description");
635 if (description.contains(PREFIX_CHANNEL)) {
640 public String getChanneId() {
641 return group + "#" + channel;
644 public String getGroupLabel() {
645 return getGroupAttribute("group");
648 public String getGroupDescription() {
649 return getGroupAttribute("group");
652 public String getLabel() {
653 return getChannelAttribute("label");
656 public String getDescription() {
657 return getChannelAttribute("description");
660 public boolean getAdvanced() {
661 String attr = getChannelAttribute("advanced");
662 return attr.isEmpty() ? false : Boolean.valueOf(attr);
665 public boolean getReadyOnly() {
666 String attr = getChannelAttribute("readOnly");
667 return attr.isEmpty() ? false : Boolean.valueOf(attr);
670 public String getCategory() {
671 return getChannelAttribute("category");
674 public String getMin() {
675 return getChannelAttribute("min");
678 public String getMax() {
679 return getChannelAttribute("max");
682 public String getStep() {
683 return getChannelAttribute("step");
686 public String getPattern() {
687 return getChannelAttribute("pattern");
690 public String getGroupAttribute(String attribute) {
691 String key = PREFIX_GROUP + group + "." + attribute;
692 String value = messages.getText(key);
693 return !value.equals(key) ? value : "";
696 public String getChannelAttribute(String attribute) {
697 String key = PREFIX_CHANNEL + channel + "." + attribute;
698 String value = messages.getText(key);
699 return !value.equals(key) ? value : "";
702 private String getText(String key) {
703 return messages.get(key);
707 public static class ChannelMap {
708 private final Map<String, ShellyChannel> map = new HashMap<>();
710 private ChannelMap add(ShellyChannel def) {
711 map.put(def.getChanneId(), def);
715 public ShellyChannel get(String channelName) throws IllegalArgumentException {
716 ShellyChannel def = null;
717 if (channelName.contains("#")) {
718 def = map.get(channelName);
723 for (HashMap.Entry<String, ShellyChannel> entry : map.entrySet()) {
724 if (entry.getValue().channel.contains("#" + channelName)) {
725 def = entry.getValue();
731 throw new IllegalArgumentException("Channel definition for " + channelName + " not found!");