-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLogitechGHUB_BatterySensorParent_Driver.groovy
140 lines (118 loc) · 5.04 KB
/
LogitechGHUB_BatterySensorParent_Driver.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
/*
* Logitech G-Hub Battery Sensor Parent Driver
*
* Gets battery levels for Logitech G-Hub Devices connected to a PC / laptop
*
*/
metadata {
definition(name: 'G-Hub Battery Parent Driver', namespace: 'SIMNET', author: 'sburke781') {
capability 'Actuator'
attribute 'lastupdate', 'date'
command 'refresh'
command 'poll'
}
}
preferences {
input 'IP', 'text', title: 'IP Address of PC / Laptop', required: true
input 'Port', 'number', title: 'Port configured in LGSTrayBattery', required: true, defaultValue: 12321
input 'autoPoll', 'bool', required: true, title: 'Enable Auto Poll', defaultValue: false
input 'pollInterval', 'text', title: 'Poll interval (minutes)', required: true, defaultValue: '10'
input(name: 'DebugLogging', type: 'bool', title:'Enable Debug Logging', displayDuringSetup: true, defaultValue: false)
input(name: 'WarnLogging', type: 'bool', title:'Enable Warning Logging', displayDuringSetup: true, defaultValue: true)
input(name: 'ErrorLogging', type: 'bool', title:'Enable Error Logging', displayDuringSetup: true, defaultValue: true)
input(name: 'InfoLogging', type: 'bool', title:'Enable Description Text (Info) Logging', displayDuringSetup: true, defaultValue: false)
}
//void installed() { }
void updated() {
debugLog("updated: AutoPolling = ${autoPoll}, StatusPollingInterval = ${pollInterval}")
updatePolling()
}
void poll(){
getBatteryReadings()
}
void refresh() {
retrieveBatterySensorDevices()
poll()
}
void retrieveBatterySensorDevices() {
debugLog('retrieveBatterySensorDevices: Refreshing G-Hub Battery Sensor List')
def getParams = [
uri: "http://${IP}:${Port}/devices",
headers: ['User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36', 'Accept': 'text/xml'],
contentType: 'text/plain',
textParser: true
]
try {
httpGet(getParams) { resp ->
String htmlResponse = groovy.xml.XmlUtil.escapeXml(resp.data.text)
htmlResponse = htmlResponse.replace('<html>', '').replace('</html>', '').replace('<br>', '')
String[] devices = htmlResponse.split('</a>')
for (int i = 0; i < devices.length; i++) {
String deviceName = "${devices[i].split(' : ')[0]}";
String deviceId = "${devices[i].split(' : ')[1].replace('<a href=\'', '').split('>')[1]}"
debugLog("retrieveBatterySensorDevices: Id = ${deviceId}, Name = ${deviceName}")
childDevice = findChildDevice("ghub_${deviceId}")
if (childDevice == null) {
childDevice = addChildDevice('simnet', 'Virtual Battery Sensor', "ghub_${deviceId}", [label: "${deviceName} Battery Sensor"])
}
}
}
} catch (Exception e) {
warnLog("refresh: call to update G-Hub device battery levels failed: ${e}")
}
}
void getBatteryReadings() {
for (device in getChildDevices()) {
def getParams = [
uri: "http://${IP}:${Port}/device/${device.deviceNetworkId.replace('ghub_', '')}",
headers: ['User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36', 'Accept': 'text/xml'],
contentType: 'text/xml'
]
try {
httpGet(getParams) { resp ->
String batteryPC = resp.data.battery_percent.text()
String deviceId = resp.data.device_id.text()
String deviceName = resp.data.device_name.text()
debugLog("getBatteryReadings: ${deviceName}(${deviceId}) - ${batteryPC}%")
if (batteryPC != 'NaN' && batteryPC != '0.00') { device.setBattery(new BigDecimal(batteryPC)) }
}
} catch (Exception e) {
log.warn "getBatteryReadings: call to update G-Hub device battery levels failed: ${e}"
}
}
}
def findChildDevice(childDeviceId) {
getChildDevices()?.find { it.deviceNetworkId == "${childDeviceId}" }
}
//Utility methods
void debugLog(String debugMessage) {
if (DebugLogging == true) { log.debug(debugMessage) }
}
void errorLog(String errorMessage) {
if (ErrorLogging == true) { log.error(errorMessage) }
}
void infoLog(String infoMessage) {
if(InfoLogging == true) { log.info(infoMessage) }
}
void warnLog(String warnMessage) {
if (WarnLogging == true) { log.warn(warnMessage) }
}
void updatePolling() {
String sched = ''
debugLog('updatePolling: Updating Polling called, about to unschedule poll')
unschedule('poll')
debugLog('updatePolling: Unscheduleing poll complete')
if (autoPoll == true) {
sched = "0 0/${pollInterval} * ? * * *"
debugLog("updatePolling: Setting up schedule with settings: schedule(\"${sched}\",poll)")
try {
schedule("${sched}", 'poll')
}
catch (Exception e) {
errorLog('updatePolling: Error - ' + e)
}
infoLog('updatePolling: Scheduled poll set')
}
else { infoLog('updatePolling: Automatic polling disabled') }
}
void getSchedule() { }