-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathzone.go
380 lines (324 loc) · 13.7 KB
/
zone.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
package gotado
import (
"context"
"errors"
"fmt"
)
// GetState returns the state of the zone.
func (z *Zone) GetState(ctx context.Context) (*ZoneState, error) {
state := &ZoneState{}
if err := z.client.get(ctx, apiURL("homes/%d/zones/%d/state", z.home.ID, z.ID), state); err != nil {
return nil, err
}
return state, nil
}
// GetCapabilities returns the capabilities of the zone.
func (z *Zone) GetCapabilities(ctx context.Context) (*ZoneCapabilities, error) {
capabilities := &ZoneCapabilities{}
if err := z.client.get(ctx, apiURL("homes/%d/zones/%d/capabilities", z.home.ID, z.ID), capabilities); err != nil {
return nil, err
}
return capabilities, nil
}
// GetDevices lists all devices in the zone
func (z *Zone) GetDevices(ctx context.Context) ([]*Device, error) {
devices := make([]*Device, 0)
if err := z.client.get(ctx, apiURL("homes/%d/zones/%d/devices", z.home.ID, z.ID), &devices); err != nil {
return nil, err
}
for _, device := range devices {
device.client = z.client
device.home = z.home
}
return devices, nil
}
// ResumeSchedule resumes the zone's smart schedule.
func (z *Zone) ResumeSchedule(ctx context.Context) error {
return z.client.delete(ctx, apiURL("homes/%d/zones/%d/overlay", z.home.ID, z.ID))
}
// SetHeatingOff turns off the heating in the zone.
func (z *Zone) SetHeatingOff(ctx context.Context) error {
overlay := &ZoneOverlay{
Setting: &ZoneSetting{
Type: "HEATING",
Power: "OFF",
},
}
if err := z.client.put(ctx, apiURL("homes/%d/zones/%d/overlay", z.home.ID, z.ID), overlay); err != nil {
return err
}
if overlay.Type != "MANUAL" || overlay.Setting.Power != "OFF" {
return errors.New("tado° refused to turn off heating")
}
return nil
}
// SetHeatingOn turns on the heating in the zone. The temperature should
// use the unit configured for the home.
func (z *Zone) SetHeatingOn(ctx context.Context, temperature float64) error {
temperatureSetting := &ZoneSettingTemperature{}
switch z.home.TemperatureUnit {
case TemperatureUnitCelsius:
temperatureSetting.Celsius = temperature
case TemperatureUnitFahrenheit:
temperatureSetting.Fahrenheit = temperature
default:
return fmt.Errorf("invalid temperature unit '%s'", z.home.TemperatureUnit)
}
overlay := &ZoneOverlay{
Setting: &ZoneSetting{
Type: ZoneTypeHeating,
Power: PowerOn,
Temperature: temperatureSetting,
},
}
if err := z.client.put(ctx, apiURL("homes/%d/zones/%d/overlay", z.home.ID, z.ID), overlay); err != nil {
return err
}
if overlay.Type != OverlayTypeManual || overlay.Setting.Power != PowerOn {
return errors.New("tado° refused to set the given temperature")
}
return nil
}
// GetManualControlTerminationCondition returns the condition how long manual
// control in the zone will remain active after a tado° device was controlled
// manually.
func (z *Zone) GetManualControlTerminationCondition(ctx context.Context) (*ZoneOverlayTermination, error) {
defaultOverlay := &ZoneDefaultOverlay{}
if err := z.client.get(ctx, apiURL("homes/%d/zones/%d/defaultOverlay", z.home.ID, z.ID), defaultOverlay); err != nil {
return nil, err
}
return defaultOverlay.TerminationCondition, nil
}
// SetManualControlTerminationCondition sets the given manual control termination condition.
// Possible types for the condition are "MANUAL" (Until ended by user), "TIMER"
// and "TADO_MODE" (Until next automatic change).
func (z *Zone) SetManualControlTerminationCondition(ctx context.Context, condition *ZoneOverlayTermination) error {
defaultOverlay := &ZoneDefaultOverlay{
TerminationCondition: condition,
}
if err := z.client.put(ctx, apiURL("homes/%d/zones/%d/defaultOverlay", z.home.ID, z.ID), defaultOverlay); err != nil {
return err
}
return nil
}
// ManualControlUntilAutoChange ensures that manual control of the zone remains
// active until the next automatic change in the tado° schedule.
func (z *Zone) ManualControlUntilAutoChange(ctx context.Context) error {
return z.SetManualControlTerminationCondition(ctx, &ZoneOverlayTermination{Type: OverlayTypeAuto})
}
// ManualControlTimer ensures that manual control of the zone remains active for
// the given duration in seconds.
func (z *Zone) ManualControlTimer(ctx context.Context, duration int32) error {
return z.SetManualControlTerminationCondition(ctx, &ZoneOverlayTermination{Type: OverlayTypeTimer, DurationInSeconds: duration})
}
// ManualControlUntilUserEnd ensures that manual control of the zone remains
// active until ended by the user.
func (z *Zone) ManualControlUntilUserEnd(ctx context.Context) error {
return z.SetManualControlTerminationCondition(ctx, &ZoneOverlayTermination{Type: OverlayTypeManual})
}
// OpenWindow puts the zone into open window mode (open window must have been
// detected by tado° beforehand).
func (z *Zone) OpenWindow(ctx context.Context) error {
return z.client.post(ctx, apiURL("homes/%d/zones/%d/state/openWindow/activate", z.home.ID, z.ID))
}
// CloseWindow ends open window mode in the zone.
func (z *Zone) CloseWindow(ctx context.Context) error {
return z.client.delete(ctx, apiURL("homes/%d/zones/%d/state/openWindow", z.home.ID, z.ID))
}
// EnableOpenWindowDetection enable open window detection with the given heating timeout
// duration in seconds after an open window has been detected.
func (z *Zone) EnableOpenWindowDetection(ctx context.Context, timeout int32) error {
return z.client.put(ctx, apiURL("homes/%d/zones/%d/openWindowDetection", z.home.ID, z.ID), OpenWindowDetection{Enabled: true, TimeoutInSeconds: timeout})
}
// DisableOpenWindowDetection disable open window detection.
func (z *Zone) DisableOpenWindowDetection(ctx context.Context) error {
return z.client.put(ctx, apiURL("homes/%d/zones/%d/openWindowDetection", z.home.ID, z.ID), OpenWindowDetection{Enabled: false})
}
// GetOpenWindowDetection returns the current open window detection settings.
func (z *Zone) GetOpenWindowDetection(ctx context.Context) (*OpenWindowDetection, error) {
// Get fresh zone object to ensure that the returned settings are up to date.
zone, err := z.home.GetZone(ctx, z.Name)
if err != nil {
return nil, err
}
return &OpenWindowDetection{
Enabled: zone.OpenWindowDetection.Enabled,
TimeoutInSeconds: zone.OpenWindowDetection.TimeoutInSeconds,
}, nil
}
// EnableDazzleMode enables dazzle mode in the zone.
func (z *Zone) EnableDazzleMode(ctx context.Context) error {
return z.client.put(ctx, apiURL("homes/%d/zones/%d/dazzle", z.home.ID, z.ID), DazzleMode{Enabled: true})
}
// DisableDazzleMode disables dazzle mode in the zone.
func (z *Zone) DisableDazzleMode(ctx context.Context) error {
return z.client.put(ctx, apiURL("homes/%d/zones/%d/dazzle", z.home.ID, z.ID), DazzleMode{Enabled: false})
}
// GetDazzleMode returns the current dazzle mode settings.
func (z *Zone) GetDazzleMode(ctx context.Context) (*ZoneDazzleMode, error) {
// Get fresh zone object to ensure that the returned settings are up to date.
zone, err := z.home.GetZone(ctx, z.Name)
if err != nil {
return nil, err
}
return &zone.DazzleMode, nil
}
// GetEarlyStart checks if early start is enabled in the zone.
func (z *Zone) GetEarlyStart(ctx context.Context) (bool, error) {
earlyStart := &EarlyStart{}
if err := z.client.get(ctx, apiURL("homes/%d/zones/%d/earlyStart", z.home.ID, z.ID), earlyStart); err != nil {
return false, err
}
return earlyStart.Enabled, nil
}
// SetEarlyStart enables or disables early start in the zone.
func (z *Zone) SetEarlyStart(ctx context.Context, earlyStart bool) error {
return z.client.put(ctx, apiURL("homes/%d/zones/%d/earlyStart", z.home.ID, z.ID), &EarlyStart{Enabled: earlyStart})
}
// newScheduleTimetable creates a new schedule timetable linked to the zone.
func (z *Zone) newScheduleTimetable(id int32, typ TimetableType) *ScheduleTimetable {
return &ScheduleTimetable{
client: z.client,
zone: z,
ID: id,
Type: typ,
}
}
// newHeatingSchedule creates a new heating schedule linked to the zone.
func (z *Zone) newHeatingSchedule(timetable *ScheduleTimetable, blocks []*ScheduleTimeBlock) *HeatingSchedule {
return &HeatingSchedule{
zone: z,
Timetable: timetable,
Blocks: blocks,
}
}
// ScheduleMonToSun has the same schedule for all days between monday and sunday.
func (z *Zone) ScheduleMonToSun(ctx context.Context) (*HeatingSchedule, error) {
timetable := z.newScheduleTimetable(0, TimetableOneDay)
blocks, err := timetable.GetTimeBlocks(ctx)
if err != nil {
return nil, fmt.Errorf("unable to get schedule time blocks: %w", err)
}
return z.newHeatingSchedule(timetable, blocks), nil
}
// TimetableTMonToFriSatSun has the same schedule for all days between monday
// and friday and different schedules for saturday and sunday.
func (z *Zone) ScheduleMonToFriSatSun(ctx context.Context) (*HeatingSchedule, error) {
timetable := z.newScheduleTimetable(1, TimetableThreeDay)
blocks, err := timetable.GetTimeBlocks(ctx)
if err != nil {
return nil, fmt.Errorf("unable to get schedule time blocks: %w", err)
}
return z.newHeatingSchedule(timetable, blocks), nil
}
// ScheduleAllDays has a different schedule for each day of the week.
func (z *Zone) ScheduleAllDays(ctx context.Context) (*HeatingSchedule, error) {
timetable := z.newScheduleTimetable(2, TimetableSevenDay)
blocks, err := timetable.GetTimeBlocks(ctx)
if err != nil {
return nil, fmt.Errorf("unable to get schedule time blocks: %w", err)
}
return z.newHeatingSchedule(timetable, blocks), nil
}
// GetActiveScheduleTimetable returns the active schedule timetable for the zone.
func (z *Zone) GetActiveScheduleTimetable(ctx context.Context) (*ScheduleTimetable, error) {
timetable := &ScheduleTimetable{}
if err := z.client.get(ctx, apiURL("homes/%d/zones/%d/schedule/activeTimetable", z.home.ID, z.ID), timetable); err != nil {
return nil, err
}
timetable.client = z.client
timetable.zone = z
return timetable, nil
}
// SetActiveScheduleTimetable sets the active schedule timetable for the zone.
// Should be one of TimetableMonToSun(), TimetableMonToFriSatSun() or TimetableAllDays(),
func (z *Zone) SetActiveScheduleTimetable(ctx context.Context, timetable *ScheduleTimetable) error {
newTimetable := &ScheduleTimetable{ID: timetable.ID}
return z.client.put(ctx, apiURL("homes/%d/zones/%d/schedule/activeTimetable", z.home.ID, z.ID), newTimetable)
}
// GetHeatingSchedule gets the whole active schedule for the zone, including active timetable and time blocks.
func (z *Zone) GetHeatingSchedule(ctx context.Context) (*HeatingSchedule, error) {
timetable, err := z.GetActiveScheduleTimetable(ctx)
if err != nil {
return nil, fmt.Errorf("unable to get active schedule timetable: %w", err)
}
blocks, err := timetable.GetTimeBlocks(ctx)
if err != nil {
return nil, fmt.Errorf("unable to get time blocks: %w", err)
}
var scheduleDays ScheduleDays
switch timetable.Type {
case TimetableOneDay:
scheduleDays = ScheduleDaysMonToSun
case TimetableThreeDay:
scheduleDays = ScheduleDaysMonToFriSatSun
case TimetableSevenDay:
scheduleDays = ScheduleDaysMonTueWedThuFriSatSun
default:
return nil, errors.New("unknown schedule timetable type")
}
return &HeatingSchedule{
zone: z,
ScheduleDays: scheduleDays,
Timetable: timetable,
Blocks: blocks,
}, nil
}
// SetHeatingSchedule sets the whole active schedule for the zone, including active timetable and time blocks.
func (z *Zone) SetHeatingSchedule(ctx context.Context, schedule *HeatingSchedule) error {
if err := z.SetActiveScheduleTimetable(ctx, schedule.Timetable); err != nil {
return fmt.Errorf("unable to set active schedule timetable: %w", err)
}
if err := schedule.Timetable.SetTimeBlocks(ctx, schedule.Blocks); err != nil {
return fmt.Errorf("unable to set time blocks: %w", err)
}
return nil
}
// GetAwayConfiguration returns the away configuration of the zone.
func (z *Zone) GetAwayConfiguration(ctx context.Context) (*AwayConfiguration, error) {
awayConfig := &AwayConfiguration{}
if err := z.client.get(ctx, apiURL("homes/%d/zones/%d/schedule/awayConfiguration", z.home.ID, z.ID), awayConfig); err != nil {
return nil, err
}
return awayConfig, nil
}
// SetAwayConfiguration updates the away configuration of the zone.
func (z *Zone) SetAwayConfiguration(ctx context.Context, awayConfig *AwayConfiguration) error {
return z.client.put(ctx, apiURL("homes/%d/zones/%d/schedule/awayConfiguration", z.home.ID, z.ID), *awayConfig)
}
// SetAwayMinimumTemperature sets the minimum temperature for away mode in the zone.
func (z *Zone) SetAwayMinimumTemperature(ctx context.Context, temperature float64) error {
awayConfig, err := z.GetAwayConfiguration(ctx)
if err != nil {
return fmt.Errorf("unable to get current away configuration: %w", err)
}
switch z.home.TemperatureUnit {
case TemperatureUnitCelsius:
awayConfig.Setting.Temperature.Celsius = temperature
case TemperatureUnitFahrenheit:
awayConfig.Setting.Temperature.Fahrenheit = temperature
default:
return fmt.Errorf("invalid temperature unit '%s'", z.home.TemperatureUnit)
}
return z.SetAwayConfiguration(ctx, awayConfig)
}
// SetAwayPreheatOff turns off preheat before arrival. Tado° will only start
// heating after arrival. To turn preheating back on, use SetAwayPreheatComfortLevel().
func (z *Zone) SetAwayPreheatOff(ctx context.Context) error {
awayConfig, err := z.GetAwayConfiguration(ctx)
if err != nil {
return fmt.Errorf("unable to get current away configuration: %w", err)
}
awayConfig.AutoAdjust = false
return z.SetAwayConfiguration(ctx, awayConfig)
}
// SetAwayPreheatComfortLevel sets the comfort level for preheating before arrival.
func (z *Zone) SetAwayPreheatComfortLevel(ctx context.Context, comfortLevel ComfortLevel) error {
awayConfig, err := z.GetAwayConfiguration(ctx)
if err != nil {
return fmt.Errorf("unable to get current away configuration: %w", err)
}
awayConfig.AutoAdjust = true
awayConfig.ComfortLevel = comfortLevel
return z.SetAwayConfiguration(ctx, awayConfig)
}