Skip to content

Commit 2470eeb

Browse files
committed
Update device handlers
1 parent 12f8a81 commit 2470eeb

12 files changed

+432
-1610
lines changed

devicetypes/gupta/tasmota.src/TasmotaContactSensor.groovy

Lines changed: 107 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* Authors
55
* - sandeep gupta
66
*
7+
* Version 1.0 - 11/17/2019
8+
*
79
* Copyright 2019
810
*
911
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
@@ -20,6 +22,7 @@ metadata {
2022

2123
definition (name: "Tasmota Contact Sensor", namespace: "gupta", author: "Sandeep Gupta") {
2224
capability "Contact Sensor"
25+
capability "Actuator"
2326
capability "Refresh"
2427

2528
command "open"
@@ -29,7 +32,9 @@ metadata {
2932
attribute "update", "string"
3033
attribute "device_details", "string"
3134
attribute "details", "string"
32-
attribute "rssi", "string"
35+
attribute "wifi", "string"
36+
attribute "rssiLevel", "number"
37+
attribute "healthStatus", "string"
3338
}
3439

3540
simulator {
@@ -45,39 +50,44 @@ metadata {
4550
}
4651

4752
tileAttribute("device.device_details", key: "SECONDARY_CONTROL") {
48-
attributeState("device_details", action: "refresh", label: '${currentValue}', icon:"st.secondary.refresh-icon")
53+
attributeState("default", action: "refresh", label: '${currentValue}', icon:"st.secondary.refresh-icon")
4954
attributeState("refresh", label: 'Updating data from server...')
5055
}
5156
}
5257

53-
standardTile("contactclosed", "device.contact", width: 2, height: 2) {
58+
standardTile("contactclosed", "device.contact", width: 2, height: 2, decoration: "flat") {
5459
state("closed", label:'CLOSE', icon:"st.contact.contact.closed", backgroundColor:"#79b821", action: "closed")
5560
state("open", label:'CLOSE', icon:"st.contact.contact.closed", backgroundColor:"#79b821", action: "closed")
5661
}
5762

58-
standardTile("contactopen", "device.contact", width: 2, height: 2) {
63+
standardTile("contactopen", "device.contact", width: 2, height: 2, decoration: "flat") {
5964
state("closed", label:'OPEN', icon:"st.contact.contact.open", backgroundColor:"#ffa81e", action: "open")
6065
state("open", label:'OPEN', icon:"st.contact.contact.open", backgroundColor:"#ffa81e", action: "open")
6166
}
6267

63-
valueTile("rssi", "device.rssi", width: 2, height: 2) {
64-
state ("rssi", label: '${currentValue}', backgroundColors:[
65-
[value: 100, color: "#44b621"],
66-
[value: 85, color: "#90d2a7"],
67-
[value: 68, color: "#1e9cbb"],
68-
[value: 51, color: "#153591"],
69-
[value: 34, color: "#f1d801"],
70-
[value: 17, color: "#d04e00"],
71-
[value: 0, color: "#bc2323"]
72-
])
68+
valueTile("wifi", "device.wifi", width: 1, height: 1, decoration: "flat") {
69+
state ("default", label: '${currentValue}', backgroundColor: "#e86d13", icon: "https://github.com/sgupta999/GuptaSmartthingsRepository/raw/master/icons/blank1x1-orange.png")
70+
}
71+
72+
standardTile("rssiLevel", "device.rssiLevel", width: 1, height: 1, decoration: "flat") {
73+
state ("1", icon: "https://github.com/sgupta999/GuptaSmartthingsRepository/raw/master/icons/wifi0.png")
74+
state ("2", icon: "https://github.com/sgupta999/GuptaSmartthingsRepository/raw/master/icons/wifi1.png")
75+
state ("3", icon: "https://github.com/sgupta999/GuptaSmartthingsRepository/raw/master/icons/wifi2.png")
76+
state ("4", icon: "https://github.com/sgupta999/GuptaSmartthingsRepository/raw/master/icons/wifi3.png")
7377
}
7478

75-
valueTile("details", "device.details", width: 6, height: 2) {
76-
state "details", label: '${currentValue}', backgroundColor: "#ffffff"
79+
standardTile("healthStatus", "device.healthStatus", width: 2, height: 1, decoration: "flat") {
80+
state "default", icon: "https://github.com/sgupta999/GuptaSmartthingsRepository/raw/master/icons/online1x2.png"
81+
state "online", icon: "https://github.com/sgupta999/GuptaSmartthingsRepository/raw/master/icons/online1x2.png"
82+
state "offline", icon: "https://github.com/sgupta999/GuptaSmartthingsRepository/raw/master/icons/offline1x2.png"
83+
}
84+
85+
valueTile("details", "device.details", width: 6, height: 2, decoration: "flat") {
86+
state "default", label: '${currentValue}', icon: "https://github.com/sgupta999/GuptaSmartthingsRepository/raw/master/icons/blank1x3-green.png", backgroundColor: "#90d2a7"
7787
}
7888

7989
main "main"
80-
details(["main", "contactopen", "rssi", "contactclosed","details" ])
90+
details(["main", "contactopen","healthStatus", "contactclosed","wifi", "rssiLevel","details" ])
8191
}
8292
}
8393

@@ -93,6 +103,46 @@ def parse(String description) {
93103
break;
94104
}
95105
}
106+
def installed() {
107+
configure()
108+
refresh()
109+
}
110+
111+
def refresh(){
112+
sendEvent(name : "update", value : 'refresh', isStateChange: 'true', descriptionText : 'Refreshing from Server...');
113+
log.debug "Sent 'refresh' command for device: ${device}"
114+
}
115+
116+
def ping(){
117+
return ((device.currentValue('healthStatus')?: "offline") == "online")
118+
}
119+
120+
def configure() {
121+
log.trace "Executing 'configure'"
122+
sendEvent(name: "DeviceWatch-Enroll", value: [protocol: "cloud", scheme:"untracked"].encodeAsJson(), displayed: false)
123+
markDeviceOnline()
124+
initialize()
125+
}
126+
127+
def markDeviceOnline() {
128+
state.pingState = 'online';
129+
setDeviceHealth("online")
130+
}
131+
132+
def markDeviceOffline() {
133+
state.pingState = 'offline';
134+
setDeviceHealth("offline")
135+
}
136+
137+
private setDeviceHealth(String healthState) {
138+
log.debug("healthStatus: ${device.currentValue('healthStatus')}; DeviceWatch-DeviceStatus: ${device.currentValue('DeviceWatch-DeviceStatus')}")
139+
// ensure healthState is valid
140+
List validHealthStates = ["online", "offline"]
141+
healthState = validHealthStates.contains(healthState) ? healthState : device.currentValue("healthStatus")
142+
// set the healthState
143+
sendEvent(name: "DeviceWatch-DeviceStatus", value: healthState)
144+
sendEvent(name: "healthStatus", value: healthState)
145+
}
96146

97147
def processMQTT(attribute, value){
98148
//log.debug "Processing ${attribute} Event: ${value} from MQTT for device: ${device}"
@@ -107,57 +157,54 @@ def processMQTT(attribute, value){
107157

108158
def updateTiles(Object val ){
109159
//log.debug "Msg ${val}"
110-
state.pingState = (state?.pingState == null) ? 'ONLINE': state.pingState;
111-
if ((val == 'Online') || (val == 'Offline')){
160+
if (['online','offline'].contains(val.toLowerCase())){
112161
log.debug "Received Health Check LWT event ${val}"
113-
if (val == 'Online') {
114-
state.pingState = 'ONLINE';
115-
sendEvent(name: 'rssi', value: state.RSSI+ "%\n" + state.ssid1 + "\n["+ state.pingState + "]", isStateChange : 'true', displayed: 'false')
116-
} else {
117-
state.pingState = 'OFFLINE';
118-
sendEvent(name: 'rssi', value: "0" + "%\n" + state.ssid1 + "\n["+ state.pingState + "]", isStateChange : 'true', displayed: 'false')
119-
}
162+
(val.toLowerCase() == 'online') ? markDeviceOnline() : markDeviceOffline()
120163
return;
121-
}
122-
state.updates = (state?.updates == null) ? "" : state.updates + val + "\n";
164+
}
165+
166+
state.updates = (state.updates == null) ? "" : state.updates + val + "\n";
123167
def value = parseJson(val);
124-
if (state?.update1 == null) state.update1 = false
125-
if (state?.update2 == null) state.update2 = false
126-
if (state?.update3 == null) state.update3 = false
127-
if (state?.update4 == null) state.update4 = false
128-
if (value?.Status != null) state.update1 = true else
129-
if (value?.StatusFWR != null) state.update2 = true else
130-
if (value?.StatusNET != null) state.update3 = true else
131-
if (value?.StatusSTS != null) state.update4 = true;
132-
(value?.Status != null)
133-
state.topic = (value?.Status?.Topic != null) ? value?.Status?.Topic : state.topic
134-
state.friendlyName = (value?.Status?.FriendlyName != null) ? value?.Status?.FriendlyName : state.friendlyName
135-
state.firmware = (value?.StatusFWR?.Version != null) ? value?.StatusFWR?.Version : state.firmware
136-
state.macAddress = ( value?.StatusNET?.Mac != null) ? value?.StatusNET?.Mac : state.macAddress
137-
state.ipAddress = (value?.StatusNET?.IPAddress != null) ? value?.StatusNET?.IPAddress : state.ipAddress
138-
if (value?.StatusSTS?.Time != null) state.currentTimestamp = Date.parse("yyyy-MM-dd'T'HH:mm:ss",value?.StatusSTS?.Time).format("EEE MMM dd, yyyy 'at' hh:mm:ss a")
139-
state.ssid1 = (value?.StatusSTS?.Wifi?.SSId != null) ? value?.StatusSTS?.Wifi?.SSId : state.ssid1
140-
state.upTime = (value?.StatusSTS?.Uptime != null) ? value?.StatusSTS?.Uptime : state.upTime
141-
state.RSSI = (value?.StatusSTS?.Wifi?.RSSI != null) ? value?.StatusSTS?.Wifi?.RSSI : state.RSSI
142-
//log.debug "Are updates ready ${state.update1}, ${state.update2}, ${state.update3}, ${state.update4}"
168+
169+
state.update1 = value?.Status ? true : state.update1 ?: false
170+
state.update2 = value?.StatusFWR ? true : state.update2 ?: false
171+
state.update3 = value?.StatusNET ? true : state.update3 ?: false
172+
state.update4 = value?.StatusSTS ? true : state.update4 ?: false
173+
174+
state.topic = (value?.Status?.Topic) ?: state.topic
175+
state.friendlyName = (value?.Status?.FriendlyName) ?: state.friendlyName
176+
state.firmware = (value?.StatusFWR?.Version) ?: state.firmware
177+
state.macAddress = ( value?.StatusNET?.Mac) ?: state.macAddress
178+
state.ipAddress = (value?.StatusNET?.IPAddress) ?: state.ipAddress
179+
if (value?.StatusSTS?.Time) state.currentTimestamp = Date.parse("yyyy-MM-dd'T'HH:mm:ss",value?.StatusSTS?.Time).format("EEE MMM dd, yyyy 'at' hh:mm:ss a")
180+
state.ssid1 = (value?.StatusSTS?.Wifi?.SSId) ?: state.ssid1
181+
state.upTime = (value?.StatusSTS?.Uptime) ?: state.upTime
182+
state.RSSI = (value?.StatusSTS?.Wifi?.RSSI) ?: state.RSSI
183+
state.rssiLevel = (value?.StatusSTS?.Wifi?.RSSI) ? (0..10).contains(state.RSSI) ? 1
184+
: (11..45).contains(state.RSSI)? 2
185+
: (46..80).contains(state.RSSI)? 3
186+
: (81..100).contains(state.RSSI) ? 4 : 5
187+
: state.rssiLevel
188+
189+
//log.debug "Are updates ready ${state.update1}, ${state.update2}, ${state.update3}, ${state.update4}"
143190
//log.debug "Time is ${state.currentTimestamp}"
144191
if (state.update1 && state.update2 && state.update3 && state.update4){
145-
sendEvent(name: 'device_details', value: state.topic + ", running for: " + state.upTime +
146-
"\nIP: " + state.ipAddress + " [ " + state.ssid1+": "+state.RSSI + "% ]", isStateChange : 'true', displayed: 'false')
147-
sendEvent(name: 'details', value: state.topic + "\n" + state.friendlyName + "\n" + state.ipAddress + " [ " +state.macAddress + " ]\n" + state.firmware +
148-
" - Up Time: " + state.upTime + "\nLast updated: " + state.currentTimestamp , isStateChange : 'true', displayed: 'false')
149-
sendEvent(name: 'rssi', value: state.RSSI+ "%\n" + state.ssid1 + "\n["+ state.pingState + "]", isStateChange : 'true', displayed: 'false')
150-
log.debug "Refresh is [${state.refresh}]. Processed Status updates for device: [${device}]\n ${state.updates}"
151-
state.updates = "";
152-
state.update1 = state.update2 = state.update3 = state.update4 = false;
153-
state.refresh = false
192+
runIn(3,fireEvents)
154193
}
155194
}
156195

157-
def refresh(){
158-
state.refresh = true;
159-
sendEvent(name : "update", value : 'refresh', isStateChange : 'true', descriptionText : 'Refreshing from Server...');
160-
log.debug "Sent 'refresh' command for device: ${device}"
196+
def fireEvents(){
197+
sendEvent(name: 'device_details', value: state.topic + ", running for: " + state.upTime +
198+
"\nIP: " + state.ipAddress + " [ " + state.ssid1+": "+state.RSSI + "% ]", displayed: 'false')
199+
sendEvent(name: 'details', value: state.topic + "\n" + state.friendlyName + "\n" + state.ipAddress + " [ " +state.macAddress + " ]\n" +
200+
state.firmware + " - Up Time: " + state.upTime + "\nLast Updated: " + state.currentTimestamp +"\n\n" , displayed: 'false')
201+
//sendEvent(name: 'healthStatus', value: (state.pingState?:'online') , displayed: 'false')
202+
(state.RSSI < 100) ? sendEvent(name: 'wifi', value: state.RSSI +"%\nRSSI\n\n", displayed: 'false')
203+
: sendEvent(name: 'wifi', value: state.RSSI +"%\nRSSI\n\n\n", displayed: 'false')
204+
sendEvent(name: 'rssiLevel', value: state.rssiLevel, displayed: 'false')
205+
log.debug "Processed Status updates for device: [${device}]\n ${state.updates}"
206+
state.updates = "";
207+
state.update1 = state.update2 = state.update3 = state.update4 = false;
161208
}
162209

163210
def open(){

0 commit comments

Comments
 (0)