]> git.basschouten.com Git - openhab-addons.git/blob
fb0ac38c19d4cba0b8708855bb00b19fae0ddb99
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.shelly.internal.api;
14
15 import static org.openhab.binding.shelly.internal.ShellyBindingConstants.*;
16 import static org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.*;
17 import static org.openhab.binding.shelly.internal.discovery.ShellyThingCreator.*;
18 import static org.openhab.binding.shelly.internal.util.ShellyUtils.*;
19
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsDevice;
28 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsDimmer;
29 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsGlobal;
30 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsInput;
31 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsRelay;
32 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsRgbwLight;
33 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsStatus;
34 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyThermnostat;
35 import org.openhab.binding.shelly.internal.util.ShellyVersionDTO;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import com.google.gson.Gson;
40
41 /**
42  * The {@link ShellyDeviceProfile} creates a device profile based on the settings returned from the API's /settings
43  * call. This is used to be more dynamic in controlling the device, but also to overcome some issues in the API (e.g.
44  * RGBW2 returns "no meter" even it has one)
45  *
46  * @author Markus Michels - Initial contribution
47  */
48 @NonNullByDefault
49 public class ShellyDeviceProfile {
50     private final Logger logger = LoggerFactory.getLogger(ShellyDeviceProfile.class);
51     private static final Pattern GEN1_VERSION_PATTERN = Pattern.compile("v\\d+\\.\\d+\\.\\d+(-[a-z0-9]*)?");
52     private static final Pattern GEN2_VERSION_PATTERN = Pattern.compile("\\d+\\.\\d+\\.\\d+(-[a-fh-z0-9]*)?");
53
54     public boolean initialized = false; // true when initialized
55
56     public String thingName = "";
57     public boolean extFeatures = false;
58
59     public String settingsJson = "";
60     public ShellySettingsDevice device = new ShellySettingsDevice();
61     public ShellySettingsGlobal settings = new ShellySettingsGlobal();
62     public ShellySettingsStatus status = new ShellySettingsStatus();
63
64     public String name = "";
65     public boolean discoverable = true;
66     public boolean alwaysOn = true;
67     public boolean isGen2 = false;
68     public boolean isBlu = false;
69     public String gateway = "";
70
71     public String hwRev = "";
72     public String hwBatchId = "";
73     public String fwVersion = "";
74     public String fwDate = "";
75
76     public boolean hasRelays = false; // true if it has at least 1 power meter
77     public int numRelays = 0; // number of relays/outputs
78     public int numRollers = 0; // number of Rollers, usually 1
79     public boolean isRoller = false; // true for Shelly2 in roller mode
80     public boolean isDimmer = false; // true for a Shelly Dimmer (SHDM-1)
81     public int numInputs = 0; // number of inputs
82
83     public int numMeters = 0;
84     public boolean isEMeter = false; // true for ShellyEM/3EM
85
86     public boolean isLight = false; // true if it is a Shelly Bulb/RGBW2
87     public boolean isBulb = false; // true only if it is a Bulb
88     public boolean isDuo = false; // true only if it is a Duo
89     public boolean isRGBW2 = false; // true only if it a RGBW2
90     public boolean inColor = false; // true if bulb/rgbw2 is in color mode
91
92     public boolean isSensor = false; // true for HT & Smoke
93     public boolean hasBattery = false; // true if battery device
94     public boolean isSense = false; // true if thing is a Shelly Sense
95     public boolean isMotion = false; // true if thing is a Shelly Sense
96     public boolean isHT = false; // true for H&T
97     public boolean isDW = false; // true for Door Window sensor
98     public boolean isButton = false; // true for a Shelly Button 1
99     public boolean isIX = false; // true for a Shelly IX
100     public boolean isTRV = false; // true for a Shelly TRV
101     public boolean isSmoke = false; // true for Shelly Smoke
102     public boolean isWall = false; // true: Shelly Wall Display
103     public boolean is3EM = false; // true for Shelly 3EM and Pro 3EM
104     public boolean isEM50 = false; // true for Shelly Pro EM50
105
106     public int minTemp = 0; // Bulb/Duo: Min Light Temp
107     public int maxTemp = 0; // Bulb/Duo: Max Light Temp
108
109     public int updatePeriod = 2 * UPDATE_SETTINGS_INTERVAL_SECONDS + 10;
110
111     public String coiotEndpoint = "";
112
113     public Map<String, String> irCodes = new HashMap<>(); // Sense: list of stored IR codes
114
115     public ShellyDeviceProfile() {
116     }
117
118     public ShellyDeviceProfile initialize(String thingType, String jsonIn, @Nullable ShellySettingsDevice device)
119             throws ShellyApiException {
120         Gson gson = new Gson();
121         initialized = false;
122         if (device != null) {
123             this.device = device;
124         }
125
126         initFromThingType(thingType);
127
128         String json = jsonIn;
129         // It is not guaranteed, that the array entries are in order. Check all
130         // possible variants. See openhab#15514.
131         if (json.contains("\"ext_temperature\":{\"0\":[{") || json.contains("\"ext_temperature\":{\"1\":[{")
132                 || json.contains("\"ext_temperature\":{\"2\":[{")) {
133             // Shelly UNI uses ext_temperature array, reformat to avoid GSON exception
134             json = json.replace("ext_temperature", "ext_temperature_array");
135         }
136         if (json.contains("\"ext_humidity\":{\"0\":[{")) {
137             // Shelly UNI uses ext_humidity array, reformat to avoid GSON exception
138             json = json.replace("ext_humidity", "ext_humidity_array");
139         }
140         settingsJson = json;
141         settings = fromJson(gson, json, ShellySettingsGlobal.class);
142
143         // General settings
144         if (getString(device.hostname).isEmpty() && !getString(device.mac).isEmpty()) {
145             device.hostname = device.mac.length() >= 12 ? "shelly-" + device.mac.toUpperCase().substring(6, 11)
146                     : "unknown";
147         }
148         device.mode = getString(settings.mode).toLowerCase();
149         name = getString(settings.name);
150         hwRev = settings.hwinfo != null ? getString(settings.hwinfo.hwRevision) : "";
151         hwBatchId = settings.hwinfo != null ? getString(settings.hwinfo.batchId.toString()) : "";
152         fwDate = substringBefore(device.fw, "-");
153         fwVersion = extractFwVersion(device.fw);
154         ShellyVersionDTO version = new ShellyVersionDTO();
155         extFeatures = version.compare(fwVersion, SHELLY_API_FW_110) >= 0;
156         discoverable = (settings.discoverable == null) || settings.discoverable;
157
158         String mode = getString(device.mode);
159         isRoller = mode.equalsIgnoreCase(SHELLY_MODE_ROLLER);
160         inColor = isLight && mode.equalsIgnoreCase(SHELLY_MODE_COLOR);
161
162         numRelays = !isLight ? getInteger(device.numOutputs) : 0;
163         if ((numRelays > 0) && (settings.relays == null)) {
164             numRelays = 0;
165         }
166         hasRelays = (numRelays > 0) || isDimmer;
167         numRollers = getInteger(device.numRollers);
168         numInputs = settings.inputs != null ? settings.inputs.size() : hasRelays ? isRoller ? 2 : 1 : 0;
169
170         isEMeter = settings.emeters != null;
171         numMeters = !isEMeter ? getInteger(device.numMeters) : getInteger(device.numEMeters);
172         if ((numMeters == 0) && isLight) {
173             // RGBW2 doesn't report, but has one
174             numMeters = inColor ? 1 : getInteger(device.numOutputs);
175         }
176
177         initialized = true;
178         return this;
179     }
180
181     public boolean containsEventUrl(String eventType) {
182         return containsEventUrl(settingsJson, eventType);
183     }
184
185     public boolean containsEventUrl(String json, String eventType) {
186         String settings = json.toLowerCase();
187         return settings.contains((eventType + SHELLY_EVENTURL_SUFFIX).toLowerCase());
188     }
189
190     public boolean isInitialized() {
191         return initialized;
192     }
193
194     public void initFromThingType(String name) {
195         String thingType = (name.contains("-") ? substringBefore(name, "-") : name).toLowerCase().trim();
196         if (thingType.isEmpty()) {
197             return;
198         }
199
200         isBlu = isBluSeries(thingType); // e.g. SBBT for BLU Button
201         isGen2 = isGeneration2(thingType) || isBlu;
202
203         String type = getString(device.type);
204         isDimmer = type.equalsIgnoreCase(SHELLYDT_DIMMER) || type.equalsIgnoreCase(SHELLYDT_DIMMER2)
205                 || type.equalsIgnoreCase(SHELLYDT_PLUSDIMMERUS)
206                 || thingType.equalsIgnoreCase(THING_TYPE_SHELLYPLUSDIMMERUS_STR);
207         isBulb = thingType.equals(THING_TYPE_SHELLYBULB_STR);
208         isDuo = thingType.equals(THING_TYPE_SHELLYDUO_STR) || thingType.equals(THING_TYPE_SHELLYVINTAGE_STR)
209                 || thingType.equals(THING_TYPE_SHELLYDUORGBW_STR);
210         isRGBW2 = thingType.startsWith(THING_TYPE_SHELLYRGBW2_PREFIX);
211         isLight = isBulb || isDuo || isRGBW2;
212         if (isLight) {
213             minTemp = isBulb ? MIN_COLOR_TEMP_BULB : MIN_COLOR_TEMP_DUO;
214             maxTemp = isBulb ? MAX_COLOR_TEMP_BULB : MAX_COLOR_TEMP_DUO;
215         }
216
217         boolean isFlood = thingType.equals(THING_TYPE_SHELLYFLOOD_STR);
218         isSmoke = thingType.equals(THING_TYPE_SHELLYSMOKE_STR) || thingType.equals(THING_TYPE_SHELLYPLUSSMOKE_STR);
219         boolean isGas = thingType.equals(THING_TYPE_SHELLYGAS_STR);
220         boolean isUNI = thingType.equals(THING_TYPE_SHELLYUNI_STR);
221         isHT = thingType.equals(THING_TYPE_SHELLYHT_STR) || thingType.equals(THING_TYPE_SHELLYPLUSHT_STR)
222                 || thingType.equals(THING_TYPE_SHELLYPLUSHTG3_STR) || thingType.equals(THING_TYPE_SHELLYBLUHT_STR);
223         isDW = thingType.equals(THING_TYPE_SHELLYDOORWIN_STR) || thingType.equals(THING_TYPE_SHELLYDOORWIN2_STR)
224                 || thingType.equals(THING_TYPE_SHELLYBLUDW_STR);
225         isMotion = thingType.startsWith(THING_TYPE_SHELLYMOTION_STR)
226                 || thingType.equals(THING_TYPE_SHELLYBLUMOTION_STR);
227         isSense = thingType.equals(THING_TYPE_SHELLYSENSE_STR);
228         isIX = thingType.equals(THING_TYPE_SHELLYIX3_STR) || thingType.equals(THING_TYPE_SHELLYPLUSI4_STR)
229                 || thingType.equals(THING_TYPE_SHELLYPLUSI4DC_STR);
230         isButton = thingType.equals(THING_TYPE_SHELLYBUTTON1_STR) || thingType.equals(THING_TYPE_SHELLYBUTTON2_STR)
231                 || thingType.equals(THING_TYPE_SHELLYBLUBUTTON_STR);
232         isTRV = thingType.equals(THING_TYPE_SHELLYTRV_STR);
233         isWall = thingType.equals(THING_TYPE_SHELLYPLUSWALLDISPLAY_STR);
234         is3EM = thingType.equals(THING_TYPE_SHELLY3EM_STR) || thingType.startsWith(THING_TYPE_SHELLYPRO3EM_STR);
235         isEM50 = thingType.startsWith(THING_TYPE_SHELLYPROEM50_STR);
236
237         isSensor = isHT || isFlood || isDW || isSmoke || isGas || isButton || isUNI || isMotion || isSense || isTRV
238                 || isWall;
239         hasBattery = isHT || isFlood || isDW || isSmoke || isButton || isMotion || isTRV || isBlu;
240         alwaysOn = !hasBattery || (isMotion && !isBlu) || isSense; // true means: device is reachable all the time (no
241                                                                    // sleep mode)
242     }
243
244     public void updateFromStatus(ShellySettingsStatus status) {
245         if (hasRelays) {
246             // Dimmer-2 doesn't report inputs under /settings, only on /status, we need to update that info after init
247             if (status.inputs != null) {
248                 numInputs = status.inputs.size();
249             }
250         } else if (status.input != null) {
251             // RGBW2
252             numInputs = 1;
253         }
254     }
255
256     public String getControlGroup(int i) {
257         if (i < 0) {
258             logger.debug("{}: Invalid index {} for getControlGroup()", thingName, i);
259             return "";
260         }
261         int idx = i + 1;
262         if (isDimmer) {
263             return CHANNEL_GROUP_DIMMER_CONTROL;
264         } else if (isRoller) {
265             return numRollers <= 1 ? CHANNEL_GROUP_ROL_CONTROL : CHANNEL_GROUP_ROL_CONTROL + idx;
266         } else if (isDimmer) {
267             return CHANNEL_GROUP_RELAY_CONTROL;
268         } else if (hasRelays) {
269             return numRelays <= 1 ? CHANNEL_GROUP_RELAY_CONTROL : CHANNEL_GROUP_RELAY_CONTROL + idx;
270         } else if (isRGBW2) {
271             return settings.lights == null || settings.lights != null && settings.lights.size() <= 1
272                     ? CHANNEL_GROUP_LIGHT_CONTROL
273                     : CHANNEL_GROUP_LIGHT_CHANNEL + idx;
274         } else if (isLight) {
275             return CHANNEL_GROUP_LIGHT_CONTROL;
276         } else if (isButton) {
277             return CHANNEL_GROUP_STATUS;
278         } else if (isSensor) {
279             return CHANNEL_GROUP_SENSOR;
280         }
281
282         // e.g. ix3
283         return numRelays == 1 ? CHANNEL_GROUP_STATUS : CHANNEL_GROUP_STATUS + idx;
284     }
285
286     public String getMeterGroup(int idx) {
287         return numMeters > 1 ? CHANNEL_GROUP_METER + (idx + 1) : CHANNEL_GROUP_METER;
288     }
289
290     public String getInputGroup(int i) {
291         int idx = i + 1; // group names are 1-based
292         if (isRGBW2) {
293             return CHANNEL_GROUP_LIGHT_CONTROL;
294         } else if (isIX) {
295             return CHANNEL_GROUP_STATUS + idx;
296         } else if (isButton) {
297             return CHANNEL_GROUP_STATUS;
298         } else if (isRoller) {
299             return numRelays <= 2 ? CHANNEL_GROUP_ROL_CONTROL : CHANNEL_GROUP_ROL_CONTROL + idx;
300         } else {
301             // Device has 1 input per relay: 0=off, 1+2 depend on switch mode
302             return numRelays <= 1 ? CHANNEL_GROUP_RELAY_CONTROL : CHANNEL_GROUP_RELAY_CONTROL + idx;
303         }
304     }
305
306     public String getInputSuffix(int i) {
307         int idx = i + 1; // channel names are 1-based
308         if (isRGBW2 || isIX) {
309             return ""; // RGBW2 has only 1 channel
310         } else if (isRoller || isDimmer) {
311             // Roller has 2 relays, but it will be mapped to 1 roller with 2 inputs
312             return String.valueOf(idx);
313         } else if (hasRelays) {
314             return numRelays == 1 && numInputs >= 2 ? String.valueOf(idx) : "";
315         }
316         return "";
317     }
318
319     @SuppressWarnings("null")
320     public boolean inButtonMode(int idx) {
321         if (idx < 0) {
322             logger.debug("{}: Invalid index {} for inButtonMode()", thingName, idx);
323             return false;
324         }
325         String btnType = "";
326         if (isButton) {
327             return true;
328         } else if (isIX && settings.inputs != null && idx < settings.inputs.size()) {
329             ShellySettingsInput input = settings.inputs.get(idx);
330             btnType = getString(input.btnType);
331         } else if (isDimmer) {
332             if (settings.dimmers != null) {
333                 ShellySettingsDimmer dimmer = settings.dimmers.get(0);
334                 btnType = getString(dimmer.btnType);
335             }
336         } else if (settings.relays != null) {
337             if (numRelays == 1) {
338                 ShellySettingsRelay relay = settings.relays.get(0);
339                 if (relay.btnType != null) {
340                     btnType = getString(relay.btnType);
341                 } else {
342                     // Shelly 1L has 2 inputs
343                     btnType = idx == 0 ? getString(relay.btnType1) : getString(relay.btnType2);
344                 }
345             } else if (idx < settings.relays.size()) {
346                 // only one input channel
347                 ShellySettingsRelay relay = settings.relays.get(idx);
348                 btnType = getString(relay.btnType);
349             }
350         } else if (isRGBW2 && (settings.lights != null) && (idx < settings.lights.size())) {
351             ShellySettingsRgbwLight light = settings.lights.get(idx);
352             btnType = getString(light.btnType);
353         }
354
355         return btnType.equalsIgnoreCase(SHELLY_BTNT_MOMENTARY) || btnType.equalsIgnoreCase(SHELLY_BTNT_MOM_ON_RELEASE)
356                 || btnType.equalsIgnoreCase(SHELLY_BTNT_ONE_BUTTON) || btnType.equalsIgnoreCase(SHELLY_BTNT_TWO_BUTTON)
357                 || btnType.equalsIgnoreCase(SHELLY_BTNT_DETACHED);
358     }
359
360     public int getRollerFav(int id) {
361         if (id >= 0 && getBool(settings.favoritesEnabled) && settings.favorites != null
362                 && id < settings.favorites.size()) {
363             return settings.favorites.get(id).pos;
364         }
365         return -1;
366     }
367
368     public String[] getValveProfileList(int valveId) {
369         if (isTRV && settings.thermostats != null) {
370             int sz = settings.thermostats.size();
371             if (valveId <= sz) {
372                 if (settings.thermostats != null) {
373                     ShellyThermnostat t = settings.thermostats.get(valveId);
374                     return t.profileNames;
375                 }
376             }
377         }
378         return new String[0];
379     }
380
381     public String getValueProfile(int valveId, int profileId) {
382         int id = profileId;
383         if (id <= 0 && settings.thermostats != null) {
384             id = settings.thermostats.get(0).profile;
385         }
386         return "" + id;
387     }
388
389     public static String extractFwVersion(@Nullable String version) {
390         if (version != null) {
391             // fix version e.g.
392             // 20210319-122304/v.1.10-Dimmer1-gfd4cc10 (with v.1. instead of v1.)
393             // 20220809-125346/v1.12-g99f7e0b (.0 in 1.12.0 missing)
394             String vers = version.replace("/v.1.10-", "/v1.10.0-") //
395                     .replace("/v1.12-", "/v1.12.0");
396
397             // Extract version from string, e.g. 20210226-091047/v1.10.0-rc2-89-g623b41ec0-master
398             Matcher matcher = version.startsWith("v") ? GEN1_VERSION_PATTERN.matcher(vers)
399                     : GEN2_VERSION_PATTERN.matcher(vers);
400             if (matcher.find()) {
401                 return matcher.group(0);
402             }
403         }
404         return "";
405     }
406
407     public static boolean isGeneration2(String thingType) {
408         return thingType.startsWith("shellyplus") || thingType.startsWith("shellypro") || thingType.contains("mini")
409                 || thingType.startsWith("shellywall") || (thingType.startsWith("shelly") && thingType.contains("g3"))
410                 || isBluSeries(thingType) || thingType.startsWith(THING_TYPE_SHELLYBLUGW_STR);
411     }
412
413     public static boolean isBluSeries(String thingType) {
414         return thingType.startsWith("shellyblu") && !thingType.startsWith(THING_TYPE_SHELLYBLUGW_STR);
415     }
416
417     public boolean coiotEnabled() {
418         if ((settings.coiot != null) && (settings.coiot.enabled != null)) {
419             return settings.coiot.enabled;
420         }
421
422         // If device is not yet intialized or the enabled property is missing we assume that CoIoT is enabled
423         return true;
424     }
425
426     public static String buildBluServiceName(String name, String mac) throws IllegalArgumentException {
427         String model = name.contains("-") ? substringBefore(name, "-") : name; // e.g. SBBT-02C or just SBDW
428         switch (model) {
429             case SHELLYDT_BLUBUTTON:
430                 return (THING_TYPE_SHELLYBLUBUTTON_STR + "-" + mac).toLowerCase();
431             case SHELLYDT_BLUDW:
432                 return (THING_TYPE_SHELLYBLUDW_STR + "-" + mac).toLowerCase();
433             case SHELLYDT_BLUMOTION:
434                 return (THING_TYPE_SHELLYBLUMOTION_STR + "-" + mac).toLowerCase();
435             case SHELLYDT_BLUHT:
436                 return (THING_TYPE_SHELLYBLUHT_STR + "-" + mac).toLowerCase();
437             default:
438                 throw new IllegalArgumentException("Unsupported BLU device model " + model);
439         }
440     }
441 }