-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecobee.php
78 lines (62 loc) · 1.83 KB
/
ecobee.php
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
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
function indoor_temp()
{
return f2c(get_response()["thermostatList"][0]["remoteSensors"][2]["capability"][0]["value"] / 10);
}
function f2c($f)
{
return round(($f - 32) / 1.8, 1);
}
function get_response()
{
require 'constants.php';
$client = new Client([
'base_uri' => 'https://api.ecobee.com'
]);
$response = $client->get(
'/1/thermostat?json={"selection":{"selectionType":"registered","selectionMatch":"","includeSensors":"true"}}',
[
'headers' => [
'Content-Type' => 'application/json;charset=UTF-8',
'Authorization' => 'Bearer ' . $app
],
'http_errors' => false
]
);
$status_code = $response->getStatusCode();
$data = json_decode($response->getBody()->getContents(), true);
if ($status_code != 200) {
if ($status_code == 500) {
refresh();
$data = get_response();
} else {
throw new RuntimeException($status_code . $data);
}
}
return $data;
}
function refresh()
{
require 'constants.php';
$client = new Client([
'base_uri' => 'https://api.ecobee.com'
]);
$response = $client->post(
"/token?grant_type=refresh_token&code=$refresh&client_id=$key",
[
'http_errors' => false
]
);
$status_code = $response->getStatusCode();
$data = json_decode($response->getBody()->getContents(), true);
if ($status_code != 200) {
throw new RuntimeException($status_code . $data);
}
$refresh_token = $data['refresh_token'];
$access_token = $data['access_token'];
$file = "<?php \$key = '$key';\n\$refresh = '$refresh_token'; \n\$app = '$access_token';\n?>\n";
file_put_contents('constants.php', $file);
}
?>