]> git.basschouten.com Git - openhab-addons.git/blob
d0d09d32d34b7b96969ec311f9cf4b0045a1a867
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.ShellySettingsStatus;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.google.gson.Gson;
32 import com.google.gson.JsonSyntaxException;
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
71     public int numMeters = 0;
72     public boolean isEMeter = false; // true for ShellyEM/3EM
73
74     public boolean isLight = false; // true if it is a Shelly Bulb/RGBW2
75     public boolean isBulb = false; // true only if it is a Bulb
76     public boolean isDuo = false; // true only if it is a Duo
77     public boolean isRGBW2 = false; // true only if it a a RGBW2
78     public boolean inColor = false; // true if bulb/rgbw2 is in color mode
79
80     public boolean isSensor = false; // true for HT & Smoke
81     public boolean hasBattery = false; // true if battery device
82     public boolean isSense = false; // true if thing is a Shelly Sense
83     public boolean isHT = false; // true for H&T
84     public boolean isDW = false; // true for Door Window sensor
85     public boolean isButton = false; // true for a Shelly Button 1
86     public boolean isIX3 = false; // true for a Shelly IX
87
88     public int minTemp = 0; // Bulb/Duo: Min Light Temp
89     public int maxTemp = 0; // Bulb/Duo: Max Light Temp
90
91     public int updatePeriod = 2 * UPDATE_SETTINGS_INTERVAL_SECONDS + 10;
92
93     public Map<String, String> irCodes = new HashMap<>(); // Sense: list of stored IR codes
94
95     public ShellyDeviceProfile() {
96     }
97
98     public ShellyDeviceProfile initialize(String thingType, String json) throws ShellyApiException {
99         Gson gson = new Gson();
100
101         initialized = false;
102
103         try {
104             initFromThingType(thingType);
105             settingsJson = json;
106             settings = gson.fromJson(json, ShellySettingsGlobal.class);
107         } catch (IllegalArgumentException | JsonSyntaxException e) {
108             throw new ShellyApiException(
109                     thingName + ": Unable to transform settings JSON " + e.toString() + ", json='" + json + "'", e);
110         }
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         hasRelays = (numRelays > 0) || isDimmer;
134         numRollers = getInteger(settings.device.numRollers);
135
136         isEMeter = settings.emeters != null;
137         numMeters = !isEMeter ? getInteger(settings.device.numMeters) : getInteger(settings.device.numEMeters);
138         if ((numMeters == 0) && isLight) {
139             // RGBW2 doesn't report, but has one
140             numMeters = inColor ? 1 : getInteger(settings.device.numOutputs);
141         }
142         isRoller = mode.equalsIgnoreCase(SHELLY_MODE_ROLLER);
143
144         if (settings.sleepMode != null) {
145             // Sensor, usally 12h
146             updatePeriod = getString(settings.sleepMode.unit).equalsIgnoreCase("m") ? settings.sleepMode.period * 60 // minutes
147                     : settings.sleepMode.period * 3600; // hours
148             updatePeriod += 600; // give 10min extra
149         } else if ((settings.coiot != null) && (settings.coiot.updatePeriod != null)) {
150             // Derive from CoAP update interval, usually 2*15+5s=50sec -> 70sec
151             updatePeriod = Math.max(UPDATE_SETTINGS_INTERVAL_SECONDS, 3 * getInteger(settings.coiot.updatePeriod)) + 10;
152         } else {
153             updatePeriod = 2 * UPDATE_SETTINGS_INTERVAL_SECONDS + 10;
154         }
155
156         initialized = true;
157         return this;
158     }
159
160     public boolean containsEventUrl(String eventType) {
161         return containsEventUrl(settingsJson, eventType);
162     }
163
164     public boolean containsEventUrl(String json, String eventType) {
165         String settings = json.toLowerCase();
166         return settings.contains((eventType + SHELLY_EVENTURL_SUFFIX).toLowerCase());
167     }
168
169     public boolean isInitialized() {
170         return initialized;
171     }
172
173     public void initFromThingType(String name) {
174         String thingType = (name.contains("-") ? substringBefore(name, "-") : name).toLowerCase().trim();
175         if (thingType.isEmpty()) {
176             return;
177         }
178
179         isBulb = thingType.equals(THING_TYPE_SHELLYBULB_STR);
180         isDuo = thingType.equals(THING_TYPE_SHELLYDUO_STR) || thingType.equals(THING_TYPE_SHELLYVINTAGE_STR);
181         isRGBW2 = thingType.startsWith(THING_TYPE_SHELLYRGBW2_PREFIX);
182         isLight = isBulb || isDuo || isRGBW2;
183         if (isLight) {
184             minTemp = isBulb ? MIN_COLOR_TEMP_BULB : MIN_COLOR_TEMP_DUO;
185             maxTemp = isBulb ? MAX_COLOR_TEMP_BULB : MAX_COLOR_TEMP_DUO;
186         }
187
188         boolean isFlood = thingType.equals(THING_TYPE_SHELLYFLOOD_STR);
189         boolean isSmoke = thingType.equals(THING_TYPE_SHELLYSMOKE_STR);
190         boolean isGas = thingType.equals(THING_TYPE_SHELLYGAS_STR);
191         isHT = thingType.equals(THING_TYPE_SHELLYHT_STR);
192         isDW = thingType.equals(THING_TYPE_SHELLYDOORWIN_STR) || thingType.equals(THING_TYPE_SHELLYDOORWIN2_STR);
193         isSense = thingType.equals(THING_TYPE_SHELLYSENSE_STR);
194         isIX3 = thingType.equals(THING_TYPE_SHELLYIX3_STR);
195         isButton = thingType.equals(THING_TYPE_SHELLYBUTTON1_STR);
196         isSensor = isHT || isFlood || isDW || isSmoke || isGas || isButton || isSense;
197         hasBattery = isHT || isFlood || isDW || isSmoke || isButton; // we assume that Sense is connected to // the
198                                                                      // charger
199     }
200
201     public String getControlGroup(int i) {
202         if (i < 0) {
203             logger.debug("{}: Invalid index {} for getControlGroup()", thingName, i);
204             return "";
205         }
206         int idx = i + 1;
207         if (isDimmer) {
208             return CHANNEL_GROUP_DIMMER_CONTROL;
209         } else if (isRoller) {
210             return numRollers == 1 ? CHANNEL_GROUP_ROL_CONTROL : CHANNEL_GROUP_ROL_CONTROL + idx;
211         } else if (hasRelays) {
212             return numRelays == 1 ? CHANNEL_GROUP_RELAY_CONTROL : CHANNEL_GROUP_RELAY_CONTROL + idx;
213         } else if (isLight) {
214             return numRelays == 1 ? CHANNEL_GROUP_LIGHT_CONTROL : CHANNEL_GROUP_LIGHT_CONTROL + idx;
215         } else if (isButton) {
216             return CHANNEL_GROUP_STATUS;
217         } else if (isSensor) {
218             return CHANNEL_GROUP_SENSOR;
219         }
220
221         // e.g. ix3
222         return numRelays == 1 ? CHANNEL_GROUP_STATUS : CHANNEL_GROUP_STATUS + idx;
223     }
224
225     public String getInputGroup(int i) {
226         int idx = i + 1; // group names are 1-based
227         if (isRGBW2) {
228             return CHANNEL_GROUP_LIGHT_CONTROL;
229         } else if (isIX3) {
230             return CHANNEL_GROUP_STATUS + idx;
231         } else if (isButton) {
232             return CHANNEL_GROUP_STATUS;
233         } else if (isRoller) {
234             return numRelays <= 2 ? CHANNEL_GROUP_ROL_CONTROL : CHANNEL_GROUP_ROL_CONTROL + idx;
235         } else {
236             // Device has 1 input per relay: 0=off, 1+2 depend on switch mode
237             return numRelays <= 1 ? CHANNEL_GROUP_RELAY_CONTROL : CHANNEL_GROUP_RELAY_CONTROL + idx;
238         }
239     }
240
241     public String getInputChannel(int i) {
242         int idx = i + 1; // channel names are 1-based
243         if (isRGBW2 || isIX3) {
244             return CHANNEL_INPUT; // RGBW2 has only 1 channel
245         } else if (hasRelays) {
246             return CHANNEL_INPUT + idx;
247         }
248         return CHANNEL_INPUT;
249     }
250
251     public boolean inButtonMode(int idx) {
252         if (idx < 0) {
253             logger.debug("{}: Invalid index {} for inButtonMode()", thingName, idx);
254             return false;
255         }
256         String btnType = "";
257         if (isButton) {
258             return true;
259         } else if (isIX3) {
260             if ((settings.inputs != null) && (idx >= 0) && (idx < settings.inputs.size())) {
261                 ShellySettingsInput input = settings.inputs.get(idx);
262                 btnType = input.btnType;
263             }
264         } else if (isDimmer) {
265             if ((settings.dimmers != null) && (idx >= 0) && (idx < settings.dimmers.size())) {
266                 ShellySettingsDimmer dimmer = settings.dimmers.get(idx);
267                 btnType = dimmer.btnType;
268             }
269         } else if ((settings.relays != null) && (idx >= 0) && (idx < settings.relays.size())) {
270             ShellySettingsRelay relay = settings.relays.get(idx);
271             btnType = relay.btnType;
272         }
273
274         if (btnType.equals(SHELLY_BTNT_MOMENTARY) || btnType.equals(SHELLY_BTNT_MOM_ON_RELEASE)
275                 || btnType.equals(SHELLY_BTNT_DETACHED) || btnType.equals(SHELLY_BTNT_ONE_BUTTON)) {
276             return true;
277         }
278         return false;
279     }
280 }