2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
14 * The {@link ShellyCoIoTVersion1} implements the parsing for CoIoT version 1
16 * @author Markus Michels - Initial contribution
18 package org.openhab.binding.shelly.internal.coap;
20 import static org.openhab.binding.shelly.internal.ShellyBindingConstants.*;
21 import static org.openhab.binding.shelly.internal.api.ShellyApiJsonDTO.*;
22 import static org.openhab.binding.shelly.internal.util.ShellyUtils.*;
24 import java.util.List;
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.openhab.binding.shelly.internal.coap.ShellyCoapJSonDTO.CoIotDescrBlk;
30 import org.openhab.binding.shelly.internal.coap.ShellyCoapJSonDTO.CoIotDescrSen;
31 import org.openhab.binding.shelly.internal.coap.ShellyCoapJSonDTO.CoIotSensor;
32 import org.openhab.binding.shelly.internal.handler.ShellyBaseHandler;
33 import org.openhab.binding.shelly.internal.handler.ShellyColorUtils;
34 import org.openhab.core.library.types.OnOffType;
35 import org.openhab.core.library.types.OpenClosedType;
36 import org.openhab.core.library.unit.SIUnits;
37 import org.openhab.core.library.unit.Units;
38 import org.openhab.core.types.State;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * The {@link ShellyCoIoTVersion1} implements the parsing for CoIoT version 2
45 * @author Markus Michels - Initial contribution
48 public class ShellyCoIoTVersion2 extends ShellyCoIoTProtocol implements ShellyCoIoTInterface {
49 private final Logger logger = LoggerFactory.getLogger(ShellyCoIoTVersion2.class);
51 public ShellyCoIoTVersion2(String thingName, ShellyBaseHandler thingHandler, Map<String, CoIotDescrBlk> blkMap,
52 Map<String, CoIotDescrSen> sensorMap) {
53 super(thingName, thingHandler, blkMap, sensorMap);
57 public int getVersion() {
58 return ShellyCoapJSonDTO.COIOT_VERSION_2;
62 * Process CoIoT status update message. If a status update is received, but the device description has not been
63 * received yet a GET is send to query device description.
65 * @param sensorUpdates Complete list of sensor updates
66 * @param sen The specific sensor update to handle
67 * @param updates Resulting updates (new updates will be added to input list)
70 public boolean handleStatusUpdate(List<CoIotSensor> sensorUpdates, CoIotDescrSen sen, int serial, CoIotSensor s,
71 Map<String, State> updates, ShellyColorUtils col) {
72 // first check the base implementation
73 if (super.handleStatusUpdate(sensorUpdates, sen, s, updates, col)) {
74 // process by the base class
78 // Process status information and convert into channel updates
79 // Integer rIndex = Integer.parseInt(sen.links) + 1;
80 int rIndex = getIdFromBlk(sen);
81 String rGroup = getProfile().numRelays <= 1 ? CHANNEL_GROUP_RELAY_CONTROL
82 : CHANNEL_GROUP_RELAY_CONTROL + rIndex;
83 String mGroup = profile.numMeters <= 1 ? CHANNEL_GROUP_METER
84 : CHANNEL_GROUP_METER + (profile.isEMeter ? getIdFromBlk(sen) : rIndex);
86 boolean processed = true;
87 double value = getDouble(s.value);
90 case "3103": // H, humidity, 0-100 percent, unknown 999
91 case "3106": // L, luminosity, lux, U32, -1
92 case "3110": // S, luminosityLevel, dark/twilight/bright, "unknown"=unknown
93 case "3111": // B, battery, 0-100%, unknown -1
94 case "3112": // S, charger, 0/1
95 case "3115": // S, sensorError, 0/1
96 // processed by base handler
99 case "6109": // P, overpowerValue, W, U32
101 // Relay: S, mode, relay/roller or
102 // Dimmer: S, mode, color/white
103 // skip, could check against thing mode...
106 case "1101": // S, output, 0/1
107 updatePower(profile, updates, rIndex, sen, s, sensorUpdates);
109 case "1102": // roler_0: S, roller, open/close/stop -> roller state
110 updateChannel(updates, CHANNEL_GROUP_ROL_CONTROL, CHANNEL_ROL_CONTROL_STATE, getStringType(s.valueStr));
112 case "1103": // roller_0: S, rollerPos, 0-100, unknown -1
113 int pos = Math.max(SHELLY_MIN_ROLLER_POS, Math.min((int) value, SHELLY_MAX_ROLLER_POS));
114 updateChannel(updates, CHANNEL_GROUP_ROL_CONTROL, CHANNEL_ROL_CONTROL_CONTROL,
115 toQuantityType((double) (SHELLY_MAX_ROLLER_POS - pos), Units.PERCENT));
117 case "1105": // S, valvle, closed/opened/not_connected/failure/closing/opening/checking or unbknown
118 updateChannel(updates, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_VALVE, getStringType(s.valueStr));
121 case "2101": // Input_0: S, input, 0/1
122 case "2201": // Input_1: S, input, 0/1
123 case "2301": // Input_2: S, input, 0/1
124 case "2401": // Input_3: S, input, 0/1
125 handleInput(sen, s, rGroup, updates);
127 case "2102": // Input_0: EV, inputEvent, S/SS/SSS/L
128 case "2202": // Input_1: EV, inputEvent
129 case "2302": // Input_2: EV, inputEvent
130 case "2402": // Input_3: EV, inputEvent
131 handleInputEvent(sen, getString(s.valueStr), -1, serial, updates);
133 case "2103": // EVC, inputEventCnt, U16
134 case "2203": // EVC, inputEventCnt, U16
135 case "2303": // EVC, inputEventCnt, U16
136 case "2403": // EVC, inputEventCnt, U16
137 handleInputEvent(sen, "", getInteger((int) s.value), serial, updates);
139 case "3101": // sensor_0: T, extTemp, C, -55/125; unknown 999
140 case "3201": // sensor_1: T, extTemp, C, -55/125; unknown 999
141 case "3301": // sensor_2: T, extTemp, C, -55/125; unknown 999
142 int idx = getExtTempId(sen.id);
144 // H&T, Fllod, DW only have 1 channel, 1/1PM with Addon have up to to 3 sensors
145 String channel = profile.isSensor ? CHANNEL_SENSOR_TEMP : CHANNEL_SENSOR_TEMP + idx;
146 updateChannel(updates, CHANNEL_GROUP_SENSOR, channel,
147 toQuantityType(value, DIGITS_TEMP, SIUnits.CELSIUS));
149 logger.debug("{}: Unable to get extSensorId {} from {}/{}", thingName, sen.id, sen.type, sen.desc);
152 case "3104": // T, deviceTemp, Celsius -40/300; 999=unknown
153 updateChannel(updates, CHANNEL_GROUP_DEV_STATUS, CHANNEL_DEVST_ITEMP,
154 toQuantityType(value, DIGITS_NONE, SIUnits.CELSIUS));
156 case "3102": // sensor_0: T, extTemp, F, -67/257, unknown 999
157 case "3202": // sensor_1: T, extTemp, F, -67/257, unknown 999
158 case "3302": // sensor_2: T, extTemp, F, -67/257, unknown 999
159 case "3105": // T, deviceTemp, Fahrenheit -40/572
160 // skip, we use only C
163 case "3107": // C, Gas concentration, U16
164 updateChannel(updates, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_PPM, getDecimal(s.value));
166 case "3108": // DW: S, dwIsOpened, 0/1, -1=unknown
168 updateChannel(updates, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_CONTACT,
169 value != 0 ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
171 logger.debug("{}: Sensor error reported, check device, battery and installation", thingName);
174 case "3109": // S, tilt, 0-180deg, -1
175 updateChannel(updates, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_TILT,
176 toQuantityType(s.value, DIGITS_NONE, Units.DEGREE_ANGLE));
178 case "3113": // S, sensorOp, warmup/normal/fault
179 updateChannel(updates, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_SSTATE, getStringType(s.valueStr));
181 case "3114": // S, selfTest, not_completed/completed/running/pending
182 updateChannel(updates, CHANNEL_GROUP_DEV_STATUS, CHANNEL_DEVST_SELFTTEST, getStringType(s.valueStr));
184 case "3117": // S, extInput, 0/1
185 handleInput(sen, s, rGroup, updates);
188 updateChannel(updates, mGroup, CHANNEL_SENSOR_VOLTAGE,
189 toQuantityType(getDouble(s.value), DIGITS_VOLT, Units.VOLT));
192 case "4101": // relay_0/light_0: P, power, W
193 case "4201": // relay_1/light_1: P, power, W
194 case "4301": // relay_2/light_2: P, power, W
195 case "4401": // relay_3/light_3: P, power, W
196 case "4105": // emeter_0: P, power, W
197 case "4205": // emeter_1: P, power, W
198 case "4305": // emeter_2: P, power, W
199 case "4102": // roller_0: P, rollerPower, W, 0-2300, unknown -1
200 case "4202": // roller_1: P, rollerPower, W, 0-2300, unknown -1
201 logger.debug("{}: Updating {}:currentWatts with {}", thingName, mGroup, s.value);
202 updateChannel(updates, mGroup, CHANNEL_METER_CURRENTWATTS,
203 toQuantityType(s.value, DIGITS_WATT, Units.WATT));
204 if (!profile.isRGBW2 && !profile.isRoller) {
205 // only for regular, not-aggregated meters
206 updateChannel(updates, mGroup, CHANNEL_LAST_UPDATE, getTimestamp());
210 case "4103": // relay_0: E, energy, Wmin, U32
211 case "4203": // relay_1: E, energy, Wmin, U32
212 case "4303": // relay_2: E, energy, Wmin, U32
213 case "4403": // relay_3: E, energy, Wmin, U32
214 case "4104": // roller_0: E, rollerEnergy, Wmin, U32, -1
215 case "4204": // roller_0: E, rollerEnergy, Wmin, U32, -1
216 case "4106": // emeter_0: E, energy, Wh, U32
217 case "4206": // emeter_1: E, energy, Wh, U32
218 case "4306": // emeter_2: E, energy, Wh, U32
219 double total = profile.isEMeter ? s.value / 1000 : s.value / 60 / 1000;
220 updateChannel(updates, mGroup, CHANNEL_METER_TOTALKWH,
221 toQuantityType(total, DIGITS_KWH, Units.KILOWATT_HOUR));
224 case "4107": // emeter_0: E, energyReturned, Wh, U32, -1
225 case "4207": // emeter_1: E, energyReturned, Wh, U32, -1
226 case "4307": // emeter_2: E, energyReturned, Wh, U32, -1
227 updateChannel(updates, mGroup, CHANNEL_EMETER_TOTALRET,
228 toQuantityType(getDouble(s.value) / 1000, DIGITS_KWH, Units.KILOWATT_HOUR));
231 case "4108": // emeter_0: V, voltage, 0-265V, U32, -1
232 case "4208": // emeter_1: V, voltage, 0-265V, U32, -1
233 case "4308": // emeter_2: V, voltage, 0-265V, U32, -1
234 updateChannel(updates, mGroup, CHANNEL_EMETER_VOLTAGE,
235 toQuantityType(getDouble(s.value), DIGITS_VOLT, Units.VOLT));
238 case "4109": // emeter_0: A, current, 0/120A, -1
239 case "4209": // emeter_1: A, current, 0/120A, -1
240 case "4309": // emeter_2: A, current, 0/120A, -1
241 updateChannel(updates, rGroup, CHANNEL_EMETER_CURRENT,
242 toQuantityType(getDouble(s.value), DIGITS_VOLT, Units.AMPERE));
245 case "4110": // emeter_0: S, powerFactor, 0/1, -1
246 case "4210": // emeter_1: S, powerFactor, 0/1, -1
247 case "4310": // emeter_2: S, powerFactor, 0/1, -1
248 updateChannel(updates, rGroup, CHANNEL_EMETER_PFACTOR, getDecimal(s.value));
251 case "5101": // {"I":5101,"T":"S","D":"brightness","R":"0/100","L":1},
252 case "5102": // {"I":5102,"T":"S","D":"gain","R":"0/100","L":1},
253 case "5103": // {"I":5103,"T":"S","D":"colorTemp","U":"K","R":"3000/6500","L":1},
254 case "5105": // {"I":5105,"T":"S","D":"red","R":"0/255","L":1},
255 case "5106": // {"I":5106,"T":"S","D":"green","R":"0/255","L":1},
256 case "5107": // {"I":5107,"T":"S","D":"blue","R":"0/255","L":1},
257 case "5108": // {"I":5108,"T":"S","D":"white","R":"0/255","L":1},
258 // already covered by base handler
261 case "6101": // A, overtemp, 0/1
263 thingHandler.postEvent(ALARM_TYPE_OVERTEMP, true);
266 case "6102": // relay_0: A, overpower, 0/1
267 case "6202": // relay_1: A, overpower, 0/1
268 case "6302": // relay_2: A, overpower, 0/1
269 case "6402": // relay_3: A, overpower, 0/1
271 thingHandler.postEvent(ALARM_TYPE_OVERPOWER, true);
274 case "6104": // relay_0: A, loadError, 0/1
275 case "6204": // relay_1: A, loadError, 0/1
276 case "6304": // relay_2: A, loadError, 0/1
277 case "6404": // relay_3: A, loadError, 0/1
279 thingHandler.postEvent(ALARM_TYPE_LOADERR, true);
282 case "6103": // roller_0: A, rollerStopReason, normal/safety_switch/obstacle/overpower
283 reason = getString(s.valueStr);
284 updateChannel(updates, CHANNEL_GROUP_ROL_CONTROL, CHANNEL_ROL_CONTROL_STOPR, getStringType(reason));
285 if (!reason.isEmpty() && !reason.equalsIgnoreCase(SHELLY_API_STOPR_NORMAL)) {
286 thingHandler.postEvent("ROLLER_" + reason.toUpperCase(), true);
288 case "6106": // A, flood, 0/1, -1
289 updateChannel(updates, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_FLOOD,
290 value == 1 ? OnOffType.ON : OnOffType.OFF);
293 case "6107": // A, motion, 0/1, -1
294 // {"I":6107,"T":"A","D":"motion","R":["0/1","-1"],"L":1},
295 updateChannel(updates, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_MOTION,
296 value == 1 ? OnOffType.ON : OnOffType.OFF);
298 case "3119": // Motion timestamp
299 // {"I":3119,"T":"S","D":"timestamp","U":"s","R":["U32","-1"],"L":1},
300 updateChannel(updates, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_MOTION_TS,
301 getTimestamp(getString(profile.settings.timezone), (long) s.value));
303 case "3120": // motionActive
304 // {"I":3120,"T":"S","D":"motionActive","R":["0/1","-1"],"L":1},
307 case "6108": // A, gas, none/mild/heavy/test or unknown
308 updateChannel(updates, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_ALARM_STATE, getStringType(s.valueStr));
310 case "6110": // A, vibration, 0/1, -1=unknown
311 updateChannel(updates, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_VIBRATION,
312 value == 1 ? OnOffType.ON : OnOffType.OFF);
314 case "9102": // EV, wakeupEvent, battery/button/periodic/poweron/sensor/ext_power, "unknown"=unknown
315 if (s.valueArray.size() > 0) {
316 thingHandler.updateWakeupReason(s.valueArray);
317 lastWakeup = (String) s.valueArray.get(0);
320 case "9103": // EVC, cfgChanged, U16
321 if ((lastCfgCount != -1) && (lastCfgCount != s.value)) {
322 thingHandler.requestUpdates(1, true); // refresh config
324 lastCfgCount = (int) s.value;
334 public CoIotDescrSen fixDescription(@Nullable CoIotDescrSen sen, Map<String, CoIotDescrBlk> blkMap) {
335 return super.fixDescription(sen, blkMap);
338 private static final String ID_4101_DESCR = "{ \"I\":4101, \"T\":\"P\", \"D\":\"power\", \"U\": \"W\", \"R\":\"0/3500\", \"L\": 1}";
339 private static final String ID_4103_DESCR = "{ \"I\":4103, \"T\":\"E\", \"D\":\"energy\", \"U\": \"Wmin\", \"R\":\"U32\", \"L\": 1}";
342 public void completeMissingSensorDefinition(Map<String, CoIotDescrSen> sensorMap) {
343 if (profile.isDuo && profile.inColor) {
344 addSensor(sensorMap, "4101", ID_4101_DESCR);
345 addSensor(sensorMap, "4103", ID_4103_DESCR);
347 super.completeMissingSensorDefinition(sensorMap);