forked from jason0x43/hubitat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jason0x43-nest_thermostat.groovy
277 lines (233 loc) · 6.18 KB
/
jason0x43-nest_thermostat.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
/**
* A simple Nest thermostat driver
*
* Author: Jason Cheatham
* Last updated: 2018-12-13, 08:23:39-0500
*/
metadata {
definition(name: 'Nest Thermostat', namespace: 'jason0x43', author: 'jason0x43') {
capability 'Relative Humidity Measurement'
capability 'Thermostat'
capability 'Temperature Measurement'
capability 'Sensor'
capability 'Refresh'
command 'eco'
command 'sunblockOn'
command 'sunblockOff'
command 'away'
command 'home'
command 'setScale', ['enum']
attribute 'away', 'boolean'
attribute 'sunblockEnabled', 'boolean'
attribute 'sunblockActive', 'boolean'
attribute 'scale', 'enum', ['f', 'c']
}
preferences {
input(
name: 'pollInterval',
type: 'enum',
title: 'Update interval (in minutes)',
options: ['5', '10', '30'],
required: true
)
}
}
def auto() {
log.debug 'auto()'
nestPut([hvac_mode: 'heat-cool'])
refresh()
}
def away() {
log.debug 'away()'
parent.setAway(true)
refresh(true)
}
def cool() {
log.debug 'cool()'
nestPut([hvac_mode: 'cool'])
refresh()
}
def eco() {
log.debug 'eco()'
nestPut([hvac_mode: 'eco'])
refresh()
}
def emergencyHeat() {
log.debug 'emergencyHeat is not implemented'
}
def fanAuto() {
log.debug 'fanAuto()'
nestPut([hvac_mode: 'heat'])
refresh()
}
def fanCirculate() {
log.debug 'fanCirculate()'
nestPut([
fan_timer_duration: 15,
fan_timer_active: true
])
refresh()
}
def fanOn() {
log.debug 'Nest only supports "auto" and "circulate"'
}
def heat() {
log.debug 'heat()'
nestPut([hvac_mode: 'heat'])
refresh()
}
def home() {
log.debug 'home()'
parent.setAway(false)
refresh(false)
}
def off() {
log.debug 'off()'
nestPut([hvac_mode: 'off'])
refresh()
}
def setCoolingSetpoint(target) {
log.debug "setCoolingSetpoint(${target})"
setTargetTemp(target, 'cool')
}
def setHeatingSetpoint(target) {
log.debug "setHeatingSetpoint(${target})"
setTargetTemp(target, 'heat')
}
def setScale(scale) {
log.debug "setScale(${scale})"
scale = scale ? scale.toLowerCase() : null
if (scale != 'f' && scale != 'c') {
log.error "Invalid scale ${scale}"
return
}
nestPut([temperature_scale: scale])
refresh()
}
def setSchedule(schedule) {
log.debug "setSchedule(${schedule})"
}
def setThermostatFanMode(mode) {
log.debug "setThermostatFanMode(${mode})"
}
def setThermostatMode(mode) {
log.debug "setThermostatMode(${mode})"
}
def sunblockOff() {
log.debug 'sunblockOff()'
nestPut([sunlight_correction_enabled: false])
refresh()
}
def sunblockOn() {
log.debug 'sunblockOn()'
nestPut([sunlight_correction_enabled: true])
refresh()
}
def updated() {
log.debug 'Updated'
// log.trace('Unscheduling poll timer')
unschedule()
if (pollInterval == '5') {
// log.trace "Polling every 5 minutes"
runEvery5Minutes(refresh)
} else if (pollInterval == '10') {
// log.trace "Polling every 10 minutes"
runEvery10Minutes(refresh)
} else if (pollInterval == '30') {
// log.trace "Polling every 30 minutes"
runEvery30Minutes(refresh)
}
}
def parse(description) {
log.debug 'Received event: ' + description
}
def refresh(isAway) {
log.debug 'Refreshing'
if (isAway != null) {
updateState([away: isAway, triesLeft: 3])
} else {
updateState()
}
}
private nestPut(data) {
def id = getDataValue('nestId')
parent.nestPut("/devices/thermostats/${id}", data)
}
private setTargetTemp(temp, heatOrCool) {
def id = getDataValue('nestId')
def mode = device.currentValue('thermostatMode')
if (mode != heatOrCool && mode != 'heat-cool') {
log.debug "Not ${heatOrCool}ing"
return
}
def value = temp.toInteger()
def scale = device.currentValue('scale')
if (mode == 'heat-cool') {
if (heatOrCool == 'cool') {
nestPut(["target_temperature_high_${scale}": value])
} else {
nestPut(["target_temperature_low_${scale}": value])
}
} else {
nestPut(["target_temperature_${scale}": value])
}
refresh()
}
private updateState(args) {
def id = getDataValue('nestId')
def data = parent.nestGet("/devices/thermostats/${id}")
if (data == null) {
log.error 'Got null data from parent'
return
}
// If the thermostat mode doesn't agree with the 'away' state, wait a few
// seconds and update again
if (
args &&
(
args.away == true && data.hvac_mode != 'eco' ||
args.away == false && data.hvac_mode == 'eco'
) &&
args.triesLeft > 0
) {
// log.trace "Device hasn't updated for away yet, retrying"
runIn(3, 'updateState', [data: [
away: args.away,
triesLeft: args.triesLeft - 1
]])
return
}
def scale = data.temperature_scale.toLowerCase()
def away = parent.isAway()
// log.trace "data: ${data}"
sendEvent(name: 'away', value: away)
sendEvent(name: 'thermostatMode', value: data.hvac_mode)
sendEvent(name: 'humidity', value: data.humidity)
sendEvent(
name: 'thermostatFanMode',
value: data.fan_timer_active ? 'circulate' : 'auto'
)
sendEvent(name: 'scale', value: scale)
sendEvent(name: 'sunblockEnabled', value: data.sunlight_correction_enabled)
sendEvent(name: 'sunblockActive', value: data.sunlight_correction_active)
sendEvent(name: 'temperature', value: data["ambient_temperature_${scale}"])
sendEvent(name: 'temperatureUnit', value: data.temperature_scale)
sendEvent(name: 'nestPresence', value: away ? 'away' : 'home')
sendEvent(name: 'hasLeaf', value: data.has_leaf)
def state = data.hvac_state == 'off' ? 'idle' : data.hvac_state
sendEvent(name: 'thermostatOperatingState', value: state)
// log.trace "thermostatMode: ${data.hvac_mode}"
if (data.hvac_mode == 'heat') {
// log.trace 'setting heating setpoint to ' + data["target_temperature_${scale}"]
sendEvent(name: 'heatingSetpoint', value: data["target_temperature_${scale}"])
} else if (data.hvac_mode == 'cool') {
// log.trace 'setting cooling setpoint to ' + data["target_temperature_${scale}"]
sendEvent(name: 'coolingSetpoint', value: data["target_temperature_${scale}"])
} else if (data.hvac_mode == 'eco') {
sendEvent(name: 'heatingSetpoint', value: data["eco_temperature_low_${scale}"])
sendEvent(name: 'coolingSetpoint', value: data["eco_temperature_high_${scale}"])
} else if (data.hvac_mode == 'heat-cool') {
sendEvent(name: 'heatingSetpoint', value: data["target_temperature_low_${scale}"])
sendEvent(name: 'coolingSetpoint', value: data["target_temperature_high_${scale}"])
}
}