-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtado.go
174 lines (156 loc) · 4.37 KB
/
tado.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
package tado
import (
"net/http"
"sync"
"time"
"github.com/SebastiaanKlippert/go-tado/tadoauth"
)
type authClient interface {
GetToken(username, password string) (*tadoauth.TokenResponse, error)
RefreshToken(refreshToken string) (*tadoauth.TokenResponse, error)
}
// Client is the main client used to communicate with the Tado API.
type Client struct {
HTTPClient *http.Client
authClient authClient
baseURL string
username, password string
tr *tadoauth.TokenResponse
accessTokenValidUntil time.Time
mutex *sync.Mutex
}
// NewClient returns a new Tado client.
func NewClient(username, password string) *Client {
return &Client{
username: username,
password: password,
accessTokenValidUntil: time.Time{},
baseURL: defaultBaseURL,
mutex: new(sync.Mutex),
authClient: tadoauth.NewClient(),
HTTPClient: http.DefaultClient,
}
}
func (c *Client) validateAccessToken() error {
c.mutex.Lock()
defer c.mutex.Unlock()
// check if access token is valid for at least 5 more seconds
if c.accessTokenValidUntil.After(time.Now().Add(5 * time.Second)) {
return nil
}
// access token is expired, or will expire soon, get a new one
var err error
if c.tr != nil && c.tr.RefreshToken != "" {
// exchange refresh token for new access token
c.tr, err = c.authClient.RefreshToken(c.tr.RefreshToken)
} else {
// get new token based on username and password
c.tr, err = c.authClient.GetToken(c.username, c.password)
}
if err != nil {
return err
}
c.accessTokenValidUntil = time.Now().Add(time.Duration(c.tr.ExpiresIn) * time.Second)
return nil
}
// GetMe returns the users data from the API.
func (c *Client) GetMe() (*GetMeOutput, error) {
in := new(GetMeInput)
out := new(GetMeOutput)
err := c.do(in, out)
if err != nil {
return nil, err
}
return out, nil
}
// GetHome returns the Home data for a single home.
func (c *Client) GetHome(in *GetHomeInput) (*GetHomeOutput, error) {
out := new(GetHomeOutput)
err := c.do(in, out)
if err != nil {
return nil, err
}
return out, nil
}
// GetDevices returns the devices in a home.
func (c *Client) GetDevices(in *GetDevicesInput) (GetDevicesOutput, error) {
out := make(GetDevicesOutput, 0)
err := c.do(in, &out)
if err != nil {
return nil, err
}
return out, nil
}
// GetUsers returns the users in a home.
func (c *Client) GetUsers(in *GetUsersInput) (GetUsersOutput, error) {
out := make(GetUsersOutput, 0)
err := c.do(in, &out)
if err != nil {
return nil, err
}
return out, nil
}
// GetZones returns the zones for a single home.
func (c *Client) GetZones(in *GetZonesInput) (GetZonesOutput, error) {
out := make(GetZonesOutput, 0)
err := c.do(in, &out)
if err != nil {
return nil, err
}
return out, nil
}
// GetHomeState returns the presence state for a single home.
func (c *Client) GetHomeState(in *GetHomeStateInput) (*GetHomeStateOutput, error) {
out := new(GetHomeStateOutput)
err := c.do(in, out)
if err != nil {
return nil, err
}
return out, nil
}
// GetZoneState returns the state for a single zone within a home.
func (c *Client) GetZoneState(in *GetZoneStateInput) (*GetZoneStateOutput, error) {
out := new(GetZoneStateOutput)
err := c.do(in, out)
if err != nil {
return nil, err
}
return out, nil
}
// GetWeather returns the weather info for a home.
func (c *Client) GetWeather(in *GetWeatherInput) (*GetWeatherOutput, error) {
out := new(GetWeatherOutput)
err := c.do(in, out)
if err != nil {
return nil, err
}
return out, nil
}
// GetDayReport returns a detailed zone report of a specific date.
func (c *Client) GetDayReport(in *GetDayReportInput) (*GetDayReportOutput, error) {
out := new(GetDayReportOutput)
err := c.do(in, out)
if err != nil {
return nil, err
}
return out, nil
}
// PutOverlay sets an overlay in a zone, it can be used to contol settings overruling a schema.
// For example to set the heating or hot water.
func (c *Client) PutOverlay(in *PutOverlayInput) (*PutOverlayOutput, error) {
out := new(PutOverlayOutput)
err := c.do(in, out)
if err != nil {
return nil, err
}
return out, nil
}
// DeleteOverlay deletes an overlay for a zone.
func (c *Client) DeleteOverlay(in *DeleteOverlayInput) (*DeleteOverlayOutput, error) {
out := new(DeleteOverlayOutput)
err := c.do(in, out)
if err != nil {
return nil, err
}
return out, nil
}