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.HashMap;
20 import java.util.HashSet;
21 import java.util.LinkedHashMap;
23 import java.util.Optional;
26 import javax.measure.Unit;
28 import org.eclipse.jdt.annotation.NonNullByDefault;
29 import org.eclipse.jdt.annotation.Nullable;
30 import org.openhab.binding.shelly.internal.api.ShellyDeviceProfile;
31 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyInputState;
32 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyRollerStatus;
33 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsDimmer;
34 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsEMeter;
35 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsGlobal;
36 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsMeter;
37 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsRelay;
38 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsRgbwLight;
39 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsStatus;
40 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyShortLightStatus;
41 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyShortStatusRelay;
42 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyStatusLightChannel;
43 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyStatusRelay;
44 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyStatusSensor;
45 import org.openhab.binding.shelly.internal.handler.ShellyThingInterface;
46 import org.openhab.core.thing.Channel;
47 import org.openhab.core.thing.ChannelUID;
48 import org.openhab.core.thing.Thing;
49 import org.openhab.core.thing.binding.builder.ChannelBuilder;
50 import org.openhab.core.thing.type.ChannelKind;
51 import org.openhab.core.thing.type.ChannelTypeUID;
52 import org.osgi.service.component.annotations.Activate;
53 import org.osgi.service.component.annotations.Component;
54 import org.osgi.service.component.annotations.Reference;
57 * The {@link ShellyCHANNEL_DEFINITIONSDTO} defines channel information for dynamically created channels. Those will be
58 * added on the first thing status update
60 * @author Markus Michels - Initial contribution
63 @Component(service = ShellyChannelDefinitions.class)
64 public class ShellyChannelDefinitions {
66 public static final String ITEMT_STRING = "String";
67 public static final String ITEMT_NUMBER = "Number";
68 public static final String ITEMT_SWITCH = "Switch";
69 public static final String ITEMT_CONTACT = "Contact";
70 public static final String ITEMT_ROLLER = "Rollershutter";
71 public static final String ITEMT_DIMMER = "Dimmer";
72 public static final String ITEMT_LOCATION = "Location";
73 public static final String ITEMT_DATETIME = "DateTime";
74 public static final String ITEMT_TEMP = "Number:Temperature";
75 public static final String ITEMT_LUX = "Number:Illuminance";
76 public static final String ITEMT_POWER = "Number:Power";
77 public static final String ITEMT_ENERGY = "Number:Energy";
78 public static final String ITEMT_VOLT = "Number:ElectricPotential";
79 public static final String ITEMT_AMP = "Number:ElectricPotential";
80 public static final String ITEMT_ANGLE = "Number:Angle";
81 public static final String ITEMT_DISTANCE = "Number:Length";
82 public static final String ITEMT_SPEED = "Number:Speed";
83 public static final String ITEMT_VOLUME = "Number:Volume";
84 public static final String ITEMT_TIME = "Number:Time";
85 public static final String ITEMT_PERCENT = "Number:Dimensionless";
87 // shortcuts to avoid line breaks (make code more readable)
88 private static final String CHGR_DEVST = CHANNEL_GROUP_DEV_STATUS;
89 private static final String CHGR_RELAY = CHANNEL_GROUP_RELAY_CONTROL;
90 private static final String CHGR_ROLLER = CHANNEL_GROUP_ROL_CONTROL;
91 private static final String CHGR_LIGHT = CHANNEL_GROUP_LIGHT_CONTROL;
92 private static final String CHGR_STATUS = CHANNEL_GROUP_STATUS;
93 private static final String CHGR_METER = CHANNEL_GROUP_METER;
94 private static final String CHGR_SENSOR = CHANNEL_GROUP_SENSOR;
95 private static final String CHGR_CONTROL = CHANNEL_GROUP_CONTROL;
96 private static final String CHGR_BAT = CHANNEL_GROUP_BATTERY;
98 public static final String PREFIX_GROUP = "group-type." + BINDING_ID + ".";
99 public static final String PREFIX_CHANNEL = "channel-type." + BINDING_ID + ".";
101 private static final ChannelMap CHANNEL_DEFINITIONS = new ChannelMap();
104 public ShellyChannelDefinitions(@Reference ShellyTranslationProvider translationProvider) {
105 ShellyTranslationProvider m = translationProvider;
110 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_NAME, "deviceName", ITEMT_STRING))
111 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_ITEMP, "deviceTemp", ITEMT_TEMP))
112 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_WAKEUP, "sensorWakeup", ITEMT_STRING))
113 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_ACCUWATTS, "meterAccuWatts", ITEMT_POWER))
114 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_ACCUTOTAL, "meterAccuTotal", ITEMT_POWER))
115 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_ACCURETURNED, "meterAccuReturned", ITEMT_POWER))
116 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_VOLTAGE, "supplyVoltage", ITEMT_VOLT))
117 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_CHARGER, "charger", ITEMT_SWITCH))
118 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_LED_STATUS_DISABLE, "ledStatusDisable", ITEMT_SWITCH))
119 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_LED_POWER_DISABLE, "ledPowerDisable", ITEMT_SWITCH))
120 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_SELFTTEST, "selfTest", ITEMT_STRING))
121 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_UPTIME, "uptime", ITEMT_NUMBER))
122 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_HEARTBEAT, "heartBeat", ITEMT_DATETIME))
123 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_UPDATE, "updateAvailable", ITEMT_SWITCH))
124 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_CALIBRATED, "calibrated", ITEMT_SWITCH))
125 .add(new ShellyChannel(m, CHGR_DEVST, CHANNEL_DEVST_SCHEDULE, "deviceSchedule", ITEMT_SWITCH))
128 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_OUTPUT_NAME, "outputName", ITEMT_STRING))
129 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_OUTPUT, "system:power", ITEMT_SWITCH))
130 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_INPUT, "inputState", ITEMT_SWITCH))
131 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_BUTTON_TRIGGER, "system:button", ITEMT_STRING))
132 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_STATUS_EVENTTYPE, "lastEvent", ITEMT_STRING))
133 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_STATUS_EVENTCOUNT, "eventCount", ITEMT_NUMBER))
134 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_TIMER_AUTOON, "timerAutoOn", ITEMT_TIME))
135 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_TIMER_AUTOOFF, "timerAutoOff", ITEMT_TIME))
136 .add(new ShellyChannel(m, CHGR_RELAY, CHANNEL_TIMER_ACTIVE, "timerActive", ITEMT_SWITCH))
139 .add(new ShellyChannel(m, CHANNEL_GROUP_DIMMER_CONTROL, CHANNEL_BRIGHTNESS, "dimmerBrightness",
143 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_CONTROL, "rollerShutter", ITEMT_ROLLER))
144 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_POS, "rollerPosition", ITEMT_DIMMER))
145 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_FAV, "rollerFavorite", ITEMT_NUMBER))
146 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_STATE, "rollerState", ITEMT_STRING))
147 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_STOPR, "rollerStop", ITEMT_STRING))
148 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_ROL_CONTROL_SAFETY, "rollerSafety", ITEMT_SWITCH))
149 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_INPUT, "inputState", ITEMT_SWITCH))
150 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_STATUS_EVENTTYPE, "lastEvent", ITEMT_STRING))
151 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_STATUS_EVENTCOUNT, "eventCount", ITEMT_NUMBER))
152 .add(new ShellyChannel(m, CHGR_ROLLER, CHANNEL_EVENT_TRIGGER, "system:button", "system:button"))
155 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_LIGHT_POWER, "system:power", ITEMT_SWITCH))
156 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_INPUT, "inputState", ITEMT_SWITCH))
157 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_BUTTON_TRIGGER, "system:button", ITEMT_STRING))
158 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_STATUS_EVENTTYPE, "lastEvent", ITEMT_STRING))
159 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_STATUS_EVENTCOUNT, "eventCount", ITEMT_NUMBER))
160 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_TIMER_AUTOON, "timerAutoOn", ITEMT_TIME))
161 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_TIMER_AUTOOFF, "timerAutoOff", ITEMT_TIME))
162 .add(new ShellyChannel(m, CHGR_LIGHT, CHANNEL_TIMER_ACTIVE, "timerActive", ITEMT_SWITCH))
165 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_METER_CURRENTWATTS, "meterWatts", ITEMT_POWER))
166 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_METER_TOTALKWH, "meterTotal", ITEMT_ENERGY))
167 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_METER_LASTMIN1, "lastPower1", ITEMT_ENERGY))
168 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_LAST_UPDATE, "lastUpdate", ITEMT_DATETIME))
171 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_TOTALRET, "meterReturned", ITEMT_ENERGY))
172 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_REACTWATTS, "meterReactive", ITEMT_POWER))
173 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_VOLTAGE, "meterVoltage", ITEMT_VOLT))
174 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_CURRENT, "meterCurrent", ITEMT_AMP))
175 .add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_PFACTOR, "meterPowerFactor", ITEMT_PERCENT))
178 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_TEMP, "sensorTemp", ITEMT_TEMP))
179 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_HUM, "sensorHumidity", ITEMT_PERCENT))
180 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_LUX, "sensorLux", ITEMT_LUX))
181 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_ILLUM, "sensorIllumination", ITEMT_STRING))
182 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_VOLTAGE, "sensorADC", ITEMT_VOLT))
183 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_STATE, "sensorContact", ITEMT_CONTACT))
184 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_SSTATE, "sensorState", ITEMT_STRING))
185 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_TILT, "sensorTilt", ITEMT_ANGLE))
186 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_MOTION, "sensorMotion", ITEMT_SWITCH))
187 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_MOTION_TS, "motionTimestamp", ITEMT_DATETIME))
188 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_MOTION_ACT, "motionActive", ITEMT_SWITCH))
189 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_VIBRATION, "vibration", ITEMT_SWITCH))
190 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_FLOOD, "sensorFlood", ITEMT_SWITCH))
191 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_SMOKE, "sensorSmoke", ITEMT_SWITCH))
192 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_PPM, "sensorPPM", ITEMT_NUMBER))
193 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_VALVE, "sensorValve", ITEMT_STRING))
194 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_ALARM_STATE, "alarmState", ITEMT_STRING))
195 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_ERROR, "sensorError", ITEMT_STRING))
196 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_LAST_UPDATE, "lastUpdate", ITEMT_DATETIME))
197 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_SLEEPTIME, "sensorSleepTime", ITEMT_NUMBER))
198 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSE_KEY, "senseKey", ITEMT_STRING)) // Sense
201 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_INPUT, "inputState", ITEMT_SWITCH))
202 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_STATUS_EVENTTYPE, "lastEvent", ITEMT_STRING))
203 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_STATUS_EVENTCOUNT, "eventCount", ITEMT_NUMBER))
204 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_BUTTON_TRIGGER, "system:button", ITEMT_STRING))
205 .add(new ShellyChannel(m, CHGR_STATUS, CHANNEL_LAST_UPDATE, "lastUpdate", ITEMT_DATETIME))
207 // Addon with external sensors
208 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENDOR_TEMP1, "sensorExtTemp", ITEMT_TEMP))
209 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENDOR_TEMP2, "sensorExtTemp", ITEMT_TEMP))
210 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENDOR_TEMP3, "sensorExtTemp", ITEMT_TEMP))
211 .add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_ESENDOR_HUMIDITY, "sensorExtHum", ITEMT_PERCENT))
214 .add(new ShellyChannel(m, CHGR_BAT, CHANNEL_SENSOR_BAT_LEVEL, "system:battery-level", ITEMT_PERCENT))
215 .add(new ShellyChannel(m, CHGR_BAT, CHANNEL_SENSOR_BAT_LOW, "system:low-battery", ITEMT_SWITCH))
218 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_POSITION, "sensorPosition", ITEMT_DIMMER))
219 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_MODE, "controlMode", ITEMT_STRING))
220 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_PROFILE, "controlProfile", ITEMT_NUMBER))
221 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_SETTEMP, "targetTemp", ITEMT_TEMP))
222 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_BCONTROL, "boostControl", ITEMT_SWITCH))
223 .add(new ShellyChannel(m, CHGR_CONTROL, CHANNEL_CONTROL_BTIMER, "boostTimer", ITEMT_TIME));
226 public static @Nullable ShellyChannel getDefinition(String channelName) throws IllegalArgumentException {
227 String group = substringBefore(channelName, "#");
228 String channel = substringAfter(channelName, "#");
230 if (group.contains(CHANNEL_GROUP_METER)) {
231 group = CHANNEL_GROUP_METER; // map meter1..n to meter
232 } else if (group.contains(CHANNEL_GROUP_RELAY_CONTROL)) {
233 group = CHANNEL_GROUP_RELAY_CONTROL; // map meter1..n to meter
234 } else if (group.contains(CHANNEL_GROUP_LIGHT_CHANNEL)) {
235 group = CHANNEL_GROUP_LIGHT_CHANNEL;
236 } else if (group.contains(CHANNEL_GROUP_STATUS)) {
237 group = CHANNEL_GROUP_STATUS; // map status1..n to meter
240 if (channel.startsWith(CHANNEL_INPUT)) {
241 channel = CHANNEL_INPUT;
242 } else if (channel.startsWith(CHANNEL_BUTTON_TRIGGER)) {
243 channel = CHANNEL_BUTTON_TRIGGER;
244 } else if (channel.startsWith(CHANNEL_STATUS_EVENTTYPE)) {
245 channel = CHANNEL_STATUS_EVENTTYPE;
246 } else if (channel.startsWith(CHANNEL_STATUS_EVENTCOUNT)) {
247 channel = CHANNEL_STATUS_EVENTCOUNT;
250 String channelId = group + "#" + channel;
251 return CHANNEL_DEFINITIONS.get(channelId);
255 * Auto-create relay channels depending on relay type/mode
257 * @return ArrayList<Channel> of channels to be added to the thing
259 public static Map<String, Channel> createDeviceChannels(final Thing thing, final ShellyDeviceProfile profile,
260 final ShellySettingsStatus status) {
261 Map<String, Channel> add = new LinkedHashMap<>();
263 addChannel(thing, add, profile.settings.name != null, CHGR_DEVST, CHANNEL_DEVST_NAME);
265 if (!profile.isSensor && !profile.isIX && getDouble(status.temperature) != SHELLY_API_INVTEMP) {
266 // Only some devices report the internal device temp
267 addChannel(thing, add, status.tmp != null || status.temperature != null, CHGR_DEVST, 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 = (((status.meters != null) && (status.meters.size() > 1) && !profile.isRoller
273 && !profile.isRGBW2) || ((status.emeters != null && status.emeters.size() > 1)));
274 addChannel(thing, add, accuChannel, CHGR_DEVST, CHANNEL_DEVST_ACCUWATTS);
275 addChannel(thing, add, accuChannel, CHGR_DEVST, CHANNEL_DEVST_ACCUTOTAL);
276 addChannel(thing, add, accuChannel && (status.emeters != null), CHGR_DEVST, CHANNEL_DEVST_ACCURETURNED);
277 addChannel(thing, add, status.voltage != null || profile.settings.supplyVoltage != null, CHGR_DEVST,
278 CHANNEL_DEVST_VOLTAGE);
279 addChannel(thing, add,
280 profile.status.uptime != null && (!profile.hasBattery || profile.isMotion || profile.isTRV), CHGR_DEVST,
281 CHANNEL_DEVST_UPTIME);
282 addChannel(thing, add, true, CHGR_DEVST, CHANNEL_DEVST_UPDATE);
283 addChannel(thing, add, true, CHGR_DEVST, CHANNEL_DEVST_HEARTBEAT);
284 addChannel(thing, add, profile.settings.ledPowerDisable != null, CHGR_DEVST, CHANNEL_LED_POWER_DISABLE);
285 addChannel(thing, add, profile.settings.ledStatusDisable != null, CHGR_DEVST, CHANNEL_LED_STATUS_DISABLE); // WiFi
286 addChannel(thing, add, profile.settings.calibrated != null, CHGR_DEVST, CHANNEL_DEVST_CALIBRATED);
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> createRelayChannels(final Thing thing, final ShellyDeviceProfile profile,
297 final ShellyStatusRelay relay, int idx) {
298 Map<String, Channel> add = new LinkedHashMap<>();
299 String group = profile.getControlGroup(idx);
301 if (profile.settings.relays != null) {
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
307 addChannel(thing, add, rs.ison != null, group, CHANNEL_OUTPUT);
308 addChannel(thing, add, rs.name != null, group, CHANNEL_OUTPUT_NAME);
309 addChannel(thing, add, rs.autoOn != null, group, CHANNEL_TIMER_AUTOON);
310 addChannel(thing, add, rs.autoOff != null, group, CHANNEL_TIMER_AUTOOFF);
311 addChannel(thing, add, timer, group, CHANNEL_TIMER_ACTIVE);
314 // Shelly 1/1PM Addon
315 if (relay.extTemperature != null) {
316 addChannel(thing, add, relay.extTemperature.sensor1 != null, CHGR_SENSOR, CHANNEL_ESENDOR_TEMP1);
317 addChannel(thing, add, relay.extTemperature.sensor2 != null, CHGR_SENSOR, CHANNEL_ESENDOR_TEMP2);
318 addChannel(thing, add, relay.extTemperature.sensor3 != null, CHGR_SENSOR, CHANNEL_ESENDOR_TEMP3);
320 if (relay.extHumidity != null) {
321 addChannel(thing, add, relay.extHumidity.sensor1 != null, CHGR_SENSOR, CHANNEL_ESENDOR_HUMIDITY);
327 public static Map<String, Channel> createDimmerChannels(final Thing thing, final ShellyDeviceProfile profile,
328 final ShellySettingsStatus dstatus, int idx) {
329 Map<String, Channel> add = new LinkedHashMap<>();
330 String group = profile.getControlGroup(idx);
332 // Shelly Dimmer has an additional brightness channel
333 addChannel(thing, add, profile.isDimmer, group, CHANNEL_BRIGHTNESS);
335 if (profile.settings.dimmers != null) {
336 ShellySettingsDimmer ds = profile.settings.dimmers.get(idx);
337 addChannel(thing, add, ds.autoOn != null, group, CHANNEL_TIMER_AUTOON);
338 addChannel(thing, add, ds.autoOff != null, group, CHANNEL_TIMER_AUTOOFF);
340 ShellyShortLightStatus dss = dstatus.dimmers.get(idx);
341 addChannel(thing, add, dss != null && dss.hasTimer != null, group, CHANNEL_TIMER_ACTIVE);
346 public static Map<String, Channel> createLightChannels(final Thing thing, final ShellyDeviceProfile profile,
347 final ShellyStatusLightChannel status, int idx) {
348 Map<String, Channel> add = new LinkedHashMap<>();
349 String group = profile.getControlGroup(idx);
351 if (profile.settings.lights != null) {
352 ShellySettingsRgbwLight light = profile.settings.lights.get(idx);
353 // Create power channel in color mode and brightness channel in white mode
354 addChannel(thing, add, profile.inColor, group, CHANNEL_LIGHT_POWER);
355 addChannel(thing, add, !profile.inColor, group, CHANNEL_BRIGHTNESS);
356 addChannel(thing, add, light.temp != null, CHANNEL_GROUP_WHITE_CONTROL, CHANNEL_COLOR_TEMP);
357 addChannel(thing, add, light.autoOn != null, group, CHANNEL_TIMER_AUTOON);
358 addChannel(thing, add, light.autoOff != null, group, CHANNEL_TIMER_AUTOOFF);
359 addChannel(thing, add, status.hasTimer != null, group, CHANNEL_TIMER_ACTIVE);
364 public static Map<String, Channel> createInputChannels(final Thing thing, final ShellyDeviceProfile profile,
365 final ShellySettingsStatus status, String group) {
366 Map<String, Channel> add = new LinkedHashMap<>();
367 if (status.inputs != null) {
368 // Create channels per input. For devices with more than 1 input (Dimmer, 1L) multiple channel sets are
369 // created by adding the index to the channel name
370 boolean multi = (profile.numRelays == 1 || profile.isDimmer || profile.isRoller) && profile.numInputs >= 2;
371 for (int i = 0; i < profile.numInputs; i++) {
372 String suffix = multi ? String.valueOf(i + 1) : "";
373 ShellyInputState input = status.inputs.get(i);
374 addChannel(thing, add, true, group, CHANNEL_INPUT + suffix);
375 if (profile.inButtonMode(i)) {
376 addChannel(thing, add, input.event != null, group, CHANNEL_STATUS_EVENTTYPE + suffix);
377 addChannel(thing, add, input.eventCount != null, group, CHANNEL_STATUS_EVENTCOUNT + suffix);
379 addChannel(thing, add, true, group,
380 (!profile.isRoller ? CHANNEL_BUTTON_TRIGGER + suffix : CHANNEL_EVENT_TRIGGER));
382 } else if (status.input != null) {
383 // old RGBW2 firmware
384 addChannel(thing, add, true, group, CHANNEL_INPUT);
385 addChannel(thing, add, true, group, CHANNEL_BUTTON_TRIGGER);
390 public static Map<String, Channel> createRollerChannels(Thing thing, final ShellyRollerStatus roller) {
391 Map<String, Channel> add = new LinkedHashMap<>();
392 addChannel(thing, add, true, CHGR_ROLLER, CHANNEL_ROL_CONTROL_CONTROL);
393 addChannel(thing, add, true, CHGR_ROLLER, CHANNEL_ROL_CONTROL_STATE);
394 addChannel(thing, add, true, CHGR_ROLLER, CHANNEL_EVENT_TRIGGER);
395 addChannel(thing, add, roller.currentPos != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_POS);
396 addChannel(thing, add, roller.stopReason != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_STOPR);
397 addChannel(thing, add, roller.safetySwitch != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_SAFETY);
399 ShellyThingInterface handler = (ShellyThingInterface) thing.getHandler();
400 if (handler != null) {
401 ShellySettingsGlobal settings = handler.getProfile().settings;
402 if (getBool(settings.favoritesEnabled) && (settings.favorites != null)) {
403 addChannel(thing, add, roller.currentPos != null, CHGR_ROLLER, CHANNEL_ROL_CONTROL_FAV);
409 public static Map<String, Channel> createMeterChannels(Thing thing, final ShellySettingsMeter meter, String group) {
410 Map<String, Channel> newChannels = new LinkedHashMap<>();
411 addChannel(thing, newChannels, meter.power != null, group, CHANNEL_METER_CURRENTWATTS);
412 addChannel(thing, newChannels, meter.total != null, group, CHANNEL_METER_TOTALKWH);
413 addChannel(thing, newChannels, meter.counters != null && meter.counters[0] != null, group,
414 CHANNEL_METER_LASTMIN1);
415 addChannel(thing, newChannels, meter.timestamp != null, group, CHANNEL_LAST_UPDATE);
419 public static Map<String, Channel> createEMeterChannels(final Thing thing, final ShellySettingsEMeter emeter,
421 Map<String, Channel> newChannels = new LinkedHashMap<>();
422 addChannel(thing, newChannels, emeter.power != null, group, CHANNEL_METER_CURRENTWATTS);
423 addChannel(thing, newChannels, emeter.total != null, group, CHANNEL_METER_TOTALKWH);
424 addChannel(thing, newChannels, emeter.totalReturned != null, group, CHANNEL_EMETER_TOTALRET);
425 addChannel(thing, newChannels, emeter.reactive != null, group, CHANNEL_EMETER_REACTWATTS);
426 addChannel(thing, newChannels, emeter.voltage != null, group, CHANNEL_EMETER_VOLTAGE);
427 addChannel(thing, newChannels, emeter.current != null, group, CHANNEL_EMETER_CURRENT);
428 addChannel(thing, newChannels, emeter.power != null, group, CHANNEL_EMETER_PFACTOR); // EM has no PF. but power
430 addChannel(thing, newChannels, true, group, CHANNEL_LAST_UPDATE);
434 public static Map<String, Channel> createSensorChannels(final Thing thing, final ShellyDeviceProfile profile,
435 final ShellyStatusSensor sdata) {
436 Map<String, Channel> newChannels = new LinkedHashMap<>();
439 addChannel(thing, newChannels, sdata.tmp != null || sdata.thermostats != null, CHANNEL_GROUP_SENSOR,
440 CHANNEL_SENSOR_TEMP);
441 addChannel(thing, newChannels, sdata.hum != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_HUM);
442 addChannel(thing, newChannels, sdata.lux != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_LUX);
443 addChannel(thing, newChannels, sdata.lux != null && sdata.lux.illumination != null, CHANNEL_GROUP_SENSOR,
444 CHANNEL_SENSOR_ILLUM);
445 addChannel(thing, newChannels, sdata.flood != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_FLOOD);
446 addChannel(thing, newChannels, sdata.smoke != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_FLOOD);
447 addChannel(thing, newChannels, (profile.settings.externalPower != null) || (sdata.charger != null), CHGR_DEVST,
448 CHANNEL_DEVST_CHARGER);
449 addChannel(thing, newChannels,
450 sdata.motion != null || ((sdata.sensor != null) && (sdata.sensor.motion != null)), CHANNEL_GROUP_SENSOR,
451 CHANNEL_SENSOR_MOTION);
452 if (sdata.sensor != null) { // DW, Sense or Motion
453 addChannel(thing, newChannels, sdata.sensor.state != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_STATE); // DW/DW2
454 addChannel(thing, newChannels, sdata.sensor.motionActive != null, CHANNEL_GROUP_SENSOR, // Motion
455 CHANNEL_SENSOR_MOTION_ACT);
456 addChannel(thing, newChannels, sdata.sensor.motionTimestamp != null, CHANNEL_GROUP_SENSOR, // Motion
457 CHANNEL_SENSOR_MOTION_TS);
458 addChannel(thing, newChannels, sdata.sensor.vibration != null, CHANNEL_GROUP_SENSOR,
459 CHANNEL_SENSOR_VIBRATION);
461 if (sdata.accel != null) { // DW2
462 addChannel(thing, newChannels, sdata.accel.tilt != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_TILT);
466 if (sdata.gasSensor != null) {
467 addChannel(thing, newChannels, sdata.gasSensor.selfTestState != null, CHGR_DEVST, CHANNEL_DEVST_SELFTTEST);
468 addChannel(thing, newChannels, sdata.gasSensor.sensorState != null, CHANNEL_GROUP_SENSOR,
469 CHANNEL_SENSOR_SSTATE);
470 addChannel(thing, newChannels, sdata.concentration != null && sdata.concentration.ppm != null,
471 CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_PPM);
472 addChannel(thing, newChannels, sdata.valves != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_VALVE);
473 addChannel(thing, newChannels, sdata.gasSensor.sensorState != null, CHANNEL_GROUP_SENSOR,
474 CHANNEL_SENSOR_ALARM_STATE);
478 addChannel(thing, newChannels, profile.isSense, CHANNEL_GROUP_SENSOR, CHANNEL_SENSE_KEY);
481 addChannel(thing, newChannels, sdata.adcs != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_VOLTAGE);
485 addChannel(thing, newChannels, true, CHANNEL_GROUP_DEV_STATUS, CHANNEL_DEVST_SCHEDULE);
486 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_SETTEMP);
487 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_BCONTROL);
488 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_BTIMER);
489 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_POSITION);
490 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_MODE);
491 addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_PROFILE);
492 addChannel(thing, newChannels, true, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_STATE); // TRV
496 if (sdata.bat != null) {
497 addChannel(thing, newChannels, sdata.bat.value != null, CHANNEL_GROUP_BATTERY, CHANNEL_SENSOR_BAT_LEVEL);
498 addChannel(thing, newChannels, sdata.bat.value != null, CHANNEL_GROUP_BATTERY, CHANNEL_SENSOR_BAT_LOW);
501 addChannel(thing, newChannels, sdata.sensorError != null, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_ERROR);
502 addChannel(thing, newChannels, sdata.actReasons != null, CHGR_DEVST, CHANNEL_DEVST_WAKEUP);
503 addChannel(thing, newChannels, true, profile.isButton ? CHANNEL_GROUP_STATUS : CHANNEL_GROUP_SENSOR,
504 CHANNEL_LAST_UPDATE);
508 private static void addChannel(Thing thing, Map<String, Channel> newChannels, boolean supported, String group,
509 String channelName) throws IllegalArgumentException {
511 String channelId = group + "#" + channelName;
512 ChannelUID channelUID = new ChannelUID(thing.getUID(), channelId);
513 ShellyChannel channelDef = getDefinition(channelId);
514 if (channelDef != null) {
515 ChannelTypeUID channelTypeUID = channelDef.typeId.contains("system:")
516 ? new ChannelTypeUID(channelDef.typeId)
517 : new ChannelTypeUID(BINDING_ID, channelDef.typeId);
518 ChannelBuilder builder;
519 if (channelDef.typeId.equalsIgnoreCase("system:button")) {
520 builder = ChannelBuilder.create(channelUID, null).withKind(ChannelKind.TRIGGER);
522 builder = ChannelBuilder.create(channelUID, channelDef.itemType);
524 if (!channelDef.label.isEmpty()) {
525 char grseq = lastChar(group);
526 char chseq = lastChar(channelName);
527 char sequence = isDigit(chseq) ? chseq : grseq;
528 String label = !isDigit(sequence) ? channelDef.label : channelDef.label + " " + sequence;
529 builder.withLabel(label);
531 if (!channelDef.description.isEmpty()) {
532 builder.withDescription(channelDef.description);
534 newChannels.put(channelId, builder.withType(channelTypeUID).build());
539 public class ShellyChannel {
540 private final ShellyTranslationProvider messages;
541 public String group = "";
542 public String groupLabel = "";
543 public String groupDescription = "";
545 public String channel = "";
546 public String label = "";
547 public String description = "";
548 public String itemType = "";
549 public String typeId = "";
550 public String category = "";
551 public Set<String> tags = new HashSet<>();
552 public @Nullable Unit<?> unit;
553 public Optional<Integer> min = Optional.empty();
554 public Optional<Integer> max = Optional.empty();
555 public Optional<Integer> step = Optional.empty();
556 public Optional<String> pattern = Optional.empty();
558 public ShellyChannel(ShellyTranslationProvider messages, String group, String channel, String typeId,
559 String itemType, String... category) {
560 this.messages = messages;
562 this.channel = channel;
563 this.itemType = itemType;
564 this.typeId = typeId;
566 groupLabel = getText(PREFIX_GROUP + group + ".label");
567 if (groupLabel.contains(PREFIX_GROUP)) {
570 groupDescription = getText(PREFIX_GROUP + group + ".description");
571 if (groupDescription.contains(PREFIX_GROUP)) {
572 groupDescription = "";
574 label = getText(PREFIX_CHANNEL + typeId.replace(':', '.') + ".label");
575 if (label.contains(PREFIX_CHANNEL)) {
578 description = getText(PREFIX_CHANNEL + typeId + ".description");
579 if (description.contains(PREFIX_CHANNEL)) {
584 public String getChanneId() {
585 return group + "#" + channel;
588 public String getGroupLabel() {
589 return getGroupAttribute("group");
592 public String getGroupDescription() {
593 return getGroupAttribute("group");
596 public String getLabel() {
597 return getChannelAttribute("label");
600 public String getDescription() {
601 return getChannelAttribute("description");
604 public boolean getAdvanced() {
605 String attr = getChannelAttribute("advanced");
606 return attr.isEmpty() ? false : Boolean.valueOf(attr);
609 public boolean getReadyOnly() {
610 String attr = getChannelAttribute("readOnly");
611 return attr.isEmpty() ? false : Boolean.valueOf(attr);
614 public String getCategory() {
615 return getChannelAttribute("category");
618 public String getMin() {
619 return getChannelAttribute("min");
622 public String getMax() {
623 return getChannelAttribute("max");
626 public String getStep() {
627 return getChannelAttribute("step");
630 public String getPattern() {
631 return getChannelAttribute("pattern");
634 public String getGroupAttribute(String attribute) {
635 String key = PREFIX_GROUP + group + "." + attribute;
636 String value = messages.getText(key);
637 return !value.equals(key) ? value : "";
640 public String getChannelAttribute(String attribute) {
641 String key = PREFIX_CHANNEL + channel + "." + attribute;
642 String value = messages.getText(key);
643 return !value.equals(key) ? value : "";
646 private String getText(String key) {
647 return messages.get(key);
651 public static class ChannelMap {
652 private final Map<String, ShellyChannel> map = new HashMap<>();
654 private ChannelMap add(ShellyChannel def) {
655 map.put(def.getChanneId(), def);
659 public ShellyChannel get(String channelName) throws IllegalArgumentException {
660 ShellyChannel def = null;
661 if (channelName.contains("#")) {
662 def = map.get(channelName);
667 for (HashMap.Entry<String, ShellyChannel> entry : map.entrySet()) {
668 if (entry.getValue().channel.contains("#" + channelName)) {
669 def = entry.getValue();
675 throw new IllegalArgumentException("Channel definition for " + channelName + " not found!");