]> git.basschouten.com Git - openhab-addons.git/blob
74895c0e86ee5143176e9a44f3aeea135401bd70
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.api.ShellyApiJsonDTO.*;
17 import static org.openhab.binding.shelly.internal.util.ShellyUtils.*;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.shelly.internal.api.ShellyApiJsonDTO.ShellySettingsDimmer;
24 import org.openhab.binding.shelly.internal.api.ShellyApiJsonDTO.ShellySettingsGlobal;
25 import org.openhab.binding.shelly.internal.api.ShellyApiJsonDTO.ShellySettingsInput;
26 import org.openhab.binding.shelly.internal.api.ShellyApiJsonDTO.ShellySettingsRelay;
27 import org.openhab.binding.shelly.internal.api.ShellyApiJsonDTO.ShellySettingsRgbwLight;
28 import org.openhab.binding.shelly.internal.api.ShellyApiJsonDTO.ShellySettingsStatus;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import com.google.gson.Gson;
33
34 /**
35  * The {@link ShellyDeviceProfile} creates a device profile based on the settings returned from the API's /settings
36  * call. This is used to be more dynamic in controlling the device, but also to overcome some issues in the API (e.g.
37  * RGBW2 returns "no meter" even it has one)
38  *
39  * @author Markus Michels - Initial contribution
40  */
41 @NonNullByDefault
42 public class ShellyDeviceProfile {
43     private final Logger logger = LoggerFactory.getLogger(ShellyDeviceProfile.class);
44
45     public boolean initialized = false; // true when initialized
46
47     public String thingName = "";
48     public String deviceType = "";
49
50     public String settingsJson = "";
51     public ShellySettingsGlobal settings = new ShellySettingsGlobal();
52     public ShellySettingsStatus status = new ShellySettingsStatus();
53
54     public String hostname = "";
55     public String mode = "";
56     public boolean discoverable = true;
57
58     public String hwRev = "";
59     public String hwBatchId = "";
60     public String mac = "";
61     public String fwId = "";
62     public String fwVersion = "";
63     public String fwDate = "";
64
65     public boolean hasRelays = false; // true if it has at least 1 power meter
66     public int numRelays = 0; // number of relays/outputs
67     public int numRollers = 0; // number of Rollers, usually 1
68     public boolean isRoller = false; // true for Shelly2 in roller mode
69     public boolean isDimmer = false; // true for a Shelly Dimmer (SHDM-1)
70     public int numInputs = 0; // number of inputs
71
72     public int numMeters = 0;
73     public boolean isEMeter = false; // true for ShellyEM/3EM
74
75     public boolean isLight = false; // true if it is a Shelly Bulb/RGBW2
76     public boolean isBulb = false; // true only if it is a Bulb
77     public boolean isDuo = false; // true only if it is a Duo
78     public boolean isRGBW2 = false; // true only if it a a RGBW2
79     public boolean inColor = false; // true if bulb/rgbw2 is in color mode
80
81     public boolean isSensor = false; // true for HT & Smoke
82     public boolean hasBattery = false; // true if battery device
83     public boolean isSense = false; // true if thing is a Shelly Sense
84     public boolean isMotion = false; // true if thing is a Shelly Sense
85     public boolean isHT = false; // true for H&T
86     public boolean isDW = false; // true for Door Window sensor
87     public boolean isButton = false; // true for a Shelly Button 1
88     public boolean isIX3 = false; // true for a Shelly IX
89
90     public int minTemp = 0; // Bulb/Duo: Min Light Temp
91     public int maxTemp = 0; // Bulb/Duo: Max Light Temp
92
93     public int updatePeriod = 2 * UPDATE_SETTINGS_INTERVAL_SECONDS + 10;
94
95     public String coiotEndpoint = "";
96
97     public Map<String, String> irCodes = new HashMap<>(); // Sense: list of stored IR codes
98
99     public ShellyDeviceProfile() {
100     }
101
102     public ShellyDeviceProfile initialize(String thingType, String json) throws ShellyApiException {
103         Gson gson = new Gson();
104
105         initialized = false;
106
107         initFromThingType(thingType);
108         settingsJson = json;
109         ShellySettingsGlobal gs = fromJson(gson, json, ShellySettingsGlobal.class);
110         settings = gs; // only update when no exception
111
112         // General settings
113         deviceType = getString(settings.device.type);
114         mac = getString(settings.device.mac);
115         hostname = settings.device.hostname != null && !settings.device.hostname.isEmpty()
116                 ? settings.device.hostname.toLowerCase()
117                 : "shelly-" + mac.toUpperCase().substring(6, 11);
118         mode = !getString(settings.mode).isEmpty() ? getString(settings.mode).toLowerCase() : "";
119         hwRev = settings.hwinfo != null ? getString(settings.hwinfo.hwRevision) : "";
120         hwBatchId = settings.hwinfo != null ? getString(settings.hwinfo.batchId.toString()) : "";
121         fwDate = substringBefore(settings.fw, "/");
122         fwVersion = substringBetween(settings.fw, "/", "@");
123         fwId = substringAfter(settings.fw, "@");
124         discoverable = (settings.discoverable == null) || settings.discoverable;
125
126         inColor = isLight && mode.equalsIgnoreCase(SHELLY_MODE_COLOR);
127
128         numRelays = !isLight ? getInteger(settings.device.numOutputs) : 0;
129         if ((numRelays > 0) && (settings.relays == null)) {
130             numRelays = 0;
131         }
132         isDimmer = deviceType.equalsIgnoreCase(SHELLYDT_DIMMER) || deviceType.equalsIgnoreCase(SHELLYDT_DIMMER2);
133         isRoller = mode.equalsIgnoreCase(SHELLY_MODE_ROLLER);
134         hasRelays = (numRelays > 0) || isDimmer;
135         numRollers = getInteger(settings.device.numRollers);
136         numInputs = settings.inputs != null ? settings.inputs.size() : hasRelays ? isRoller ? 2 : 1 : 0;
137
138         isEMeter = settings.emeters != null;
139         numMeters = !isEMeter ? getInteger(settings.device.numMeters) : getInteger(settings.device.numEMeters);
140         if ((numMeters == 0) && isLight) {
141             // RGBW2 doesn't report, but has one
142             numMeters = inColor ? 1 : getInteger(settings.device.numOutputs);
143         }
144
145         if (settings.sleepMode != null) {
146             // Sensor, usually 12h, H&T in USB mode 10min
147             updatePeriod = getString(settings.sleepMode.unit).equalsIgnoreCase("m") ? settings.sleepMode.period * 60 // minutes
148                     : settings.sleepMode.period * 3600; // hours
149             updatePeriod += 60; // give 1min extra
150         } else if ((settings.coiot != null) && (settings.coiot.updatePeriod != null)) {
151             // Derive from CoAP update interval, usually 2*15+10s=40sec -> 70sec
152             updatePeriod = Math.max(UPDATE_SETTINGS_INTERVAL_SECONDS, 2 * getInteger(settings.coiot.updatePeriod)) + 10;
153         } else {
154             updatePeriod = UPDATE_SETTINGS_INTERVAL_SECONDS + 10;
155         }
156
157         initialized = true;
158         return this;
159     }
160
161     public boolean containsEventUrl(String eventType) {
162         return containsEventUrl(settingsJson, eventType);
163     }
164
165     public boolean containsEventUrl(String json, String eventType) {
166         String settings = json.toLowerCase();
167         return settings.contains((eventType + SHELLY_EVENTURL_SUFFIX).toLowerCase());
168     }
169
170     public boolean isInitialized() {
171         return initialized;
172     }
173
174     public void initFromThingType(String name) {
175         String thingType = (name.contains("-") ? substringBefore(name, "-") : name).toLowerCase().trim();
176         if (thingType.isEmpty()) {
177             return;
178         }
179
180         isBulb = thingType.equals(THING_TYPE_SHELLYBULB_STR);
181         isDuo = thingType.equals(THING_TYPE_SHELLYDUO_STR) || thingType.equals(THING_TYPE_SHELLYVINTAGE_STR)
182                 || thingType.equals(THING_TYPE_SHELLYDUORGBW_STR);
183         isRGBW2 = thingType.startsWith(THING_TYPE_SHELLYRGBW2_PREFIX);
184         isLight = isBulb || isDuo || isRGBW2;
185         if (isLight) {
186             minTemp = isBulb ? MIN_COLOR_TEMP_BULB : MIN_COLOR_TEMP_DUO;
187             maxTemp = isBulb ? MAX_COLOR_TEMP_BULB : MAX_COLOR_TEMP_DUO;
188         }
189
190         boolean isFlood = thingType.equals(THING_TYPE_SHELLYFLOOD_STR);
191         boolean isSmoke = thingType.equals(THING_TYPE_SHELLYSMOKE_STR);
192         boolean isGas = thingType.equals(THING_TYPE_SHELLYGAS_STR);
193         boolean isUNI = thingType.equals(THING_TYPE_SHELLYUNI_STR);
194         isHT = thingType.equals(THING_TYPE_SHELLYHT_STR);
195         isDW = thingType.equals(THING_TYPE_SHELLYDOORWIN_STR) || thingType.equals(THING_TYPE_SHELLYDOORWIN2_STR);
196         isMotion = thingType.startsWith(THING_TYPE_SHELLYMOTION_STR);
197         isSense = thingType.equals(THING_TYPE_SHELLYSENSE_STR);
198         isIX3 = thingType.equals(THING_TYPE_SHELLYIX3_STR);
199         isButton = thingType.equals(THING_TYPE_SHELLYBUTTON1_STR);
200         isSensor = isHT || isFlood || isDW || isSmoke || isGas || isButton || isUNI || isMotion || isSense;
201         hasBattery = isHT || isFlood || isDW || isSmoke || isButton || isMotion; // we assume that Sense is connected to
202                                                                                  // the charger
203     }
204
205     public void updateFromStatus(ShellySettingsStatus status) {
206         if (hasRelays) {
207             // Dimmer-2 doesn't report inputs under /settings, only on /status, we need to update that info after
208             // initialization
209             if (status.inputs != null) {
210                 numInputs = status.inputs.size();
211             }
212         } else if (status.input != null) {
213             // RGBW2
214             numInputs = 1;
215         }
216     }
217
218     public String getControlGroup(int i) {
219         if (i < 0) {
220             logger.debug("{}: Invalid index {} for getControlGroup()", thingName, i);
221             return "";
222         }
223         int idx = i + 1;
224         if (isDimmer) {
225             return CHANNEL_GROUP_DIMMER_CONTROL;
226         } else if (isRoller) {
227             return numRollers <= 1 ? CHANNEL_GROUP_ROL_CONTROL : CHANNEL_GROUP_ROL_CONTROL + idx;
228         } else if (isDimmer) {
229             return CHANNEL_GROUP_RELAY_CONTROL;
230         } else if (hasRelays) {
231             return numRelays <= 1 ? CHANNEL_GROUP_RELAY_CONTROL : CHANNEL_GROUP_RELAY_CONTROL + idx;
232         } else if (isLight) {
233             return numRelays <= 1 ? CHANNEL_GROUP_LIGHT_CONTROL : CHANNEL_GROUP_LIGHT_CONTROL + idx;
234         } else if (isButton) {
235             return CHANNEL_GROUP_STATUS;
236         } else if (isSensor) {
237             return CHANNEL_GROUP_SENSOR;
238         }
239
240         // e.g. ix3
241         return numRelays == 1 ? CHANNEL_GROUP_STATUS : CHANNEL_GROUP_STATUS + idx;
242     }
243
244     public String getInputGroup(int i) {
245         int idx = i + 1; // group names are 1-based
246         if (isRGBW2) {
247             return CHANNEL_GROUP_LIGHT_CONTROL;
248         } else if (isIX3) {
249             return CHANNEL_GROUP_STATUS + idx;
250         } else if (isButton) {
251             return CHANNEL_GROUP_STATUS;
252         } else if (isRoller) {
253             return numRelays <= 2 ? CHANNEL_GROUP_ROL_CONTROL : CHANNEL_GROUP_ROL_CONTROL + idx;
254         } else {
255             // Device has 1 input per relay: 0=off, 1+2 depend on switch mode
256             return numRelays <= 1 ? CHANNEL_GROUP_RELAY_CONTROL : CHANNEL_GROUP_RELAY_CONTROL + idx;
257         }
258     }
259
260     public String getInputSuffix(int i) {
261         int idx = i + 1; // channel names are 1-based
262         if (isRGBW2 || isIX3) {
263             return ""; // RGBW2 has only 1 channel
264         } else if (isRoller || isDimmer) {
265             // Roller has 2 relays, but it will be mapped to 1 roller with 2 inputs
266             return String.valueOf(idx);
267         } else if (hasRelays) {
268             return (numRelays) == 1 && (numInputs >= 2) ? String.valueOf(idx) : "";
269         }
270         return "";
271     }
272
273     public boolean inButtonMode(int idx) {
274         if (idx < 0) {
275             logger.debug("{}: Invalid index {} for inButtonMode()", thingName, idx);
276             return false;
277         }
278         String btnType = "";
279         if (isButton) {
280             return true;
281         } else if (isIX3 && (settings.inputs != null) && (idx < settings.inputs.size())) {
282             ShellySettingsInput input = settings.inputs.get(idx);
283             btnType = getString(input.btnType);
284         } else if (isDimmer) {
285             if (settings.dimmers != null) {
286                 ShellySettingsDimmer dimmer = settings.dimmers.get(0);
287                 btnType = dimmer.btnType;
288             }
289         } else if (settings.relays != null) {
290             if (numRelays == 1) {
291                 ShellySettingsRelay relay = settings.relays.get(0);
292                 if (relay.btnType != null) {
293                     btnType = getString(relay.btnType);
294                 } else {
295                     // Shelly 1L has 2 inputs
296                     btnType = idx == 0 ? getString(relay.btnType1) : getString(relay.btnType2);
297                 }
298             } else if (idx < settings.relays.size()) {
299                 // only one input channel
300                 ShellySettingsRelay relay = settings.relays.get(idx);
301                 btnType = getString(relay.btnType);
302             }
303         } else if (isRGBW2 && (settings.lights != null) && (idx < settings.lights.size())) {
304             ShellySettingsRgbwLight light = settings.lights.get(idx);
305             btnType = light.btnType;
306         }
307
308         logger.trace("{}: Checking for trigger, button-type[{}] is {}", thingName, idx, btnType);
309         return btnType.equalsIgnoreCase(SHELLY_BTNT_MOMENTARY) || btnType.equalsIgnoreCase(SHELLY_BTNT_MOM_ON_RELEASE)
310                 || btnType.equalsIgnoreCase(SHELLY_BTNT_ONE_BUTTON) || btnType.equalsIgnoreCase(SHELLY_BTNT_TWO_BUTTON);
311     }
312
313     public int getRollerFav(int id) {
314         if ((id >= 0) && getBool(settings.favoritesEnabled) && (settings.favorites != null)
315                 && (id < settings.favorites.size())) {
316             return settings.favorites.get(id).pos;
317         }
318         return -1;
319     }
320 }