-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
257 lines (220 loc) · 6.73 KB
/
main.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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"time"
"golang.org/x/oauth2"
)
type TadoContext struct {
tado *http.Client
time time.Time
}
type TadoConfig struct {
Username string `json:"username"`
Password string `json:"password"`
}
func main() {
ctx := makeContext()
acc, err := ctx.getTadoAccount()
if err != nil {
log.Fatal(err)
}
for _, home := range acc.Homes {
home, err = ctx.getTadoHome(home.ID)
if err != nil {
log.Fatal(err)
}
zones, err := ctx.getTadoZones(home)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s...\n", home.Name)
for _, zone := range zones {
if zone.Type == "AIR_CONDITIONING" {
fmt.Printf("%s, ", zone.Name)
err = ctx.smartZone(home, zone)
if err != nil {
log.Fatal(err)
}
}
}
}
}
func (ctx *TadoContext) smartZone(home TadoHome, zone TadoZone) error {
state, err := ctx.getTadoZoneState(home, zone)
if err != nil {
return err
}
if expiry := state.Overlay.Termination.ProjectedExpiry; expiry != nil && expiry.After(ctx.time.Add(10*time.Minute)) {
fmt.Print("manual mode\n")
return nil
}
timetable, err := ctx.getTadoActiveTimetable(home, zone)
if err != nil {
return err
}
block, err := ctx.getTadoTimetableBlock(home, zone, timetable, ctx.time.Add(5*time.Minute))
if err != nil {
return err
}
target := block.Setting
if state.TadoMode == "AWAY" && !block.GeolocationOverride {
conf, err := ctx.getTadoAwayConfiguration(home, zone)
if err != nil {
return err
}
if conf.Setting.Power != "OFF" {
target = conf.Setting
} else {
target.Power = "OFF"
}
}
switch target.Mode {
case "COOL":
return ctx.smartCool(home, zone, state, target)
case "DRY":
return ctx.smartDry(home, zone, state, target)
case "HEAT":
return ctx.smartHeat(home, zone, state, target)
}
fmt.Printf("OK: (power=%s, mode=%s)\n", target.Power, target.Mode)
return nil
}
func (ctx *TadoContext) smartCool(home TadoHome, zone TadoZone, state TadoZoneState, target TadoSetting) error {
curMode := state.Setting.Mode
curPower := state.Setting.Power
curTemp := state.SensorDataPoints.InsideTemperature.Celsius
tgtPower := target.Power
tgtFan := target.FanSpeed
tgtTemp := target.Temperature.Celsius
if curTemp < tgtTemp+0.5 {
if curPower == "OFF" {
fmt.Printf("cooling stay off: (tgt=%v°C, cur=%v°C)\n", tgtTemp, curTemp)
_, err := ctx.putTadoOverlay(home, zone, makeOffOverlay(10*time.Minute))
return err
}
if curTemp < tgtTemp-0.5 {
fmt.Printf("cooling turn off: (tgt=%v°C, cur=%v°C)\n", tgtTemp, curTemp)
_, err := ctx.putTadoOverlay(home, zone, makeOffOverlay(15*time.Minute))
return err
}
}
if curMode == "COOL" && tgtPower == "ON" {
if curTemp > tgtTemp+4 && (tgtFan == "AUTO" || tgtFan == "MIDDLE") {
target.FanSpeed = "HIGH"
fmt.Printf("cooling boost high: (tgt=%v°C, cur=%v°C)\n", tgtTemp, curTemp)
_, err := ctx.putTadoOverlay(home, zone, makeOverlay(target, 10*time.Minute))
return err
}
if curTemp > tgtTemp+2 && (tgtFan == "AUTO" || tgtFan == "LOW") {
target.FanSpeed = "MIDDLE"
fmt.Printf("cooling boost: (tgt=%v°C, cur=%v°C)\n", tgtTemp, curTemp)
_, err := ctx.putTadoOverlay(home, zone, makeOverlay(target, 10*time.Minute))
return err
}
}
fmt.Printf("cooling OK: (tgt=%v°C, cur=%v°C, fan=%s, mode=%s)\n", tgtTemp, curTemp, tgtFan, curMode)
return nil
}
func (ctx *TadoContext) smartHeat(home TadoHome, zone TadoZone, state TadoZoneState, target TadoSetting) error {
curMode := state.Setting.Mode
curPower := state.Setting.Power
curTemp := state.SensorDataPoints.InsideTemperature.Celsius
tgtPower := target.Power
tgtFan := target.FanSpeed
tgtTemp := target.Temperature.Celsius
if curTemp > tgtTemp-0.5 {
if curPower == "OFF" {
fmt.Printf("heating stay off: (tgt=%v°C, cur=%v°C)\n", tgtTemp, curTemp)
_, err := ctx.putTadoOverlay(home, zone, makeOffOverlay(10*time.Minute))
return err
}
if curTemp > tgtTemp+0.5 {
fmt.Printf("heating turn off: (tgt=%v°C, cur=%v°C)\n", tgtTemp, curTemp)
_, err := ctx.putTadoOverlay(home, zone, makeOffOverlay(15*time.Minute))
return err
}
}
if curMode == "HEAT" && tgtPower == "ON" {
if curTemp < tgtTemp-4 && (tgtFan == "AUTO" || tgtFan == "MIDDLE") {
target.FanSpeed = "HIGH"
fmt.Printf("heating boost high: (tgt=%v°C, cur=%v°C)\n", tgtTemp, curTemp)
_, err := ctx.putTadoOverlay(home, zone, makeOverlay(target, 10*time.Minute))
return err
}
if curTemp < tgtTemp-2 && (tgtFan == "AUTO" || tgtFan == "LOW") {
target.FanSpeed = "MIDDLE"
fmt.Printf("heating boost: (tgt=%v°C, cur=%v°C)\n", tgtTemp, curTemp)
_, err := ctx.putTadoOverlay(home, zone, makeOverlay(target, 10*time.Minute))
return err
}
}
fmt.Printf("heating OK: (tgt=%v°C, cur=%v°C, fan=%s, mode=%s)\n", tgtTemp, curTemp, tgtFan, curMode)
return nil
}
func (ctx *TadoContext) smartDry(home TadoHome, zone TadoZone, state TadoZoneState, target TadoSetting) error {
curPower := state.Setting.Power
curRH := state.SensorDataPoints.Humidity.Percentage
if 0 < curRH && curRH < 50 {
if curPower == "OFF" {
fmt.Printf("drying stay off: (rh=%v%%)\n", curRH)
_, err := ctx.putTadoOverlay(home, zone, makeOffOverlay(10*time.Minute))
return err
}
if curRH < 40 {
fmt.Printf("drying turn off: (rh=%v%%)\n", curRH)
_, err := ctx.putTadoOverlay(home, zone, makeOffOverlay(15*time.Minute))
return err
}
}
fmt.Printf("drying OK: (rh=%v%%)\n", curRH)
return nil
}
func makeContext() TadoContext {
path := filepath.Join(filepath.Dir(os.Args[0]), "config.json")
file, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer file.Close()
var config TadoConfig
if err := json.NewDecoder(file).Decode(&config); err != nil {
log.Fatalf("Decode config.json: %s", err)
}
oauth := &oauth2.Config{
ClientID: "public-api-preview",
ClientSecret: "4HJGRffVR8xb3XdEUQpjgZ1VplJi6Xgw",
Scopes: []string{"home.user"},
Endpoint: oauth2.Endpoint{
TokenURL: "https://auth.tado.com/oauth/token",
},
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
token, err := oauth.PasswordCredentialsToken(ctx, config.Username, config.Password)
if err != nil {
log.Fatalf("PasswordCredentialsToken: %v", err)
}
client := oauth.Client(ctx, token)
client.Timeout = 5 * time.Second
return TadoContext{
time: time.Now(),
tado: client,
}
}
func makeOffOverlay(duration time.Duration) TadoOverlay {
return makeOverlay(TadoSetting{Type: "AIR_CONDITIONING", Power: "OFF"}, duration)
}
func makeOverlay(setting TadoSetting, duration time.Duration) TadoOverlay {
overlay := TadoOverlay{
Setting: setting,
Termination: TadoTermination{Type: "TIMER"},
}
overlay.Termination.DurationInSeconds = int(duration / time.Second)
return overlay
}