-
Notifications
You must be signed in to change notification settings - Fork 19
/
Shelly-HT.groovy
420 lines (375 loc) · 16.2 KB
/
Shelly-HT.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/**
* Shelly MQTT Device Handler
*
* Copyright 2019 Scott Grayban
*
* Please Note: This app is NOT released under any open-source license.
* Please be sure to read the license agreement before installing this code.
*
* This software package is created and licensed by Scott Grayban.
*
* This software, along with associated elements, including but not limited to online and/or electronic documentation are
* protected by international laws and treaties governing intellectual property rights.
*
* This software has been licensed to you. All rights are reserved. You may use and/or modify the software.
* You may not sublicense or distribute this software or any modifications to third parties in any way.
*
* You may not distribute any part of this software without the author's express permission
*
* By downloading, installing, and/or executing this software you hereby agree to the terms and conditions set forth
* in the Software license agreement.
* This agreement can be found on-line at: http://sgrayban.borgnet.online:8081/scotts-projects/software_License_Agreement.txt
*
* Hubitat is the Trademark and intellectual Property of Hubitat Inc.
* Shelly is the Trademark and Intellectual Property of Allterco Robotics Ltd
* Scott Grayban has no formal or informal affiliations or relationships with Hubitat or Allterco Robotics Ltd.
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License Agreement
* for the specific language governing permissions and limitations under the License.
*
*-------------------------------------------------------------------------------------------------------------------
*
* Changes:
* 3.1.3 - Merged PR #5 - Change the names of inputs for MQTT Username and Password
to avoid conflict with API Shelly username and password.
* 3.1.2 - Corrected code for getting device settings and current status
* 3.1.1 - Added code to pull settings and status when the device wakes up.
* - RSSI value is definded as exelent, good or poor. The actual rssi reading is under state variables.
* 3.1.0 - Removed MQTT alpha code
* - New code location https://gitlab.borgnet.us:8443/sgrayban/shelly-drivers
* 3.0.0 - Added flood/rain sensor device
* - Added MQTT connection check(15min) for use with RM notifications - attribute MQTTconnected
* - Renamed driver to Shelly MQTT since it can be expanded for other MQTT only devices
* 2.2.1 - License Change
* 2.1.2 - Added txtEnable
* 2.1.0 - Simplified the subscription - only need the shelly deviceid now
* 2.0.0 - First release of code
* 1.0.0 - Initial port
*
*/
metadata {
definition (
name: "Shelly MQTT",
namespace: "sgrayban",
author: "Scott Grayban",
importUrl: "https://raw.githubusercontent.com/ShellyUSA/Hubitat-Drivers/master/Shelly-HT.groovy"
)
{
capability "Initialize"
capability "Actuator"
capability "Sensor"
capability "TemperatureMeasurement"
capability "RelativeHumidityMeasurement"
capability "Battery"
capability "WaterSensor"
attribute "LastUpdateTime", "string"
attribute "temperature", "string"
attribute "humidity", "string"
attribute "battery", "string"
attribute "flood", "string"
attribute "wet", "string"
attribute "MQTTconnected", "string"
attribute "DeviceType", "string"
attribute "Primary_SSID", "string"
attribute "PrimaryIP", "number"
attribute "Secondary_WiFi", "string"
attribute "Secondary_SSID", "string"
attribute "FW_Update_Needed", "string"
attribute "MAC", "string"
attribute "Cloud", "string"
attribute "Cloud_Connected", "string"
attribute "WiFiSignal", "string"
command "initialize"
command "ReconnectMQTT"
}
preferences {
def device = [:]
device << ["ht" : "H & T"]
device << ["flood" : "Flood"]
input name: "MQTTBroker", type: "text", title: "MQTT Broker Address:", required: true
input name: "MQTTBrokerPort", type: "text", title: "MQTT Broker Port:", required: true
input name: "MQTTUsername", type: "text", title: "MQTT Username:", description: "(blank if none)"
input name: "MQTTPassword", type: "password", title: "MQTT Password:", description: "(blank if none)"
input ("DeviceType", "enum", title: "Device Mode", options: device, defaultValue: "ht")
input name: "shellyId", type: "text", title: "Your Shelly Device ID:", description: "EG; Y8AFD0", required: true
input name: "debugOutput", type: "bool", title: "Enable debug logging?", defaultValue: true
input name: "debugParse", type: "bool", title: "Enable JSON parse logging?", defaultValue: true
input name: "txtEnable", type: "bool", title: "Enable descriptionText logging", defaultValue: true
input("ip", "string", title:"Shelly IP Address", description:"Required to pull stats", defaultValue:"" , required: true)
input name: "username", type: "text", title: "Shelly Username:", description: "(blank if none)", required: false
input name: "password", type: "password", title: "Shelly Password:", description: "(blank if none)", required: false
input name: "Shellyinfo", type: "text", title: "<center><font color=blue>Info Box</font><br>Shelly API docs located</center>", description: "<center><a href='http://shelly-api-docs.shelly.cloud/'>[here]</a></center>"
}
}
def installed() {
log.info "installed..."
}
def setVersion(){
state.Version = "3.1.3"
state.InternalName = "ShellyHTMQTT"
}
def parse(String description) {
Date date = new Date();
topic = interfaces.mqtt.parseMessage(description).topic
topic = topic.substring(topic.lastIndexOf("/") + 1)
payload = interfaces.mqtt.parseMessage(description).payload
if (txtEnable) log.info topic
if (txtEnable) log.info payload
sendEvent(name: "${topic}", value: "${payload}", displayed: true)
state."${topic}" = "${payload}"
if (DeviceType == "flood") {
if (state.flood == "false") sendEvent (name: "water", value: "dry")
if (state.flood == "true") sendEvent (name: "water", value: "wet")
} else {
sendEvent (name: "water", value: "---")
}
if (state.act_reasons == '["sensor"]') {
for (i = 0; i <1; i++) { // checks only once
getSettings()
getStatus()
state.act_reasons = "0" // Now we change the reason to stop getting stats
}
}
if (debugOutput) sendEvent(name: "Last_Payload_Received", value: "Topic: ${topic}: ${payload} - ${date.toString()}", displayed: true)
state.LastUpdateTime = "${date.toString()}"
sendEvent (name: "LastUpdateTime", value: state.LastUpdateTime)
}
def updated() {
log.info "Preferences updated..."
log.warn "Debug logging is: ${debugOutput == true}"
log.warn "JSON parsing logging is: ${debugParse == true}"
log.warn "Description Text logging is: ${txtEnable == true}"
state.remove("Last_Update_Recieved")
unschedule()
runEvery15Minutes(status)
log.info "MQTT connection check set to 15 Minutes"
if (debugOutput) runIn(1800,logsOff)
if (txtEnable) runIn(600,txtOff)
if (debugParse) runIn(600,parseOff)
state.DeviceType = "$DeviceType"
dbCleanUp()
version()
ReconnectMQTT()
initialize()
}
def logsOff(){
log.warn "Debug logging disabled."
device.updateSetting("debugOutput",[value:"false",type:"bool"])
state.remove("MTQQbrokerURL")
state.remove("shellyId")
}
def parseOff(){
log.warn "Json logging auto disabled."
device.updateSetting("debugParse",[value:"false",type:"bool"])
}
def txtOff(){
log.warn "Description Text logging disabled."
device.updateSetting("txtEnable",[value:"false",type:"bool"])
}
private logDebug(msg) {
if (settings?.debugOutput || settings?.debugOutput == null) {
log.debug "$msg"
}
}
private logJSON(msg) {
if (settings?.debugParse || settings?.debugParse == null) {
log.info "$msg"
}
}
def ReconnectMQTT() {
log.info "Reconnecting MQTT"
uninstalled()
initialize()
}
def uninstalled() {
log.info "Disconnecting from MQTT"
interfaces.mqtt.disconnect()
}
def status() {
sendEvent (name: "MQTTconnected", value: "${interfaces.mqtt.isConnected() == true}")
if (txtEnable) "MQTT connected is: ${interfaces.mqtt.isConnected() == true}"
}
// Get shelly device type
def getSettings(){
logDebug "Get Shelly Settings"
def paramsSettings = [uri: "http://${username}:${password}@${ip}/settings"]
try {
httpGet(paramsSettings) {
respSettings -> respSettings.headers.each {
logJSON "ResponseSettings: ${it.name} : ${it.value}"
}
obsSettings = respSettings.data
logJSON "params: ${paramsSettings}"
logJSON "response contentType: ${respSettings.contentType}"
logJSON "response data: ${respSettings.data}"
state.DeviceType = obsSettings.device.type
state.ShellyHostname = obsSettings.device.hostname
if (state.DeviceType == "SHHT-1") sendEvent(name: "DeviceType", value: "Shelly H&T")
if (state.DeviceType == "SHWT-1") sendEvent(name: "DeviceType", value: "Shelly Flood")
updateDataValue("model", state.DeviceType)
updateDataValue("ShellyHostname", state.ShellyHostname)
updateDataValue("ShellyIP", obsSettings.wifi_sta.ip)
updateDataValue("ShellySSID", obsSettings.wifi_sta.ssid)
updateDataValue("manufacturer", "Allterco Robotics")
sendEvent(name: "Primary_SSID", value: obsSettings.wifi_sta.ssid)
sendEvent(name: "PrimaryIP", value: obsSettings.wifi_sta.ip)
sendEvent(name: "Secondary_WiFi", value: obsSettings.wifi_sta1.enabled)
sendEvent(name: "Secondary_SSID", value: obsSettings.wifi_sta1.ssid)
} // End try
} catch (e) {
log.error "something went wrong: $e"
}
} // End Refresh Status
def getStatus(){
logDebug "Shelly Status called"
getSettings()
def params = [uri: "http://${username}:${password}@${ip}/status"]
try {
httpGet(params) {
resp -> resp.headers.each {
logJSON "Response: ${it.name} : ${it.value}"
}
obs = resp.data
logJSON "params: ${params}"
logJSON "response contentType: ${resp.contentType}"
logJSON "response data: ${resp.data}"
state.mac = obs.mac
sendEvent(name: "MAC", value: state.mac)
updateDataValue("MAC", state.mac)
state.rssi = obs.wifi_sta.rssi
/*
-30 dBm Excellent | -67 dBm Good | -70 dBm Poor | -80 dBm Weak | -90 dBm Dead
*/
signal = state.rssi
if (signal <= 0 && signal >= -70) {
sendEvent(name: "WiFiSignal", value: "<font color='green'>Excellent</font>", isStateChange: true);
} else
if (signal < -70 && signal >= -80) {
sendEvent(name: "WiFiSignal", value: "<font color='green'>Good</font>", isStateChange: true);
} else
if (signal < -80 && signal >= -90) {
sendEvent(name: "WiFiSignal", value: "<font color='yellow'>Poor</font>", isStateChange: true);
} else
if (signal < -90 && signal >= -100) {
sendEvent(name: "WiFiSignal", value: "<font color='red'>Weak</font>", isStateChange: true);
}
// Device FW Updates
state.has_update = obs.has_update
if (state.has_update == true) {
if (txtEnable) log.info "sendEvent NEW SHELLY FIRMWARE"
sendEvent(name: "FW_Update_Needed", value: "<font color='red'>FIRMWARE Update Required</font>")
}
if (state.has_update == false) {
if (txtEnable) log.info "sendEvent Device FW is current"
sendEvent(name: "FW_Update_Needed", value: "<font color='green'>Device FW is current</font>")
}
// Cloud
state.cloud = obs.cloud.enabled
if (state.cloud == true) {
sendEvent(name: "Cloud", value: "<font color='green'>Enabled</font>")
} else {
sendEvent(name: "Cloud", value: "<font color='red'>Disabled</font>")
}
state.cloudConnected = obs.cloud.connected
if (state.cloudConnected == true) {
sendEvent(name: "Cloud_Connected", value: "<font color='green'>Connected</font>")
} else {
sendEvent(name: "Cloud_Connected", value: "<font color='red'>Not Connected</font>")
}
} // End try
} catch (e) {
log.error "something went wrong: $e"
}
} // End Refresh Status
def initialize() {
try {
mqttbroker = "tcp://" + settings?.MQTTBroker + ":" + settings?.MQTTBrokerPort
interfaces.mqtt.connect(mqttbroker, "hubitat" + settings?.shellyId, settings?.MQTTUsername,settings?.MQTTPassword)
if (debugOutput) {
state.MTQQbrokerURL= "${mqttbroker}"
state.shellyId = "${shellyId}"
} else {
state.remove("MTQQbrokerURL")
state.remove("shellyId")
}
//give it a chance to start
pauseExecution(1000)
logDebug "Subscribed to: ${"shellies/shelly" + settings?.DeviceType + "-" + settings?.shellyId + "/sensor/#"}"
interfaces.mqtt.subscribe("shellies/shelly" + settings?.DeviceType + "-" + settings?.shellyId + "/sensor/#")
} catch(e) {
log.info "MQTT Initialize error: ${e.message}"
}
}
def mqttClientStatus(String status) {
log.info "MQTT ${status}"
}
private dbCleanUp() {
state.remove("UpdateInfo")
state.remove("DriverUpdateInfo")
state.remove("DeviceUpdateInfo")
state.remove("Current")
state.remove("InternalName")
state.remove("version")
state.remove("Version")
state.remove("MTQQbrokerURL")
state.remove("Last_Update_Recieved")
state.remove("Last_Update")
state.remove("flood")
state.remove("water")
state.remove("DeviceType")
state.remove("devType")
state.remove("MQTTconnected")
}
def version(){
updatecheck()
schedule("0 0 18 1/1 * ? *", updatecheck) // Cron schedule
// schedule("0 0/1 * 1/1 * ? *", updatecheck) // Test Cron schedule
}
def updatecheck(){
setVersion()
def paramsUD = [uri: "https://raw.githubusercontent.com/ShellyUSA/Hubitat-Drivers/master/resources/version.json", contentType: "application/json; charset=utf-8"]
try {
httpGet(paramsUD) { respUD ->
logJSON " Version Checking - Response Data: ${respUD.data}" // Troubleshooting Debug Code - Uncommenting this line should show the JSON response from your webserver
def copyrightRead = (respUD.data.copyright)
state.Copyright = copyrightRead
def newVerRaw = (respUD.data.versions.Driver.(state.InternalName))
def newVer = (respUD.data.versions.Driver.(state.InternalName).replace(".", ""))
def currentVer = state.Version.replace(".", "")
state.DeviceUpdateInfo = (respUD.data.versions.UpdateInfo.Driver.(state.InternalName))
state.author = (respUD.data.author)
state.icon = (respUD.data.icon)
if(newVer == "NLS"){
state.Status = "<b>** This device handler is no longer supported by $state.author **</b>"
log.warn "** This driver is no longer supported by $state.author **"
}
else if(currentVer < newVer){
state.Status = "<b>New Version Available (Version: $newVerRaw)</b>"
log.warn "** There is a newer version of this device handler available (Version: $newVerRaw) **"
log.warn "** $state.DeviceUpdateInfo **"
}
else if(currentVer > newVer){
state.Status = "<b>You are using a Test version of this device handler $state.Version (Stable Version: $newVerRaw)</b>"
}
else{
state.Status = "Current"
log.info "You are using the current version of this device handler"
}
} // httpGet
} // try
catch (e) {
log.error "Something went wrong: CHECK THE JSON FILE AND IT'S URI - $e"
}
if(state.Status == "Current"){
state.DeviceUpdateInfo = "Up to date"
sendEvent(name: "DriverUpdate", value: state.DeviceUpdateInfo)
sendEvent(name: "DriverStatus", value: state.Status)
}
else {
sendEvent(name: "DriverUpdate", value: state.DeviceUpdateInfo)
sendEvent(name: "DriverStatus", value: state.Status)
}
sendEvent(name: "DriverAuthor", value: "sgrayban")
sendEvent(name: "DriverVersion", value: state.Version)
}