-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththermostat.go
137 lines (114 loc) · 4.03 KB
/
thermostat.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
package ecobee
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/sherif-fanous/go-ecobee/objects"
"io"
"io/ioutil"
"net/url"
)
const (
thermostatEndpoint = "thermostat"
thermostatSummaryEndpoint = "thermostatSummary"
)
// Thermostat retrieves a selection of thermostat data for one or more
// thermostats.
//
// For more information see: https://www.ecobee.com/home/developer/api/documentation/v1/operations/get-thermostats.shtml
func (c *Client) Thermostat(ctx context.Context, selection *objects.Selection, page *objects.Page) (*ThermostatSuccessResponse, error) {
data, err := json.Marshal(struct {
Selection *objects.Selection `json:"selection,omitempty"`
Page *objects.Page `json:"page,omitempty"`
}{
Selection: selection,
Page: page,
})
if err != nil {
return nil, fmt.Errorf("%s: %w", thermostatEndpoint, err)
}
queryParameters := url.Values{}
queryParameters.Set("json", string(data))
resp, err := c.get(ctx, fmt.Sprintf("%s%d/%s", c.apiBaseURL, c.apiVersion, thermostatEndpoint), queryParameters, nil)
if err != nil {
return nil, fmt.Errorf("%s: %w", thermostatEndpoint, err)
}
defer func() {
if resp != nil {
_, _ = io.Copy(ioutil.Discard, resp.Body)
_ = resp.Body.Close()
}
}()
thermostatSuccessResponse := ThermostatSuccessResponse{}
if err := processAPIResponse(thermostatEndpoint, resp, &thermostatSuccessResponse); err != nil {
return nil, err
}
return &thermostatSuccessResponse, nil
}
// ThermostatSummary retrieves a list of thermostat configuration and state
// revisions.
//
// For more information see: https://www.ecobee.com/home/developer/api/documentation/v1/operations/get-thermostat-summary.shtml
func (c *Client) ThermostatSummary(ctx context.Context, selection *objects.Selection) (*ThermostatSummarySuccessResponse, error) {
data, err := json.Marshal(struct {
Selection *objects.Selection `json:"selection,omitempty"`
}{
Selection: selection,
})
if err != nil {
return nil, fmt.Errorf("%s: %w", thermostatSummaryEndpoint, err)
}
queryParameters := url.Values{}
queryParameters.Set("json", string(data))
resp, err := c.get(ctx, fmt.Sprintf("%s%d/%s", c.apiBaseURL, c.apiVersion, thermostatSummaryEndpoint), queryParameters, nil)
if err != nil {
return nil, fmt.Errorf("%s: %w", thermostatSummaryEndpoint, err)
}
defer func() {
if resp != nil {
_, _ = io.Copy(ioutil.Discard, resp.Body)
_ = resp.Body.Close()
}
}()
thermostatSummaryResponse := ThermostatSummarySuccessResponse{}
if err := processAPIResponse(thermostatSummaryEndpoint, resp, &thermostatSummaryResponse); err != nil {
return nil, err
}
return &thermostatSummaryResponse, nil
}
// UpdateThermostat permits the modification of any writable thermostat or
// sub-object property.
//
// For more information see: https://www.ecobee.com/home/developer/api/documentation/v1/operations/post-update-thermostats.shtml
func (c *Client) UpdateThermostat(ctx context.Context, selection *objects.Selection, thermostat *objects.Thermostat, functions []objects.Function) (*APIStatusResponse, error) {
data, err := json.Marshal(struct {
Selection *objects.Selection `json:"selection,omitempty"`
Thermostat *objects.Thermostat `json:"thermostat,omitempty"`
Functions []objects.Function `json:"functions,omitempty"`
}{
Selection: selection,
Thermostat: thermostat,
Functions: functions,
})
if err != nil {
return nil, fmt.Errorf("%s: %w", thermostatEndpoint, err)
}
queryParameters := url.Values{}
queryParameters.Set("format", "json")
resp, err := c.post(ctx, fmt.Sprintf("%s%d/%s", c.apiBaseURL, c.apiVersion, thermostatEndpoint), queryParameters, nil, bytes.NewBuffer(data))
if err != nil {
return nil, fmt.Errorf("%s: %w", thermostatEndpoint, err)
}
defer func() {
if resp != nil {
_, _ = io.Copy(ioutil.Discard, resp.Body)
_ = resp.Body.Close()
}
}()
updateThermostatResponse := APIStatusResponse{}
if err := processAPIResponse(thermostatEndpoint, resp, &updateThermostatResponse); err != nil {
return nil, err
}
return &updateThermostatResponse, nil
}