-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathput.go
116 lines (94 loc) · 3.1 KB
/
put.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
// Copyright 2021 To Levan Giguashvili. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package sensibo
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/odinn1984/go-sensibo/models"
)
// SetDeviceTimerPayload is the payload for SetDeviceTimer API call
type SetDeviceTimerPayload struct {
MinutesFromNow int `json:"minutesFromNow"`
ACState models.ACStateData `json:"acState"`
}
// SetDeviceTimer sets the device timer.
//
// This function allows us to set the device time which will tell our device
// to set the AC state to the value of DeviceTimer.ACState.On
//
// id is the ID of the device and state is of type #models.DeviceTimer
//
// It returns the direct response from Sensibo API as a string or error
// if an issue occurred
func (s *Sensibo) SetDeviceTimer(ctx context.Context, id string, minutesFromNow int, state models.ACStateData) (string, error) {
payload := SetDeviceTimerPayload{minutesFromNow, state}
payloadStr, err := json.Marshal(payload)
if err != nil {
return "", fmt.Errorf("failed marshal on payload: \n\t%v", err)
}
resp, err := s.makePutRequest(
ctx,
"v1",
fmt.Sprintf("pods/%s/timer", id),
bytes.NewBuffer(payloadStr),
)
if err != nil {
return "", fmt.Errorf("failed setting timer: \n\t%v", err)
}
return resp, nil
}
// ToggleDeviceClimateReactPayload is the payload for ToggleDeviceClimateReact API call
type ToggleDeviceClimateReactPayload struct {
Enabled bool `json:"enabled"`
}
// ToggleDeviceClimateReact toggles the device climate react state on or off.
//
// id is the ID of the device
//
// It returns the direct response from Sensibo API as a string or error
// if an issue occurred
func (s *Sensibo) ToggleDeviceClimateReact(ctx context.Context, id string, enabled bool) (string, error) {
payload := ToggleDeviceClimateReactPayload{enabled}
payloadStr, err := json.Marshal(payload)
if err != nil {
return "", fmt.Errorf("failed marshal on payload: \n\t%v", err)
}
resp, err := s.makePutRequest(
ctx,
"v2",
fmt.Sprintf("pods/%s/smartmode", id),
bytes.NewBuffer(payloadStr),
)
if err != nil {
return "", fmt.Errorf("failed setting climate react: \n\t%v", err)
}
return resp, nil
}
// ToggleDeviceSchedulePayload is the payload for the ToggleDeviceSchedule API call
type ToggleDeviceSchedulePayload struct {
IsEnabled bool `json:"isEnabled"`
}
// ToggleDeviceSchedule toggles a device schedule state on or off.
//
// It returns the direct response from Sensibo API as a string or error
// if an issue occurred
func (s *Sensibo) ToggleDeviceSchedule(ctx context.Context, deviceID string, scheduleID string, enabled bool) (string, error) {
payload := ToggleDeviceSchedulePayload{enabled}
payloadStr, err := json.Marshal(payload)
if err != nil {
return "", fmt.Errorf("failed marshal on payload: \n\t%v", err)
}
resp, err := s.makePutRequest(
ctx,
"v1",
fmt.Sprintf("pods/%s/schedules/%s", deviceID, scheduleID),
bytes.NewBuffer(payloadStr),
)
if err != nil {
return "", fmt.Errorf("failed setting climate react: \n\t%v", err)
}
return resp, nil
}