-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecobeehvacmode.go
494 lines (430 loc) · 18.8 KB
/
ecobeehvacmode.go
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/spf13/viper"
// Shortening the import reference name seems to make it a bit easier
owm "github.com/briandowns/openweathermap"
)
// Go and REST
// http://networkbit.ch/golang-http-client/
// Go and OpenWeatherMap
// http://briandowns.github.io/openweathermap/
// Up to 60 calls a minute
const tokenUri = "https://api.ecobee.com/token"
const thermostatURI = "https://api.ecobee.com/1/thermostat?format=json"
// API Key from Ecobee
var apiKey string = getConfValue("API_KEY", "")
// Ecobee has a fairly consistent refreshtoken once it's set up. Set this below.
var refreshTokenConst = getConfValue("REFRESHTOKEN", "")
// File location for the refreshtoken. We'll keep track of it via file.
var refreshTokenFile = getConfValue("REFRESHTOKENFILE", "/etc/ecobeehvacmode/refreshtoken")
// Open Weather Map API Key
var owmApiKey = getConfValue("OWM_API_KEY", "")
// Open Weather Map location
var weatherLocation = getConfValue("OWM_WEATHER_LOCATION", "")
// If running in w mode, when to lockout furnace vs heat pump
var furnaceLockoutTempC, _ = strconv.ParseFloat(getConfValue("FURNACE_LOCKOUT_TEMP", ""), 64)
var heatpumpLockoutTempC, _ = strconv.ParseFloat(getConfValue("HEATPUMP_LOCKOUT_TEMP", ""), 64)
type PinResponse struct {
EcobeePin string `json:"ecobeePin"`
Code string `json:"code"`
Interval string `json:"interval"`
ExpiresIn int `json:"expires_in"`
Scope string `json:"scope"`
}
type TokenResponse struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
RefreshToken string `json:"refresh_token"`
ExpiresIn int `json:"expires_in"`
Scope string `json:"scope"`
}
type StatusResponse struct {
ThermostatCount int `json:"thermostatCount"`
RevisionList []string `json:"revisionList"`
StatusList []string `json:"statusList"`
Status Status
}
type Status struct {
Code int `json:"code"`
Message string `json:"message"`
}
type ThermostatResponse struct {
Page struct {
Page int `json:"page"`
TotalPages int `json:"totalPages"`
PageSize int `json:"pageSize"`
Total int `json:"total"`
} `json:"page"`
ThermostatList []struct {
Identifier string `json:"identifier"`
Name string `json:"name"`
ThermostatRev string `json:"thermostatRev"`
IsRegistered bool `json:"isRegistered"`
ModelNumber string `json:"modelNumber"`
Brand string `json:"brand"`
Features string `json:"features"`
LastModified string `json:"lastModified"`
ThermostatTime string `json:"thermostatTime"`
UtcTime string `json:"utcTime"`
Settings struct {
HvacMode string `json:"hvacMode"`
LastServiceDate string `json:"lastServiceDate"`
ServiceRemindMe bool `json:"serviceRemindMe"`
MonthsBetweenService int `json:"monthsBetweenService"`
RemindMeDate string `json:"remindMeDate"`
Vent string `json:"vent"`
VentilatorMinOnTime int `json:"ventilatorMinOnTime"`
ServiceRemindTechnician bool `json:"serviceRemindTechnician"`
EiLocation string `json:"eiLocation"`
ColdTempAlert int `json:"coldTempAlert"`
ColdTempAlertEnabled bool `json:"coldTempAlertEnabled"`
HotTempAlert int `json:"hotTempAlert"`
HotTempAlertEnabled bool `json:"hotTempAlertEnabled"`
CoolStages int `json:"coolStages"`
HeatStages int `json:"heatStages"`
MaxSetBack int `json:"maxSetBack"`
MaxSetForward int `json:"maxSetForward"`
QuickSaveSetBack int `json:"quickSaveSetBack"`
QuickSaveSetForward int `json:"quickSaveSetForward"`
HasHeatPump bool `json:"hasHeatPump"`
HasForcedAir bool `json:"hasForcedAir"`
HasBoiler bool `json:"hasBoiler"`
HasHumidifier bool `json:"hasHumidifier"`
HasErv bool `json:"hasErv"`
HasHrv bool `json:"hasHrv"`
CondensationAvoid bool `json:"condensationAvoid"`
UseCelsius bool `json:"useCelsius"`
UseTimeFormat12 bool `json:"useTimeFormat12"`
Locale string `json:"locale"`
Humidity string `json:"humidity"`
HumidifierMode string `json:"humidifierMode"`
BacklightOnIntensity int `json:"backlightOnIntensity"`
BacklightSleepIntensity int `json:"backlightSleepIntensity"`
BacklightOffTime int `json:"backlightOffTime"`
SoundTickVolume int `json:"soundTickVolume"`
SoundAlertVolume int `json:"soundAlertVolume"`
CompressorProtectionMinTime int `json:"compressorProtectionMinTime"`
CompressorProtectionMinTemp int `json:"compressorProtectionMinTemp"`
Stage1HeatingDifferentialTemp int `json:"stage1HeatingDifferentialTemp"`
Stage1CoolingDifferentialTemp int `json:"stage1CoolingDifferentialTemp"`
Stage1HeatingDissipationTime int `json:"stage1HeatingDissipationTime"`
Stage1CoolingDissipationTime int `json:"stage1CoolingDissipationTime"`
HeatPumpReversalOnCool bool `json:"heatPumpReversalOnCool"`
FanControlRequired bool `json:"fanControlRequired"`
FanMinOnTime int `json:"fanMinOnTime"`
HeatCoolMinDelta int `json:"heatCoolMinDelta"`
TempCorrection int `json:"tempCorrection"`
HoldAction string `json:"holdAction"`
HeatPumpGroundWater bool `json:"heatPumpGroundWater"`
HasElectric bool `json:"hasElectric"`
HasDehumidifier bool `json:"hasDehumidifier"`
DehumidifierMode string `json:"dehumidifierMode"`
DehumidifierLevel int `json:"dehumidifierLevel"`
DehumidifyWithAC bool `json:"dehumidifyWithAC"`
DehumidifyOvercoolOffset int `json:"dehumidifyOvercoolOffset"`
AutoHeatCoolFeatureEnabled bool `json:"autoHeatCoolFeatureEnabled"`
WifiOfflineAlert bool `json:"wifiOfflineAlert"`
HeatMinTemp int `json:"heatMinTemp"`
HeatMaxTemp int `json:"heatMaxTemp"`
CoolMinTemp int `json:"coolMinTemp"`
CoolMaxTemp int `json:"coolMaxTemp"`
HeatRangeHigh int `json:"heatRangeHigh"`
HeatRangeLow int `json:"heatRangeLow"`
CoolRangeHigh int `json:"coolRangeHigh"`
CoolRangeLow int `json:"coolRangeLow"`
UserAccessCode string `json:"userAccessCode"`
UserAccessSetting int `json:"userAccessSetting"`
AuxRuntimeAlert int `json:"auxRuntimeAlert"`
AuxOutdoorTempAlert int `json:"auxOutdoorTempAlert"`
AuxMaxOutdoorTemp int `json:"auxMaxOutdoorTemp"`
AuxRuntimeAlertNotify bool `json:"auxRuntimeAlertNotify"`
AuxOutdoorTempAlertNotify bool `json:"auxOutdoorTempAlertNotify"`
AuxRuntimeAlertNotifyTechnician bool `json:"auxRuntimeAlertNotifyTechnician"`
AuxOutdoorTempAlertNotifyTechnician bool `json:"auxOutdoorTempAlertNotifyTechnician"`
DisablePreHeating bool `json:"disablePreHeating"`
DisablePreCooling bool `json:"disablePreCooling"`
InstallerCodeRequired bool `json:"installerCodeRequired"`
DrAccept string `json:"drAccept"`
IsRentalProperty bool `json:"isRentalProperty"`
UseZoneController bool `json:"useZoneController"`
RandomStartDelayCool int `json:"randomStartDelayCool"`
RandomStartDelayHeat int `json:"randomStartDelayHeat"`
HumidityHighAlert int `json:"humidityHighAlert"`
HumidityLowAlert int `json:"humidityLowAlert"`
DisableHeatPumpAlerts bool `json:"disableHeatPumpAlerts"`
DisableAlertsOnIdt bool `json:"disableAlertsOnIdt"`
HumidityAlertNotify bool `json:"humidityAlertNotify"`
HumidityAlertNotifyTechnician bool `json:"humidityAlertNotifyTechnician"`
TempAlertNotify bool `json:"tempAlertNotify"`
TempAlertNotifyTechnician bool `json:"tempAlertNotifyTechnician"`
MonthlyElectricityBillLimit int `json:"monthlyElectricityBillLimit"`
EnableElectricityBillAlert bool `json:"enableElectricityBillAlert"`
EnableProjectedElectricityBillAlert bool `json:"enableProjectedElectricityBillAlert"`
ElectricityBillingDayOfMonth int `json:"electricityBillingDayOfMonth"`
ElectricityBillCycleMonths int `json:"electricityBillCycleMonths"`
ElectricityBillStartMonth int `json:"electricityBillStartMonth"`
VentilatorMinOnTimeHome int `json:"ventilatorMinOnTimeHome"`
VentilatorMinOnTimeAway int `json:"ventilatorMinOnTimeAway"`
BacklightOffDuringSleep bool `json:"backlightOffDuringSleep"`
AutoAway bool `json:"autoAway"`
SmartCirculation bool `json:"smartCirculation"`
FollowMeComfort bool `json:"followMeComfort"`
VentilatorType string `json:"ventilatorType"`
IsVentilatorTimerOn bool `json:"isVentilatorTimerOn"`
VentilatorOffDateTime string `json:"ventilatorOffDateTime"`
HasUVFilter bool `json:"hasUVFilter"`
CoolingLockout bool `json:"coolingLockout"`
VentilatorFreeCooling bool `json:"ventilatorFreeCooling"`
DehumidifyWhenHeating bool `json:"dehumidifyWhenHeating"`
VentilatorDehumidify bool `json:"ventilatorDehumidify"`
GroupRef string `json:"groupRef"`
GroupName string `json:"groupName"`
GroupSetting int `json:"groupSetting"`
FanSpeed string `json:"fanSpeed"`
} `json:"settings"`
} `json:"thermostatList"`
Status struct {
Code int `json:"code"`
Message string `json:"message"`
} `json:"status"`
}
func main() {
var hvacMode string
var refresh bool
var weather bool
var daemon bool
var port int
flag.StringVar(&hvacMode, "m", "", "hvac mode: heat, cool, auto, off, auxHeatOnly")
flag.BoolVar(&refresh, "r", false, "Refresh token only")
flag.BoolVar(&weather, "w", false, "Check and change hvacMode based on OpenWeatherMap")
flag.BoolVar(&daemon, "d", false, "Run as a web daemon")
flag.IntVar(&port, "p", 8081, "Web daemon port")
flag.Parse()
refreshToken := readRefreshToken()
if daemon {
webServer(port)
os.Exit(0)
}
if !refresh {
if !(hvacMode == "heat" || hvacMode == "cool" || hvacMode == "auto" || hvacMode == "off" || hvacMode == "auxHeatOnly") {
log.Fatal("Invalid HVAC mode. Must be one of: heat, cool, auto, off, auxHeatOnly")
} else {
tokenResponse := renewAccessToken(refreshToken)
setHvacMode(tokenResponse.AccessToken, hvacMode)
}
} else if weather {
changeBasedOnOwm(refreshToken)
} else {
renewAccessToken(refreshToken)
}
fmt.Println("done")
}
func getConfValue(key string, defaultValue string) string {
viper.SetConfigFile("/etc/ecobeehvacmode/ecobeehvacmode.conf")
viper.SetConfigType("env")
viper.ReadInConfig()
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
log.Print("Config file not found", err)
// Config file not found; ignore error if desired
} else {
log.Fatal("Unknown error: ", err)
}
}
value := viper.GetString(key)
if value != "" {
return value
}
return defaultValue
}
// Access: New tokens are good for 1 hour
// Refresh: Need to refresh every 30 days to keep this alive
// https://www.ecobee.com/home/developer/api/documentation/v1/auth/token-refresh.shtml
func renewAccessToken(refreshToken string) TokenResponse {
data := url.Values{
"grant_type": {"refresh_token"},
"code": {refreshToken},
"client_id": {apiKey},
}
resp, err := http.PostForm(tokenUri, data)
if err != nil {
log.Fatal("Error reading request. ", err)
}
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
var tokenResponse TokenResponse
err = json.Unmarshal(body, &tokenResponse)
writeRefreshToken(tokenResponse.RefreshToken)
return (tokenResponse)
}
// Send an API call to change the HVAC mode of the Ecobee
func setHvacMode(accessToken string, hvacMode string) {
setHvacModeData := []byte(`{"selection":{"selectionType":"registered","selectionMatch":""},"thermostat":{"settings":{"hvacMode":"` + hvacMode + `"}}}`)
req, err := http.NewRequest("POST", thermostatURI, bytes.NewBuffer(setHvacModeData))
if err != nil {
log.Fatal("Error reading request. ", err)
}
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
req.Header.Set("Authorization", "Bearer "+accessToken)
client := &http.Client{Timeout: time.Second * 10}
// Send request
resp, err := client.Do(req)
if err != nil {
log.Fatal("Error reading response. ", err)
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal("Error reading body. ", err)
}
fmt.Printf("%s\n", body)
}
func getStatusId(accessToken string) string {
detailsUrl := "https://api.ecobee.com/1/thermostatSummary?format=json&body=%7B%22selection%22%3A%7B%22selectionType%22%3A%22registered%22%2C%22selectionMatch%22%3A%22%22%2C%22includeRuntime%22%3Atrue%2C%22includeSensors%22%3Atrue%2C%22includeSettings%22%3Atrue%2C%22includeEquipmentStatus%22%3Atrue%7D%7D"
req, err := http.NewRequest("GET", detailsUrl, nil)
if err != nil {
log.Fatal("Error reading request. ", err)
}
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
req.Header.Set("Authorization", "Bearer "+accessToken)
client := &http.Client{Timeout: time.Second * 10}
// Send request
resp, err := client.Do(req)
if err != nil {
log.Fatal("Error reading response. ", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal("Error reading body. ", err)
}
fmt.Printf("%s\n", body)
var statusResponse StatusResponse
err = json.Unmarshal(body, &statusResponse)
if err != nil {
log.Fatal("Error parsing status response. ", err)
}
fmt.Println(statusResponse.RevisionList[0])
ids := strings.Split(statusResponse.RevisionList[0], ":")
return ids[0]
}
func getHvacMode(accessToken string, statusId string) string {
detailsUrl := "https://api.ecobee.com/1/thermostat?json=%7B%0A%20%20%22selection%22%3A%20%7B%0A%20%20%20%20%22selectionType%22%3A%20%22thermostats%22%2C%0A%20%20%20%20%22selectionMatch%22%3A%20%22415514179919%22%2C%0A%20%20%20%20%22includeSettings%22%3A%20%22true%22%0A%20%20%7D%0A%7D"
req, err := http.NewRequest("GET", detailsUrl, nil)
if err != nil {
log.Fatal("Error reading request. ", err)
}
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
req.Header.Set("Authorization", "Bearer "+accessToken)
client := &http.Client{Timeout: time.Second * 10}
// Send request
resp, err := client.Do(req)
if err != nil {
log.Fatal("Error reading response. ", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal("Error reading body. ", err)
}
// fmt.Printf("%s\n", body)
var thermostatResponse ThermostatResponse
err = json.Unmarshal(body, &thermostatResponse)
if err != nil {
log.Fatal("Error parsing status response. ", err)
}
resp.Body.Close()
return thermostatResponse.ThermostatList[0].Settings.HvacMode
}
func readRefreshToken() string {
b, err := os.ReadFile(refreshTokenFile)
// If the file isn't found, exit early and return the known refreshtoken
// that doesn't seem to change
if errors.Is(err, os.ErrNotExist) {
fmt.Println("refreshtoken file missing, returning known refreshtoken")
return refreshTokenConst
}
if err != nil {
log.Fatal("Error reading file. ", err)
}
refreshToken := string(b)
return refreshToken
}
// Write the refresh token to a file
func writeRefreshToken(refreshToken string) {
f, err := os.Create(refreshTokenFile)
if err != nil {
log.Fatal("Error writing file. ", err)
}
defer f.Close()
f.WriteString(refreshToken)
}
func changeBasedOnOwm(refreshToken string) {
currentTemp := getTemp()
if currentTemp < heatpumpLockoutTempC {
tokenResponse := renewAccessToken(refreshToken)
statusId := getStatusId(tokenResponse.AccessToken)
hvacMode := getHvacMode(tokenResponse.AccessToken, statusId)
if hvacMode == "heat" {
setHvacMode(tokenResponse.AccessToken, "auxHeatOnly")
}
} else if currentTemp > furnaceLockoutTempC {
tokenResponse := renewAccessToken(refreshToken)
statusId := getStatusId(tokenResponse.AccessToken)
hvacMode := getHvacMode(tokenResponse.AccessToken, statusId)
if hvacMode == "auxHeatOnly" {
setHvacMode(tokenResponse.AccessToken, "heat")
}
}
}
func getTemp() float64 {
w, err := owm.NewCurrent("C", "EN", owmApiKey) // (internal - OpenWeatherMap reference for Celcius with English output
if err != nil {
log.Fatalln(err)
}
w.CurrentByName(weatherLocation)
fmt.Println(w.Main.Temp)
return w.Main.Temp
}
// Web server mode. Takes in a request like
// http://127.0.0.1/?hvacmode=auxHeatOnly
func webServer(port int) {
portString := fmt.Sprintf(":%d", port)
http.HandleFunc("/", httpHvacMode)
if err := http.ListenAndServe(portString, nil); err != nil {
log.Fatal(err)
}
}
func httpHvacMode(w http.ResponseWriter, r *http.Request) {
refreshToken := readRefreshToken()
var hvacMode string
if r.URL.Query().Has("hvacmode") {
hvacMode = r.URL.Query().Get("hvacmode")
if hvacMode == "heat" || hvacMode == "cool" || hvacMode == "auto" || hvacMode == "off" || hvacMode == "auxHeatOnly" {
tokenResponse := renewAccessToken(refreshToken)
setHvacMode(tokenResponse.AccessToken, hvacMode)
io.WriteString(w, fmt.Sprintf("Set hvacMode to %s\n", hvacMode))
}
}
}