-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathha7net-child-temperature.groovy
87 lines (66 loc) · 2.48 KB
/
ha7net-child-temperature.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
def version() {'v0.2.0'}
metadata {
definition (name: 'HA7Net 1-Wire - Child - Temperature',
namespace: 'ckamps',
author: 'Christopher Kampmeier',
importUrl: 'https://raw.githubusercontent.com/ckamps/hubitat-drivers-ha7net/master/ha7net-child-temperature.groovy') {
capability 'TemperatureMeasurement'
capability 'Refresh'
}
preferences {
input name: 'offset', type: 'decimal', title: 'Temperature Offset', description: '-n, +n or n to adjust sensor reading', range:'*..*'
input name: 'logEnable', type: 'bool', title: 'Enable debug logging', defaultValue: false
}
}
def installed() {
initialize()
}
def updated() {
initialize()
}
def initialize() {
state.version = version()
}
def poll() {
refresh()
}
def refresh() {
sensorId = device.deviceNetworkId
if (logEnable) log.debug("Getting temperature for sensor: ${sensorId}")
try {
temp = getTemperature(sensorId)
}
catch (Exception e) {
log.warn("Can't obtain temperature for sensor ${sensorId}: ${e}")
return
}
if (logEnable) log.debug("Temperature - C: ${temp}")
temp = (location.temperatureScale == "F") ? ((temp * 1.8) + 32) : temp
temp = offset ? (temp + offset) : temp
temp = temp.round(2)
sendEvent(
name: 'temperature',
value: temp,
unit: "°${location.temperatureScale}",
descriptionText: "Temperature is ${temp}°${location.temperatureScale}",
translatable: true
)
def nowDay = new Date().format('MMM dd', location.timeZone)
def nowTime = new Date().format('h:mm a', location.timeZone)
sendEvent(
name: 'lastUpdated',
value: nowDay + " at " + nowTime,
displayed: false
)
}
private float getTemperature(sensorId) {
def uri = "http://${parent.getHa7netAddress()}"
def body = [Address_Array: sensorId]
def path = '/1Wire/ReadTemperature.html'
response = parent.doHttpPost(uri, path, body)
if (!response) throw new Exception("doHttpPost to get temperature returned empty response ${sensorId}")
element = response.'**'.find{ it.@name == 'Temperature_0' }
if (!element) throw new Exception("Can't find Temperature_0 element in response from HA7Net for sensor ${sensorId}")
if (!element.@value) throw new Exception("Empty value in Temperature_0 element in response from HA7Net for sensor ${sensorId}")
return([email protected]())
}