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.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.ShellyShortLightStatus;
39 import org.openhab.binding.shelly.internal.api.ShellyApiJsonDTO.ShellyShortStatusRelay;
40 import org.openhab.binding.shelly.internal.api.ShellyApiJsonDTO.ShellyStatusLightChannel;
41 import org.openhab.binding.shelly.internal.api.ShellyApiJsonDTO.ShellyStatusRelay;
42 import org.openhab.binding.shelly.internal.api.ShellyApiJsonDTO.ShellyStatusSensor;
43 import org.openhab.binding.shelly.internal.api.ShellyDeviceProfile;
44 import org.openhab.binding.shelly.internal.handler.ShellyBaseHandler;
45 import org.openhab.core.thing.Channel;
46 import org.openhab.core.thing.ChannelUID;
47 import org.openhab.core.thing.Thing;
48 import org.openhab.core.thing.binding.builder.ChannelBuilder;
49 import org.openhab.core.thing.type.ChannelKind;
50 import org.openhab.core.thing.type.ChannelTypeUID;
51 import org.osgi.service.component.annotations.Activate;
52 import org.osgi.service.component.annotations.Component;
53 import org.osgi.service.component.annotations.Reference;
56 * The {@link ShellyCHANNEL_DEFINITIONSDTO} defines channel information for dynamically created channels. Those will be
57 * added on the first thing status update
59 * @author Markus Michels - Initial contribution
62 @Component(service = ShellyChannelDefinitions.class)
63 public class ShellyChannelDefinitions {
65 public static final String ITEMT_STRING = "String";
66 public static final String ITEMT_NUMBER = "Number";
67 public static final String ITEMT_SWITCH = "Switch";
68 public static final String ITEMT_CONTACT = "Contact";
69 public static final String ITEMT_ROLLER = "Rollershutter";
70 public static final String ITEMT_DIMMER = "Dimmer";
71 public static final String ITEMT_LOCATION = "Location";
72 public static final String ITEMT_DATETIME = "DateTime";
73 public static final String ITEMT_TEMP = "Number:Temperature";
74 public static final String ITEMT_LUX = "Number:Illuminance";
75 public static final String ITEMT_POWER = "Number:Power";
76 public static final String ITEMT_ENERGY = "Number:Energy";
77 public static final String ITEMT_VOLT = "Number:ElectricPotential";
78 public static final String ITEMT_AMP = "Number:ElectricPotential";
79 public static final String ITEMT_ANGLE = "Number:Angle";
80 public static final String ITEMT_DISTANCE = "Number:Length";
81 public static final String ITEMT_SPEED = "Number:Speed";
82 public static final String ITEMT_VOLUME = "Number:Volume";
83 public static final String ITEMT_TIME = "Number:Time";
84 public static final String ITEMT_PERCENT = "Number:Dimensionless";
86 // shortcuts to avoid line breaks (make code more readable)
87 private static final String CHGR_DEVST = CHANNEL_GROUP_DEV_STATUS;
88 private static final String CHGR_RELAY = CHANNEL_GROUP_RELAY_CONTROL;
89 private static final String CHGR_ROLLER = CHANNEL_GROUP_ROL_CONTROL;
90 private static final String CHGR_LIGHT = CHANNEL_GROUP_LIGHT_CONTROL;
91 private static final String CHGR_STATUS = CHANNEL_GROUP_STATUS;
92 private static final String CHGR_METER = CHANNEL_GROUP_METER;
93 private static final String CHGR_SENSOR = CHANNEL_GROUP_SENSOR;
94 private static final String CHGR_CONTROL = CHANNEL_GROUP_CONTROL;
95 private static final String CHGR_BAT = CHANNEL_GROUP_BATTERY;
97 public static final String PREFIX_GROUP = "group-type." + BINDING_ID + ".";
98 public static final String PREFIX_CHANNEL = "channel-type." + BINDING_ID + ".";
100 private static final ChannelMap CHANNEL_DEFINITIONS = new ChannelMap();
103 public ShellyChannelDefinitions(@Reference ShellyTranslationProvider translationProvider) {
104 ShellyTranslationProvider m = translationProvider;
109 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_NAME, "deviceName", ITEMT_STRING))
110 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_ITEMP, "deviceTemp", ITEMT_TEMP))
111 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_WAKEUP, "sensorWakeup", ITEMT_STRING))
112 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_ACCUWATTS, "meterAccuWatts", ITEMT_POWER))
113 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_ACCUTOTAL, "meterAccuTotal", ITEMT_POWER))
114 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_ACCURETURNED, "meterAccuReturned", ITEMT_POWER))
115 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_VOLTAGE, "supplyVoltage", ITEMT_VOLT))
116 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_CHARGER, "charger", ITEMT_SWITCH))
117 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_LED_STATUS_DISABLE, "ledStatusDisable", ITEMT_SWITCH))
118 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_LED_POWER_DISABLE, "ledPowerDisable", ITEMT_SWITCH))
119 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_SELFTTEST, "selfTest", ITEMT_STRING))
120 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_UPTIME, "uptime", ITEMT_NUMBER))
121 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_HEARTBEAT, "heartBeat", ITEMT_DATETIME))
122 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_UPDATE, "updateAvailable", ITEMT_SWITCH))
123 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_CALIBRATED, "calibrated", ITEMT_SWITCH))
124 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_SCHEDULE, "deviceSchedule", ITEMT_SWITCH))
127 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_OUTPUT_NAME, "outputName", ITEMT_STRING))
128 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_OUTPUT, "system:power", ITEMT_SWITCH))
129 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_INPUT, "inputState", ITEMT_SWITCH))
130 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_BUTTON_TRIGGER, "system:button", ITEMT_STRING))
131 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_STATUS_EVENTTYPE, "lastEvent", ITEMT_STRING))
132 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_STATUS_EVENTCOUNT, "eventCount", ITEMT_NUMBER))
133 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_TIMER_AUTOON, "timerAutoOn", ITEMT_TIME))
134 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_TIMER_AUTOOFF, "timerAutoOff", ITEMT_TIME))
135 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_TIMER_ACTIVE, "timerActive", ITEMT_SWITCH))
138 .add(new ShellyChannel(m, CHANNEL_GROUP_DIMMER_CONTROL, CHANNEL_BRIGHTNESS, "dimmerBrightness",
142 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_CONTROL, "rollerShutter", ITEMT_ROLLER))
143 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_POS, "rollerPosition", ITEMT_DIMMER))
144 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_FAV, "rollerFavorite", ITEMT_NUMBER))
145 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_STATE, "rollerState", ITEMT_STRING))
146 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_STOPR, "rollerStop", ITEMT_STRING))
147 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_SAFETY, "rollerSafety", ITEMT_SWITCH))
148 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_INPUT, "inputState", ITEMT_SWITCH))
149 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_STATUS_EVENTTYPE, "lastEvent", ITEMT_STRING))
150 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_STATUS_EVENTCOUNT, "eventCount", ITEMT_NUMBER))
151 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_EVENT_TRIGGER, "system:button", "system:button"))
154 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_LIGHT_POWER, "system:power", ITEMT_SWITCH))
155 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_INPUT, "inputState", ITEMT_SWITCH))
156 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_BUTTON_TRIGGER, "system:button", ITEMT_STRING))
157 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_STATUS_EVENTTYPE, "lastEvent", ITEMT_STRING))
158 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_STATUS_EVENTCOUNT, "eventCount", ITEMT_NUMBER))
159 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_TIMER_AUTOON, "timerAutoOn", ITEMT_TIME))
160 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_TIMER_AUTOOFF, "timerAutoOff", ITEMT_TIME))
161 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_TIMER_ACTIVE, "timerActive", ITEMT_SWITCH))
164 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_METER_CURRENTWATTS, "meterWatts", ITEMT_POWER))
165 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_METER_TOTALKWH, "meterTotal", ITEMT_ENERGY))
166 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_METER_LASTMIN1, "lastPower1", ITEMT_ENERGY))
167 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_LAST_UPDATE, "lastUpdate", ITEMT_DATETIME))
170 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_TOTALRET, "meterReturned", ITEMT_ENERGY))
171 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_REACTWATTS, "meterReactive", ITEMT_POWER))
172 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_VOLTAGE, "meterVoltage", ITEMT_VOLT))
173 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_CURRENT, "meterCurrent", ITEMT_AMP))
174 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_PFACTOR, "meterPowerFactor", ITEMT_PERCENT))
177 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_TEMP, "sensorTemp", ITEMT_TEMP))
178 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_HUM, "sensorHumidity", ITEMT_PERCENT))
179 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_LUX, "sensorLux", ITEMT_LUX))
180 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_ILLUM, "sensorIllumination", ITEMT_STRING))
181 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_VOLTAGE, "sensorADC", ITEMT_VOLT))
182 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_STATE, "sensorContact", ITEMT_CONTACT))
183 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_SSTATE, "sensorState", ITEMT_STRING))
184 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_TILT, "sensorTilt", ITEMT_ANGLE))
185 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_MOTION, "sensorMotion", ITEMT_SWITCH))
186 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_MOTION_TS, "motionTimestamp", ITEMT_DATETIME))
187 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_MOTION_ACT, "motionActive", ITEMT_SWITCH))
188 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_VIBRATION, "vibration", ITEMT_SWITCH))
189 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_FLOOD, "sensorFlood", ITEMT_SWITCH))
190 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_SMOKE, "sensorSmoke", ITEMT_SWITCH))
191 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_PPM, "sensorPPM", ITEMT_NUMBER))
192 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_VALVE, "sensorValve", ITEMT_STRING))
193 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_ALARM_STATE, "alarmState", ITEMT_STRING))
194 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_ERROR, "sensorError", ITEMT_STRING))
195 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_LAST_UPDATE, "lastUpdate", ITEMT_DATETIME))
196 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_SLEEPTIME, "sensorSleepTime", ITEMT_NUMBER))
197 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSE_KEY, "senseKey", ITEMT_STRING)) // Sense
200 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_INPUT, "inputState", ITEMT_SWITCH))
201 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_STATUS_EVENTTYPE, "lastEvent", ITEMT_STRING))
202 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_STATUS_EVENTCOUNT, "eventCount", ITEMT_NUMBER))
203 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_BUTTON_TRIGGER, "system:button", ITEMT_STRING))
204 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_LAST_UPDATE, "lastUpdate", ITEMT_DATETIME))
206 // Addon with external sensors
207 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENDOR_TEMP1, "sensorExtTemp", ITEMT_TEMP))
208 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENDOR_TEMP2, "sensorExtTemp", ITEMT_TEMP))
209 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENDOR_TEMP3, "sensorExtTemp", ITEMT_TEMP))
210 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENDOR_HUMIDITY, "sensorExtHum", ITEMT_PERCENT))
213 .add(new ShellyChannel(m, CHGR_BAT, CHANNEL_SENSOR_BAT_LEVEL, "system:battery-level", ITEMT_PERCENT))
214 .add(new ShellyChannel(m, CHGR_BAT, CHANNEL_SENSOR_BAT_LOW, "system:low-battery", ITEMT_SWITCH))
217 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_POSITION, "sensorPosition", ITEMT_DIMMER))
218 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_MODE, "controlMode", ITEMT_STRING))
219 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_PROFILE, "controlProfile", ITEMT_NUMBER))
220 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_SETTEMP, "targetTemp", ITEMT_TEMP))
221 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_BCONTROL, "boostControl", ITEMT_SWITCH))
222 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_BTIMER, "boostTimer", ITEMT_TIME));
225 public static @Nullable ShellyChannel getDefinition(String channelName) throws IllegalArgumentException {
226 String group = substringBefore(channelName, "#");
227 String channel = substringAfter(channelName, "#");
229 if (group.contains(CHANNEL_GROUP_METER)) {
230 group = CHANNEL_GROUP_METER; // map meter1..n to meter
231 } else if (group.contains(CHANNEL_GROUP_RELAY_CONTROL)) {
232 group = CHANNEL_GROUP_RELAY_CONTROL; // map meter1..n to meter
233 } else if (group.contains(CHANNEL_GROUP_LIGHT_CHANNEL)) {
234 group = CHANNEL_GROUP_LIGHT_CHANNEL;
235 } else if (group.contains(CHANNEL_GROUP_STATUS)) {
236 group = CHANNEL_GROUP_STATUS; // map status1..n to meter
239 if (channel.startsWith(CHANNEL_INPUT)) {
240 channel = CHANNEL_INPUT;
241 } else if (channel.startsWith(CHANNEL_BUTTON_TRIGGER)) {
242 channel = CHANNEL_BUTTON_TRIGGER;
243 } else if (channel.startsWith(CHANNEL_STATUS_EVENTTYPE)) {
244 channel = CHANNEL_STATUS_EVENTTYPE;
245 } else if (channel.startsWith(CHANNEL_STATUS_EVENTCOUNT)) {
246 channel = CHANNEL_STATUS_EVENTCOUNT;
249 String channelId = group + "#" + channel;
250 return CHANNEL_DEFINITIONS.get(channelId);
254 * Auto-create relay channels depending on relay type/mode
256 * @return ArrayList<Channel> of channels to be added to the thing
258 public static Map<String, Channel> createDeviceChannels(final Thing thing, final ShellyDeviceProfile profile,
259 final ShellySettingsStatus status) {
260 Map<String, Channel> add = new LinkedHashMap<>();
262 addChannel(thing, add, profile.settings.name != null, CHGR_DEVST, CHANNEL_DEVST_NAME);
264 if (!profile.isSensor) {
265 // Only some devices report the internal device temp
266 addChannel(thing, add, (status.tmp != null) || (status.temperature != null), CHGR_DEVST,
267 CHANNEL_DEVST_ITEMP);
269 addChannel(thing, add, profile.settings.sleepTime != null, CHGR_SENSOR, CHANNEL_SENSOR_SLEEPTIME);
271 // If device has more than 1 meter the channel accumulatedWatts receives the accumulated value
272 boolean accuChannel = !profile.isRoller && !profile.isRGBW2
273 && (((status.meters != null) && (status.meters.size() > 1))
274 || ((status.emeters != null && status.emeters.size() > 1)));
275 addChannel(thing, add, accuChannel, CHGR_DEVST, CHANNEL_DEVST_ACCUWATTS);
276 addChannel(thing, add, accuChannel, CHGR_DEVST, CHANNEL_DEVST_ACCUTOTAL);
277 addChannel(thing, add, accuChannel && (status.emeters != null), CHGR_DEVST, CHANNEL_DEVST_ACCURETURNED);
278 addChannel(thing, add,
279 !profile.isRoller && !profile.isRGBW2
280 && (status.voltage != null || profile.settings.supplyVoltage != null),
281 CHGR_DEVST, CHANNEL_DEVST_VOLTAGE);
282 addChannel(thing, add, true, CHGR_DEVST, CHANNEL_DEVST_UPDATE);
283 addChannel(thing, add, true, CHGR_DEVST, CHANNEL_DEVST_UPTIME);
284 addChannel(thing, add, true, CHGR_DEVST, CHANNEL_DEVST_HEARTBEAT);
285 addChannel(thing, add, profile.settings.ledPowerDisable != null, CHGR_DEVST, CHANNEL_LED_POWER_DISABLE);
286 addChannel(thing, add, profile.settings.ledStatusDisable != null, CHGR_DEVST, CHANNEL_LED_STATUS_DISABLE); // WiFi
287 addChannel(thing, add, profile.settings.calibrated != null, CHGR_DEVST, CHANNEL_DEVST_CALIBRATED);
293 * Auto-create relay channels depending on relay type/mode
295 * @return ArrayList<Channel> of channels to be added to the thing
297 public static Map<String, Channel> createRelayChannels(final Thing thing, final ShellyDeviceProfile profile,
298 final ShellyStatusRelay relay, int idx) {
299 Map<String, Channel> add = new LinkedHashMap<>();
300 String group = profile.getControlGroup(idx);
302 ShellySettingsRelay rs = profile.settings.relays.get(idx);
303 ShellyShortStatusRelay rstatus = relay.relays.get(idx);
304 boolean timer = rs.hasTimer != null || (rstatus != null && rstatus.hasTimer != null); // Dimmer 1/2 have
305 // has_timer under /status
306 addChannel(thing, add, rs.ison != null, group, CHANNEL_OUTPUT);
307 addChannel(thing, add, rs.name != null, group, CHANNEL_OUTPUT_NAME);
308 addChannel(thing, add, rs.autoOn != null, group, CHANNEL_TIMER_AUTOON);
309 addChannel(thing, add, rs.autoOff != null, group, CHANNEL_TIMER_AUTOOFF);
310 addChannel(thing, add, timer, group, CHANNEL_TIMER_ACTIVE);
312 // Shelly 1/1PM Addon
313 if (relay.extTemperature != null) {
314 addChannel(thing, add, relay.extTemperature.sensor1 != null, CHGR_SENSOR, CHANNEL_ESENDOR_TEMP1);
315 addChannel(thing, add, relay.extTemperature.sensor2 != null, CHGR_SENSOR, CHANNEL_ESENDOR_TEMP2);
316 addChannel(thing, add, relay.extTemperature.sensor3 != null, CHGR_SENSOR, CHANNEL_ESENDOR_TEMP3);
318 if (relay.extHumidity != null) {
319 addChannel(thing, add, relay.extHumidity.sensor1 != null, CHGR_SENSOR, CHANNEL_ESENDOR_HUMIDITY);
325 public static Map<String, Channel> createDimmerChannels(final Thing thing, final ShellyDeviceProfile profile,
326 final ShellySettingsStatus dstatus, int idx) {
327 Map<String, Channel> add = new LinkedHashMap<>();
328 String group = profile.getControlGroup(idx);
330 // Shelly Dimmer has an additional brightness channel
331 addChannel(thing, add, profile.isDimmer, group, CHANNEL_BRIGHTNESS);
333 ShellySettingsDimmer ds = profile.settings.dimmers.get(idx);
334 addChannel(thing, add, ds.autoOn != null, group, CHANNEL_TIMER_AUTOON);
335 addChannel(thing, add, ds.autoOff != null, group, CHANNEL_TIMER_AUTOOFF);
337 ShellyShortLightStatus dss = dstatus.dimmers.get(idx);
338 addChannel(thing, add, dss != null && dss.hasTimer != null, group, CHANNEL_TIMER_ACTIVE);
342 public static Map<String, Channel> createLightChannels(final Thing thing, final ShellyDeviceProfile profile,
343 final ShellyStatusLightChannel status, int idx) {
344 Map<String, Channel> add = new LinkedHashMap<>();
345 String group = profile.getControlGroup(idx);
347 ShellySettingsRgbwLight light = profile.settings.lights.get(idx);
348 // The is no brightness channel in color mode, so we need a power channel
349 addChannel(thing, add, profile.inColor, group, CHANNEL_LIGHT_POWER);
351 addChannel(thing, add, light.autoOn != null, group, CHANNEL_TIMER_AUTOON);
352 addChannel(thing, add, light.autoOff != null, group, CHANNEL_TIMER_AUTOOFF);
353 addChannel(thing, add, status.hasTimer != null, group, CHANNEL_TIMER_ACTIVE);
357 public static Map<String, Channel> createInputChannels(final Thing thing, final ShellyDeviceProfile profile,
358 final ShellySettingsStatus status, String group) {
359 Map<String, Channel> add = new LinkedHashMap<>();
360 if (status.inputs != null) {
361 // Create channels per input. For devices with more than 1 input (Dimmer, 1L) multiple channel sets are
362 // created by adding the index to the channel name
363 boolean multi = ((profile.numRelays == 1) || profile.isDimmer || profile.isRoller)
364 && (profile.numInputs >= 2);
365 for (int i = 0; i < profile.numInputs; i++) {
366 String suffix = multi ? String.valueOf(i + 1) : "";
367 ShellyInputState input = status.inputs.get(i);
368 addChannel(thing, add, true, group, CHANNEL_INPUT + suffix);
369 if (profile.inButtonMode(i)) {
370 addChannel(thing, add, input.event != null, group, CHANNEL_STATUS_EVENTTYPE + suffix);
371 addChannel(thing, add, input.eventCount != null, group, CHANNEL_STATUS_EVENTCOUNT + suffix);
373 addChannel(thing, add, true, group,
374 (!profile.isRoller ? CHANNEL_BUTTON_TRIGGER + suffix : CHANNEL_EVENT_TRIGGER));
376 } else if (status.input != null) {
377 // old RGBW2 firmware
378 addChannel(thing, add, true, group, CHANNEL_INPUT);
379 addChannel(thing, add, true, group, CHANNEL_BUTTON_TRIGGER);
384 public static Map<String, Channel> createRollerChannels(Thing thing, final ShellyControlRoller roller) {
385 Map<String, Channel> add = new LinkedHashMap<>();
386 addChannel(thing, add, true, CHGR_ROLLER, CHANNEL_ROL_CONTROL_CONTROL);
387 addChannel(thing, add, true, CHGR_ROLLER, CHANNEL_ROL_CONTROL_STATE);
388 addChannel(thing, add, true, CHGR_ROLLER, CHANNEL_EVENT_TRIGGER);
389 addChannel(thing, add, roller.currentPos != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_POS);
390 addChannel(thing, add, roller.stopReason != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_STOPR);
391 addChannel(thing, add, roller.safetySwitch != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_SAFETY);
393 ShellyBaseHandler handler = (ShellyBaseHandler) thing.getHandler();
394 if (handler != null) {
395 ShellySettingsGlobal settings = handler.getProfile().settings;
396 if (getBool(settings.favoritesEnabled) && (settings.favorites != null)) {
397 addChannel(thing, add, roller.currentPos != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_FAV);
403 public static Map<String, Channel> createMeterChannels(Thing thing, final ShellySettingsMeter meter, String group) {
404 Map<String, Channel> newChannels = new LinkedHashMap<>();
405 addChannel(thing, newChannels, meter.power != null, group, CHANNEL_METER_CURRENTWATTS);
406 addChannel(thing, newChannels, meter.total != null, group, CHANNEL_METER_TOTALKWH);
407 addChannel(thing, newChannels, (meter.counters != null) && (meter.counters[0] != null), group,
408 CHANNEL_METER_LASTMIN1);
409 addChannel(thing, newChannels, meter.timestamp != null, group, CHANNEL_LAST_UPDATE);
413 public static Map<String, Channel> createEMeterChannels(final Thing thing, final ShellySettingsEMeter emeter,
415 Map<String, Channel> newChannels = new LinkedHashMap<>();
416 addChannel(thing, newChannels, emeter.power != null, group, CHANNEL_METER_CURRENTWATTS);
417 addChannel(thing, newChannels, emeter.total != null, group, CHANNEL_METER_TOTALKWH);
418 addChannel(thing, newChannels, emeter.totalReturned != null, group, CHANNEL_EMETER_TOTALRET);
419 addChannel(thing, newChannels, emeter.reactive != null, group, CHANNEL_EMETER_REACTWATTS);
420 addChannel(thing, newChannels, emeter.voltage != null, group, CHANNEL_EMETER_VOLTAGE);
421 addChannel(thing, newChannels, emeter.current != null, group, CHANNEL_EMETER_CURRENT);
422 addChannel(thing, newChannels, emeter.power != null, group, CHANNEL_EMETER_PFACTOR); // EM has no PF. but power
424 addChannel(thing, newChannels, true, group, CHANNEL_LAST_UPDATE);
428 public static Map<String, Channel> createSensorChannels(final Thing thing, final ShellyDeviceProfile profile,
429 final ShellyStatusSensor sdata) {
430 Map<String, Channel> newChannels = new LinkedHashMap<>();
433 addChannel(thing, newChannels, sdata.tmp != null || sdata.thermostats != null, CHANNEL_GROUP_SENSOR,
434 CHANNEL_SENSOR_TEMP);
435 addChannel(thing, newChannels, sdata.hum != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_HUM);
436 addChannel(thing, newChannels, sdata.lux != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_LUX);
437 addChannel(thing, newChannels, sdata.lux != null && sdata.lux.illumination != null, CHANNEL_GROUP_SENSOR,
438 CHANNEL_SENSOR_ILLUM);
439 addChannel(thing, newChannels, sdata.flood != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_FLOOD);
440 addChannel(thing, newChannels, sdata.smoke != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_FLOOD);
441 addChannel(thing, newChannels, (profile.settings.externalPower != null) || (sdata.charger != null), CHGR_DEVST,
442 CHANNEL_DEVST_CHARGER);
443 addChannel(thing, newChannels,
444 sdata.motion != null || ((sdata.sensor != null) && (sdata.sensor.motion != null)), CHANNEL_GROUP_SENSOR,
445 CHANNEL_SENSOR_MOTION);
446 if (sdata.sensor != null) { // DW, Sense or Motion
447 addChannel(thing, newChannels, sdata.sensor.state != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_STATE); // DW/DW2
448 addChannel(thing, newChannels, sdata.sensor.motionActive != null, CHANNEL_GROUP_SENSOR, // Motion
449 CHANNEL_SENSOR_MOTION_ACT);
450 addChannel(thing, newChannels, sdata.sensor.motionTimestamp != null, CHANNEL_GROUP_SENSOR, // Motion
451 CHANNEL_SENSOR_MOTION_TS);
452 addChannel(thing, newChannels, sdata.sensor.vibration != null, CHANNEL_GROUP_SENSOR,
453 CHANNEL_SENSOR_VIBRATION);
455 if (sdata.accel != null) { // DW2
456 addChannel(thing, newChannels, sdata.accel.tilt != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_TILT);
460 if (sdata.gasSensor != null) {
461 addChannel(thing, newChannels, sdata.gasSensor.selfTestState != null, CHGR_DEVST, CHANNEL_DEVST_SELFTTEST);
462 addChannel(thing, newChannels, sdata.gasSensor.sensorState != null, CHANNEL_GROUP_SENSOR,
463 CHANNEL_SENSOR_SSTATE);
464 addChannel(thing, newChannels, sdata.concentration != null && sdata.concentration.ppm != null,
465 CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_PPM);
466 addChannel(thing, newChannels, sdata.valves != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_VALVE);
467 addChannel(thing, newChannels, sdata.gasSensor.sensorState != null, CHANNEL_GROUP_SENSOR,
468 CHANNEL_SENSOR_ALARM_STATE);
472 addChannel(thing, newChannels, profile.isSense, CHANNEL_GROUP_SENSOR, CHANNEL_SENSE_KEY);
475 addChannel(thing, newChannels, sdata.adcs != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_VOLTAGE);
479 addChannel(thing, newChannels, true, CHANNEL_GROUP_DEV_STATUS, CHANNEL_DEVST_SCHEDULE);
480 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_SETTEMP);
481 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_BCONTROL);
482 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_BTIMER);
483 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_POSITION);
484 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_MODE);
485 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_PROFILE);
486 addChannel(thing, newChannels, true, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_STATE); // TRV
490 if (sdata.bat != null) {
491 addChannel(thing, newChannels, sdata.bat.value != null, CHANNEL_GROUP_BATTERY, CHANNEL_SENSOR_BAT_LEVEL);
492 addChannel(thing, newChannels, sdata.bat.value != null, CHANNEL_GROUP_BATTERY, CHANNEL_SENSOR_BAT_LOW);
495 addChannel(thing, newChannels, sdata.sensorError != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_ERROR);
496 addChannel(thing, newChannels, sdata.actReasons != null, CHGR_DEVST, CHANNEL_DEVST_WAKEUP);
497 addChannel(thing, newChannels, true, profile.isButton ? CHANNEL_GROUP_STATUS : CHANNEL_GROUP_SENSOR,
498 CHANNEL_LAST_UPDATE);
502 private static void addChannel(Thing thing, Map<String, Channel> newChannels, boolean supported, String group,
503 String channelName) throws IllegalArgumentException {
505 String channelId = group + "#" + channelName;
506 ChannelUID channelUID = new ChannelUID(thing.getUID(), channelId);
507 ShellyChannel channelDef = getDefinition(channelId);
508 if (channelDef != null) {
509 ChannelTypeUID channelTypeUID = channelDef.typeId.contains("system:")
510 ? new ChannelTypeUID(channelDef.typeId)
511 : new ChannelTypeUID(BINDING_ID, channelDef.typeId);
513 if (channelDef.typeId.equalsIgnoreCase("system:button")) {
514 channel = ChannelBuilder.create(channelUID, null).withKind(ChannelKind.TRIGGER)
515 .withType(channelTypeUID).build();
517 channel = ChannelBuilder.create(channelUID, channelDef.itemType).withType(channelTypeUID).build();
519 newChannels.put(channelId, channel);
524 public class ShellyChannel {
525 private final ShellyTranslationProvider messages;
526 public String group = "";
527 public String groupLabel = "";
528 public String groupDescription = "";
530 public String channel = "";
531 public String label = "";
532 public String description = "";
533 public String itemType = "";
534 public String typeId = "";
535 public String category = "";
536 public Set<String> tags = new HashSet<>();
537 public @Nullable Unit<?> unit;
538 public Optional<Integer> min = Optional.empty();
539 public Optional<Integer> max = Optional.empty();
540 public Optional<Integer> step = Optional.empty();
541 public Optional<String> pattern = Optional.empty();
543 public ShellyChannel(ShellyTranslationProvider messages, String group, String channel, String typeId,
544 String itemType, String... category) {
545 this.messages = messages;
547 this.channel = channel;
548 this.itemType = itemType;
549 this.typeId = typeId;
551 groupLabel = getText(PREFIX_GROUP + group + ".label");
552 groupDescription = getText(PREFIX_GROUP + group + ".description");
553 label = getText(PREFIX_CHANNEL + channel + ".label");
554 description = getText(PREFIX_CHANNEL + channel + ".description");
557 public String getChanneId() {
558 return group + "#" + channel;
561 public String getGroupLabel() {
562 return getGroupAttribute("group");
565 public String getGroupDescription() {
566 return getGroupAttribute("group");
569 public String getLabel() {
570 return getChannelAttribute("label");
573 public String getDescription() {
574 return getChannelAttribute("description");
577 public boolean getAdvanced() {
578 String attr = getChannelAttribute("advanced");
579 return attr.isEmpty() ? false : Boolean.valueOf(attr);
582 public boolean getReadyOnly() {
583 String attr = getChannelAttribute("readOnly");
584 return attr.isEmpty() ? false : Boolean.valueOf(attr);
587 public String getCategory() {
588 return getChannelAttribute("category");
591 public String getMin() {
592 return getChannelAttribute("min");
595 public String getMax() {
596 return getChannelAttribute("max");
599 public String getStep() {
600 return getChannelAttribute("step");
603 public String getPattern() {
604 return getChannelAttribute("pattern");
607 public String getGroupAttribute(String attribute) {
608 String key = PREFIX_GROUP + group + "." + attribute;
609 String value = messages.getText(key);
610 return !value.equals(key) ? value : "";
613 public String getChannelAttribute(String attribute) {
614 String key = PREFIX_CHANNEL + channel + "." + attribute;
615 String value = messages.getText(key);
616 return !value.equals(key) ? value : "";
619 private String getText(String key) {
620 return messages.get(key);
624 public static class ChannelMap {
625 private final Map<String, ShellyChannel> map = new HashMap<>();
627 private ChannelMap add(ShellyChannel def) {
628 map.put(def.getChanneId(), def);
632 public ShellyChannel get(String channelName) throws IllegalArgumentException {
633 ShellyChannel def = null;
634 if (channelName.contains("#")) {
635 def = map.get(channelName);
640 for (HashMap.Entry<String, ShellyChannel> entry : map.entrySet()) {
641 if (entry.getValue().channel.contains("#" + channelName)) {
642 def = entry.getValue();
648 throw new IllegalArgumentException("Channel definition for " + channelName + " not found!");