-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathnest.class.php
1588 lines (1485 loc) · 75.7 KB
/
nest.class.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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
defined('DATE_FORMAT') OR define('DATE_FORMAT', 'Y-m-d');
defined('DATETIME_FORMAT') OR define('DATETIME_FORMAT', DATE_FORMAT . ' H:i:s');
defined('USE_STATUS_BUCKETS') OR define('USE_STATUS_BUCKETS', FALSE);
define('TARGET_TEMP_MODE_COOL', 'cool');
define('TARGET_TEMP_MODE_HEAT', 'heat');
define('TARGET_TEMP_MODE_RANGE', 'range');
define('TARGET_TEMP_MODE_OFF', 'off');
define('ECO_MODE_MANUAL', 'manual-eco');
define('ECO_MODE_SCHEDULE', 'schedule');
define('FAN_MODE_AUTO', 'auto');
define('FAN_MODE_ON', 'on');
define('FAN_MODE_EVERY_DAY_ON', 'on');
define('FAN_MODE_EVERY_DAY_OFF', 'auto');
define('FAN_MODE_MINUTES_PER_HOUR', 'duty-cycle');
define('FAN_MODE_MINUTES_PER_HOUR_15', FAN_MODE_MINUTES_PER_HOUR . ',900');
define('FAN_MODE_MINUTES_PER_HOUR_30', FAN_MODE_MINUTES_PER_HOUR . ',1800');
define('FAN_MODE_MINUTES_PER_HOUR_45', FAN_MODE_MINUTES_PER_HOUR . ',2700');
define('FAN_MODE_MINUTES_PER_HOUR_ALWAYS_ON', 'on,3600');
define('FAN_MODE_TIMER', '');
define('FAN_TIMER_15M', ',900');
define('FAN_TIMER_30M', ',1800');
define('FAN_TIMER_45M', ',2700');
define('FAN_TIMER_1H', ',3600');
define('FAN_TIMER_2H', ',7200');
define('FAN_TIMER_4H', ',14400');
define('FAN_TIMER_8H', ',28800');
define('FAN_TIMER_12H', ',43200');
define('AWAY_MODE_ON', TRUE);
define('AWAY_MODE_OFF', FALSE);
define('DUALFUEL_BREAKPOINT_ALWAYS_PRIMARY', 'always-primary');
define('DUALFUEL_BREAKPOINT_ALWAYS_ALT', 'always-alt');
define('DEVICE_WITH_NO_NAME', 'Not Set');
define('DEVICE_TYPE_THERMOSTAT', 'thermostat');
define('DEVICE_TYPE_PROTECT', 'protect');
define('DEVICE_TYPE_SENSOR', 'sensor');
define('NESTAPI_ERROR_UNDER_MAINTENANCE', 1000);
define('NESTAPI_ERROR_EMPTY_RESPONSE', 1001);
define('NESTAPI_ERROR_NOT_JSON_RESPONSE', 1002);
define('NESTAPI_ERROR_API_JSON_ERROR', 1003);
define('NESTAPI_ERROR_API_OTHER_ERROR', 1004);
/**
* Unofficial Nest API
*
* This is an unofficial PHP class that will allow you to monitor and control your Nest Learning Thermostat, and Nest Protect.
*
* @category Algorithm
* @package PommePause\Nest\API
* @author Guillaume Boudreau <[email protected]>
* @license GNU LESSER GENERAL PUBLIC LICENSE Version 3
* @link https://github.com/gboudreau/nest-api/
* @link https://nest.com/
*/
class Nest
{
const USER_AGENT = 'Nest/5.0.0.23 (iOScom.nestlabs.jasper.release) os=11.0';
const PROTOCOL_VERSION = 1;
const LOGIN_URL = 'https://home.nest.com/session';
protected $days_maps = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
protected $transport_url;
protected $access_token;
protected $user;
protected $userid;
protected $cookie_file;
protected $cache_file;
protected $cache_expiration;
protected $last_status;
/**
* Constructor
*
* @param string|null $username Your Nest username.
* @param string|null $password Your Nest password.
* @param string|null $issue_token Issue-token URL
* @param string|null $cookies Google cookies
*
* @throws InvalidArgumentException|UnexpectedValueException|RuntimeException
*/
public function __construct($username = NULL, $password = NULL, $issue_token = NULL, $cookies = NULL) {
if ($issue_token === NULL && defined('ISSUE_TOKEN')) {
$issue_token = constant('ISSUE_TOKEN');
}
if ($cookies === NULL && defined('COOKIES')) {
$cookies = constant('COOKIES');
}
if (!empty($issue_token)) {
$this->issue_token = $issue_token;
if (empty($cookies)) {
throw new InvalidArgumentException('Google login requires issue_token and cookie.');
}
$this->cookies = $cookies;
$this->cookie_file = static::getTempFile('cookies', md5($this->issue_token));
$this->cache_file = static::getTempFile('cache', md5($this->issue_token));
} else {
if ($username === NULL && defined('USERNAME')) {
$username = constant('USERNAME');
}
if ($password === NULL && defined('PASSWORD')) {
$password = constant('PASSWORD');
}
if ($username === NULL || $password === NULL) {
throw new InvalidArgumentException('Nest credentials were not provided.');
}
$this->username = $username;
$this->password = $password;
$this->cookie_file = static::getTempFile('cookies', md5($username . $password));
$this->cache_file = static::getTempFile('cache', md5($username . $password));
}
static::secureTouch($this->cookie_file);
static::secureTouch($this->cache_file);
// Attempt to load the cache
$this->loadCache();
// Log in, if needed
$this->login();
}
protected static function getTempFile($type, $suffix) {
$file = sys_get_temp_dir() . "/nest_php_{$type}_{$suffix}";
if (!is_file($file) || !is_writable($file)) {
if (function_exists('posix_geteuid')) {
// Use the posix function if available. This requires php-posix or php-process package
$u = posix_getpwuid(posix_geteuid());
$unix_user = $u['name'];
} else {
$unix_user = get_current_user();
}
$file = sys_get_temp_dir() . "/nest_php_{$type}_{$unix_user}_{$suffix}";
}
return $file;
}
/**
* Get the outside temperature & humidity, given a location (zip/postal code & optional country code).
*
* @param string $postal_code Zip or postal code
* @param string $country_code (Optional) Country code
*
* @return stdClass
*
* @throws RuntimeException
*/
public function getWeather($postal_code, $country_code = NULL) {
try {
$url = "https://home.nest.com/api/0.1/weather/forecast/$postal_code";
if (!empty($country_code)) {
$url .= ",$country_code";
}
$weather = $this->doGET($url);
} catch (RuntimeException $ex) {
// NESTAPI_ERROR_NOT_JSON_RESPONSE is kinda normal. The forecast API will often return a '502 Bad Gateway' response... meh.
if ($ex->getCode() != NESTAPI_ERROR_NOT_JSON_RESPONSE) {
throw new RuntimeException("Unexpected issue fetching forecast.", $ex->getCode(), $ex);
}
}
return (object) array(
'outside_temperature' => isset($weather->now->current_temperature) ? $this->temperatureInUserScale((float) $weather->now->current_temperature) : NULL,
'outside_humidity' => isset($weather->now->current_humidity) ? $weather->now->current_humidity : NULL
);
}
/**
* Get a list of all the locations configured in the Nest account.
*
* @return array
*/
public function getUserLocations() {
$this->prepareForGet();
$structures = (array) $this->last_status->structure;
$user_structures = array();
$class_name = get_class($this);
$topaz = isset($this->last_status->topaz) ? $this->last_status->topaz : array();
$kryptonite = isset($this->last_status->kryptonite) ? $this->last_status->kryptonite : array();
foreach ($structures as $struct_id => $structure) {
// Nest Protects at this location (structure)
$protects = array();
$sensors = array();
foreach ($topaz as $protect) {
if ($protect->structure_id == $struct_id) {
$protects[] = $protect->serial_number;
}
}
foreach ($kryptonite as $serial_number => $sensor) {
if ($sensor->structure_id == $struct_id) {
$sensors[] = $serial_number;
}
}
if (empty($protects) && empty($sensors) && empty($structure->devices)) {
continue;
}
$weather_data = $this->getWeather($structure->postal_code, $structure->country_code);
$user_structures[] = (object) array(
'name' => isset($structure->name)?$structure->name:'',
'address' => !empty($structure->street_address) ? $structure->street_address : NULL,
'city' => $structure->location,
'postal_code' => $structure->postal_code,
'country' => $structure->country_code,
'outside_temperature' => $weather_data->outside_temperature,
'outside_humidity' => $weather_data->outside_humidity,
'away' => $structure->away,
'away_last_changed' => !empty($structure->away_timestamp) ? date(DATETIME_FORMAT, $structure->away_timestamp) : NULL,
'thermostats' => array_map(array($class_name, 'cleanDevices'), $structure->devices),
'protects' => $protects,
'sensors' => $sensors,
);
}
return $user_structures;
}
/**
* Get the schedule details for the specified device.
*
* @param string $serial_number The device (thermostat or protect) serial number. Defaults to the first device of the account.
*
* @return array Returns as array, one element for each day of the week for which there has at least one scheduled event.
* Array keys are a textual representation of a day, three letters, as returned by `date('D')`.
* Array values are arrays of scheduled temperatures, including a time (in minutes after midnight),
* invoke_timestamp of when the event would be activated,
* and a mode (one of the TARGET_TEMP_MODE_* defines).
*/
public function getDeviceSchedule($serial_number = NULL) {
$this->prepareForGet();
$serial_number = $this->getDefaultSerial($serial_number);
$schedule_days = $this->last_status->schedule->{$serial_number}->days;
$schedule = array();
foreach ((array)$schedule_days as $day => $scheduled_events) {
$events = array();
foreach ($scheduled_events as $scheduled_event) {
if ($scheduled_event->entry_type == 'setpoint') {
$invoke_at = strtotime("{$this->days_maps[(int) $day]} 0:00:00") + $scheduled_event->time;
$events[(int)$scheduled_event->time] = (object) array(
'time' => $scheduled_event->time/60, // in minutes
'invoke_timestamp' => ($invoke_at >= time()) ? $invoke_at : strtotime("+7 days", $invoke_at),
'target_temperature' => $scheduled_event->type == 'RANGE' ? array($this->temperatureInUserScale((float)$scheduled_event->{'temp-min'}), $this->temperatureInUserScale((float)$scheduled_event->{'temp-max'})) : $this->temperatureInUserScale((float) $scheduled_event->temp),
'mode' => $scheduled_event->type == 'HEAT' ? TARGET_TEMP_MODE_HEAT : ($scheduled_event->type == 'COOL' ? TARGET_TEMP_MODE_COOL : TARGET_TEMP_MODE_RANGE)
);
}
}
if (!empty($events)) {
ksort($events);
$schedule[(int) $day] = array_values($events);
}
}
ksort($schedule);
$sorted_schedule = array();
foreach ($schedule as $day => $events) {
$sorted_schedule[$this->days_maps[(int) $day]] = $events;
}
return $sorted_schedule;
}
/**
* Get the next scheduled event.
*
* @param string $serial_number The device (thermostat or protect) serial number. Defaults to the first device of the account.
*
* @return stdClass|bool Returns the next scheduled event, or FALSE is there is none.
*/
public function getNextScheduledEvent($serial_number = NULL) {
$schedule = $this->getDeviceSchedule($serial_number);
$next_event = FALSE;
$time = date('H') * 60 + date('i');
for ($i = 0, $day = date('D'); $i++ < 7; $day = date('D', strtotime("+ $i days"))) {
if (isset($schedule[$day])) {
foreach ($schedule[$day] as $event) {
if ($event->time > $time) {
return $event;
}
}
}
$time = 0;
}
return $next_event;
}
/**
* Get the specified device (thermostat or protect) information.
*
* @param string $serial_number The device (thermostat, sensor, or protect) serial number. Defaults to the first device of the account.
*
* @return stdClass
*/
public function getDeviceInfo($serial_number = NULL) {
$this->prepareForGet();
$serial_number = $this->getDefaultSerial($serial_number);
$topaz = isset($this->last_status->topaz) ? $this->last_status->topaz : array();
$kryptonite = isset($this->last_status->kryptonite) ? $this->last_status->kryptonite : array();
foreach ($topaz as $protect) {
if ($serial_number == $protect->serial_number) {
// The specified device is a Nest Protect
$infos = (object) array(
'co_status' => $protect->co_status == 0 ? "OK" : $protect->co_status,
'co_previous_peak' => isset($protect->co_previous_peak) ? $protect->co_previous_peak : NULL,
'co_sequence_number' => $protect->co_sequence_number,
'smoke_status' => $protect->smoke_status == 0 ? "OK" : $protect->smoke_status,
'smoke_sequence_number' => $protect->smoke_sequence_number,
'model' => $protect->model,
'software_version' => $protect->software_version,
'line_power_present' => $protect->line_power_present,
'battery_level' => $protect->battery_level,
'battery_health_state' => $protect->battery_health_state == 0 ? "OK" : $protect->battery_health_state,
'wired_or_battery' => isset($protect->wired_or_battery) ? $protect->wired_or_battery : NULL,
'born_on_date' => isset($protect->device_born_on_date_utc_secs) ? date(DATE_FORMAT, $protect->device_born_on_date_utc_secs) : NULL,
'replace_by_date' => date(DATE_FORMAT, $protect->replace_by_date_utc_secs),
'last_update' => date(DATETIME_FORMAT, $protect->{'$timestamp'}/1000),
'last_manual_test' => $protect->latest_manual_test_start_utc_secs == 0 ? NULL : date(DATETIME_FORMAT, $protect->latest_manual_test_start_utc_secs),
'ntp_green_led_brightness' => isset($protect->ntp_green_led_brightness) ? $protect->ntp_green_led_brightness : NULL,
'tests_passed' => array(
'led' => $protect->component_led_test_passed,
'pir' => $protect->component_pir_test_passed,
'temp' => $protect->component_temp_test_passed,
'smoke' => $protect->component_smoke_test_passed,
'heat' => $protect->component_heat_test_passed,
'wifi' => $protect->component_wifi_test_passed,
'als' => $protect->component_als_test_passed,
'co' => $protect->component_co_test_passed,
'us' => $protect->component_us_test_passed,
'hum' => $protect->component_hum_test_passed,
'speaker' => isset($protect->component_speaker_test_passed) ? $protect->component_speaker_test_passed : NULL,
'buzzer' => isset($protect->component_buzzer_test_passed) ? $protect->component_buzzer_test_passed : NULL,
),
'nest_features' => array(
'night_time_promise' => !empty($protect->ntp_green_led_enable) ? $protect->ntp_green_led_enable : 0,
'night_light' => !empty($protect->night_light_enable) ? $protect->night_light_enable : 0,
'auto_away' => !empty($protect->auto_away) ? $protect->auto_away : 0,
'heads_up' => !empty($protect->heads_up_enable) ? $protect->heads_up_enable : 0,
'steam_detection' => !empty($protect->steam_detection_enable) ? $protect->steam_detection_enable : 0,
'home_alarm_link' => !empty($protect->home_alarm_link_capable) ? $protect->home_alarm_link_capable : 0,
'wired_led_enable' => !empty($protect->wired_led_enable) ? $protect->wired_led_enable : 0,
),
'serial_number' => $protect->serial_number,
'location' => $protect->structure_id,
'network' => (object) array(
'online' => $protect->component_wifi_test_passed,
'local_ip' => $protect->wifi_ip_address,
'mac_address' => $protect->wifi_mac_address
),
'name' => !empty($protect->description) ? $protect->description : DEVICE_WITH_NO_NAME,
'where' => $this->getWhereById($protect->spoken_where_id),
'color' => isset($protect->device_external_color) ? $protect->device_external_color : NULL,
);
return $infos;
}
}
foreach ($kryptonite as $sensor_serial => $sensor) {
if ($serial_number == $sensor_serial) {
// The specified device is a Nest Sensor
$infos = (object) array(
'temperature' => $this->temperatureInUserScale((float) $sensor->current_temperature),
'battery_level' => $sensor->battery_level,
'last_status' => date(DATETIME_FORMAT, $sensor->last_updated_at),
'location' => $sensor->structure_id,
'where' => $this->getWhereById($sensor->where_id),
'serial_number' => $sensor_serial,
);
return $infos;
}
}
list(, $structure) = explode('.', $this->last_status->link->{$serial_number}->structure);
$structure_away = $this->last_status->structure->{$structure}->away;
$mode = strtolower($this->last_status->device->{$serial_number}->current_schedule_mode);
$target_mode = $this->last_status->shared->{$serial_number}->target_temperature_type;
$eco_mode = $this->last_status->device->{$serial_number}->eco->mode; // manual-eco, auto-eco, schedule
if ($target_mode == TARGET_TEMP_MODE_OFF) {
$target_temperatures = FALSE; // No target due to it being off
$mode = TARGET_TEMP_MODE_OFF;
} elseif ($eco_mode !== "schedule") {
// We are in eco, thus not actively using the schedule
if ($this->last_status->device->{$serial_number}->away_temperature_low_enabled && $this->last_status->device->{$serial_number}->away_temperature_high_enabled) {
// We have both low and high temp eco temperatures
$mode = TARGET_TEMP_MODE_RANGE;
$target_temperatures = array(
$this->temperatureInUserScale((float) $this->last_status->device->{$serial_number}->away_temperature_low),
$this->temperatureInUserScale((float) $this->last_status->device->{$serial_number}->away_temperature_high)
);
} elseif ($this->last_status->device->{$serial_number}->away_temperature_low_enabled) {
// We have only an eco temp low, i.e. we're heating
$mode = TARGET_TEMP_MODE_HEAT;
$target_temperatures = $this->temperatureInUserScale((float) $this->last_status->device->{$serial_number}->away_temperature_low);
} elseif ($this->last_status->device->{$serial_number}->away_temperature_high_enabled) {
// We have only an eco temp high, i.e. we're cooling
$mode = TARGET_TEMP_MODE_COOL;
$target_temperatures = $this->temperatureInUserScale((float) $this->last_status->device->{$serial_number}->away_temperature_high);
} else {
// We're in eco with no away temperatures set, i.e. we're technically off (safety temps would still kick in)
$mode = TARGET_TEMP_MODE_OFF;
$target_temperatures = FALSE;
}
} elseif ($target_mode === 'range') {
$target_temperatures = array(
$this->temperatureInUserScale((float) $this->last_status->shared->{$serial_number}->target_temperature_low),
$this->temperatureInUserScale((float) $this->last_status->shared->{$serial_number}->target_temperature_high)
);
} else {
// It is either heat or cool mode
$target_temperatures = $this->temperatureInUserScale((float) $this->last_status->shared->{$serial_number}->target_temperature);
}
$current_modes = array();
$current_modes[] = $mode;
if ($eco_mode !== "schedule") {
$current_modes[] = $eco_mode;
}
if ($structure_away) {
$current_modes[] = 'away';
}
//Process sensors associated to this thermostat
$sensors = (object)array('all' => array(), 'active' => array(), 'active_temperatures' => array());
foreach ($this->last_status->rcs_settings->{$serial_number}->associated_rcs_sensors as $sensor_serial) {
$current_sensor = $this->getDeviceInfo(self::cleanDevices($sensor_serial));
$current_sensor->is_active = in_array($sensor_serial, $this->last_status->rcs_settings->{$serial_number}->active_rcs_sensors);
if ($current_sensor->is_active) {
$sensors->active[] = $current_sensor;
$sensors->active_temperatures[] = $current_sensor->temperature;
}
$sensors->all[] = $current_sensor;
}
$infos = (object) array(
'current_state' => (object) array(
'mode' => implode(',', $current_modes),
'temperature' => $this->temperatureInUserScale((float) $this->last_status->shared->{$serial_number}->current_temperature),
'backplate_temperature' => $this->temperatureInUserScale((float) $this->last_status->device->{$serial_number}->backplate_temperature),
'humidity' => $this->last_status->device->{$serial_number}->current_humidity,
'ac' => $this->last_status->shared->{$serial_number}->hvac_ac_state,
'heat' => $this->last_status->shared->{$serial_number}->hvac_heater_state,
'alt_heat' => $this->last_status->shared->{$serial_number}->hvac_alt_heat_state,
'hot_water' => isset($this->last_status->device->{$serial_number}->has_hot_water_control) ? $this->last_status->device->{$serial_number}->hot_water_active : NULL,
'auto_away' => $this->last_status->shared->{$serial_number}->auto_away, // -1 when disabled, 0 when enabled (thermostat can set auto-away), >0 when enabled and active (thermostat is currently in auto-away mode)
'manual_away' => $structure_away, //Leaving this for others - but manual away really doesn't exist anymore and should be removed eventually
'structure_away' => $structure_away,
'leaf' => $this->last_status->device->{$serial_number}->leaf,
'battery_level' => $this->last_status->device->{$serial_number}->battery_level,
'active_stages' => (object) array(
'heat' => (object) array(
'stage1' => $this->last_status->shared->{$serial_number}->hvac_heater_state,
'stage2' => $this->last_status->shared->{$serial_number}->hvac_heat_x2_state,
'stage3' => $this->last_status->shared->{$serial_number}->hvac_heat_x3_state,
'alt' => $this->last_status->shared->{$serial_number}->hvac_alt_heat_state,
'alt_stage2' => $this->last_status->shared->{$serial_number}->hvac_alt_heat_x2_state,
'aux' => $this->last_status->shared->{$serial_number}->hvac_aux_heater_state,
'emergency' => $this->last_status->shared->{$serial_number}->hvac_emer_heat_state,
),
'cool' => (object) array(
'stage1' => $this->last_status->shared->{$serial_number}->hvac_ac_state,
'stage2' => $this->last_status->shared->{$serial_number}->hvac_cool_x2_state,
'stage3' => $this->last_status->shared->{$serial_number}->hvac_cool_x3_state,
),
),
'eco_mode' => $eco_mode,
'eco_temperatures_assist_enabled' => $this->last_status->device->{$serial_number}->auto_away_enable,
'eco_temperatures' => (object) array(
'low' => ($this->last_status->device->{$serial_number}->away_temperature_low_enabled) ? $this->temperatureInUserScale((float)$this->last_status->device->{$serial_number}->away_temperature_low) : FALSE,
'high' => ($this->last_status->device->{$serial_number}->away_temperature_high_enabled) ? $this->temperatureInUserScale((float)$this->last_status->device->{$serial_number}->away_temperature_high) : FALSE,
),
'safety_temperatures' => (object) array(
'low' => ($this->last_status->device->{$serial_number}->lower_safety_temp_enabled) ? $this->temperatureInUserScale((float)$this->last_status->device->{$serial_number}->lower_safety_temp) : FALSE,
'high' => ($this->last_status->device->{$serial_number}->upper_safety_temp_enabled) ? $this->temperatureInUserScale((float)$this->last_status->device->{$serial_number}->upper_safety_temp) : FALSE,
),
),
'target' => (object) array(
'mode' => $target_mode,
'temperature' => $target_temperatures,
'time_to_target' => $this->last_status->device->{$serial_number}->time_to_target
),
'sensors' => $sensors,
'serial_number' => $this->last_status->device->{$serial_number}->serial_number,
'scale' => $this->last_status->device->{$serial_number}->temperature_scale,
'location' => $structure,
'network' => $this->getDeviceNetworkInfo($serial_number),
'name' => !empty($this->last_status->shared->{$serial_number}->name) ? $this->last_status->shared->{$serial_number}->name : DEVICE_WITH_NO_NAME,
'auto_cool' => ((int) $this->last_status->device->{$serial_number}->leaf_threshold_cool === 0) ? FALSE : ceil($this->temperatureInUserScale((float) $this->last_status->device->{$serial_number}->leaf_threshold_cool)),
'auto_heat' => ((int) $this->last_status->device->{$serial_number}->leaf_threshold_heat === 1000) ? FALSE : floor($this->temperatureInUserScale((float) $this->last_status->device->{$serial_number}->leaf_threshold_heat)),
'where' => isset($this->last_status->device->{$serial_number}->where_id) ? $this->getWhereById($this->last_status->device->{$serial_number}->where_id) : "",
);
if ($this->last_status->device->{$serial_number}->has_humidifier) {
$infos->current_state->humidifier = $this->last_status->device->{$serial_number}->humidifier_state;
$infos->target->humidity = $this->last_status->device->{$serial_number}->target_humidity;
$infos->target->humidity_enabled = $this->last_status->device->{$serial_number}->target_humidity_enabled;
}
if ($this->last_status->device->{$serial_number}->has_fan) {
//Retained the 'fan' attribute for LTS
$infos->current_state->fan = $this->last_status->shared->{$serial_number}->hvac_fan_state;
$infos->current_state->fan_info = (object) array(
'is_active' => $this->last_status->shared->{$serial_number}->hvac_fan_state,
'mode' => $this->last_status->device->{$serial_number}->fan_mode,
'current_speed' => $this->last_status->device->{$serial_number}->fan_current_speed,
'duty_cycle' => $this->last_status->device->{$serial_number}->fan_duty_cycle, //Run time per hour (in seconds)
//Seconds since midnight
'duty_start_time' => $this->last_status->device->{$serial_number}->fan_duty_start_time,
'duty_end_time' => $this->last_status->device->{$serial_number}->fan_duty_end_time,
//Seconds remaining
'timer_timeout' => $this->last_status->device->{$serial_number}->fan_timer_timeout > 0 ? $this->last_status->device->{$serial_number}->fan_timer_timeout - time() : false,
);
}
if (isset($this->last_status->demand_response->{$serial_number}->active_events)) {
$infos->demand_response = (object)array(
'has_active_event' => false,
'has_active_peak_period' => false,
'has_user_opted_out' => false,
'events' => $this->last_status->demand_response->{$serial_number}->active_events,
);
foreach ($infos->demand_response->events as &$event) {
$event = $this->getDemandResponseEventDetails($event->id);
$is_event_active = $event->start_time_utc <= time() && time() < $event->stop_time_utc;
$infos->demand_response->has_active_event = $infos->demand_response->has_active_event || $is_event_active;
$infos->demand_response->has_active_peak_period = $infos->demand_response->has_active_peak_period || $event->in_peak_period;
if ($event->in_peak_period || $is_event_active) {
$infos->demand_response->has_user_opted_out = $event->user_opted_out;
}
//Set these additional properties for non-breaking changes
$event->is_peak_period_active = $event->in_peak_period;
$event->is_event_active = $is_event_active;
}
}
return $infos;
}
/**
* Get the last 10 days energy report.
*
* @param string $serial_number The thermostat serial number. Defaults to the first device of the account.
*
* @return stdClass|bool
*/
public function getEnergyLatest($serial_number = NULL) {
$serial_number = $this->getDefaultSerial($serial_number);
$payload = array(
'objects' => array(
array('object_key' => "energy_latest.$serial_number")
)
);
$url = '/v5/subscribe';
return $this->doPOST($url, json_encode($payload));
}
/**
* Change the thermostat target mode and temperature
*
* @param string $mode One of the TARGET_TEMP_MODE_* constants.
* @param float|array $temperature Target temperature; specify a float when setting $mode = TARGET_TEMP_MODE_HEAT or TARGET_TEMP_MODE_COLD, and a array of two float values when setting $mode = TARGET_TEMP_MODE_RANGE. Not needed when setting $mode = TARGET_TEMP_MODE_OFF. Send NULL if you want to keep the previous temperature(s) value(s).
* @param string $serial_number The thermostat serial number. Defaults to the first device of the account.
*
* @return stdClass|bool The object returned by the API call, or FALSE on error.
*/
public function setTargetTemperatureMode($mode, $temperature = NULL, $serial_number = NULL) {
$serial_number = $this->getDefaultSerial($serial_number);
if ($temperature !== NULL) {
if ($mode == TARGET_TEMP_MODE_RANGE) {
if (!is_array($temperature) || count($temperature) != 2 || !is_numeric($temperature[0]) || !is_numeric($temperature[1])) {
echo "Error: when using TARGET_TEMP_MODE_RANGE, you need to set the target temperatures (second argument of setTargetTemperatureMode) using an array of two numeric values.\n";
return FALSE;
}
$temp_low = $this->temperatureInCelsius($temperature[0], $serial_number);
$temp_high = $this->temperatureInCelsius($temperature[1], $serial_number);
$data = json_encode(array('target_change_pending' => TRUE, 'target_temperature_low' => $temp_low, 'target_temperature_high' => $temp_high));
$set_temp_result = $this->doPOST("/v2/put/shared." . $serial_number, $data);
} elseif ($mode != TARGET_TEMP_MODE_OFF) {
// heat or cool
if (!is_numeric($temperature)) {
echo "Error: when using TARGET_TEMP_MODE_HEAT or TARGET_TEMP_MODE_COLD, you need to set the target temperature (second argument of setTargetTemperatureMode) using an numeric value.\n";
return FALSE;
}
$temperature = $this->temperatureInCelsius($temperature, $serial_number);
$data = json_encode(array('target_change_pending' => TRUE, 'target_temperature' => $temperature));
$set_temp_result = $this->doPOST("/v2/put/shared." . $serial_number, $data);
}
}
$data = json_encode(array('target_change_pending' => TRUE, 'target_temperature_type' => $mode));
return $this->doPOST("/v2/put/shared." . $serial_number, $data);
}
/**
* Change the thermostat target temperature, when the thermostat is not using a range.
*
* @param float $temperature Target temperature.
* @param string $serial_number The thermostat serial number. Defaults to the first device of the account.
*
* @return stdClass|bool The object returned by the API call, or FALSE on error.
*/
public function setTargetTemperature($temperature, $serial_number = NULL) {
$serial_number = $this->getDefaultSerial($serial_number);
$temperature = $this->temperatureInCelsius($temperature, $serial_number);
$data = json_encode(array('target_change_pending' => TRUE, 'target_temperature' => $temperature));
return $this->doPOST("/v2/put/shared." . $serial_number, $data);
}
/**
* Change the thermostat target temperatures, when the thermostat is using a range.
*
* @param float $temp_low Target low temperature.
* @param float $temp_high Target high temperature.
* @param string $serial_number The thermostat serial number. Defaults to the first device of the account.
*
* @return stdClass|bool The object returned by the API call, or FALSE on error.
*/
public function setTargetTemperatures($temp_low, $temp_high, $serial_number = NULL) {
$serial_number = $this->getDefaultSerial($serial_number);
$temp_low = $this->temperatureInCelsius($temp_low, $serial_number);
$temp_high = $this->temperatureInCelsius($temp_high, $serial_number);
$data = json_encode(array('target_change_pending' => TRUE, 'target_temperature_low' => $temp_low, 'target_temperature_high' => $temp_high));
return $this->doPOST("/v2/put/shared." . $serial_number, $data);
}
/**
* Change the thermostat target temperature device
*
* @param string $sensor_serial_number The thermostat or thermostat sensor serial number to use for target temperature
* @param string $thermostat_serial_number The thermostat serial number. Defaults to the first device of the account.
*
* @return stdClass|bool The object returned by the API call, or FALSE on error.
*/
public function setTargetTemperatureSensor($sensor_serial_number, $thermostat_serial_number = NULL) {
$thermostat_serial_number = $this->getDefaultSerial($thermostat_serial_number);
$payload = array(
'objects' => array(
array(
'object_key' => "rcs_settings.$thermostat_serial_number",
'op' => 'MERGE',
'value' => array(
'active_rcs_sensors' => array(),
'rcs_control_setting' => 'OFF',
),
),
)
);
if ($thermostat_serial_number !== $sensor_serial_number) {
$payload['objects'][0]['value'] = array(
'active_rcs_sensors' => array("kryptonite.$sensor_serial_number"),
'rcs_control_setting' => 'OVERRIDE',
);
}
return $this->doPOST('/v5/put', json_encode($payload));
}
/**
* Get demand response event details
*
* @param string $event_id The Nest demand response event id
*
* @return stdClass|bool The event detail object returned by the API call, or FALSE on error.
*/
protected function getDemandResponseEventDetails($event_id) {
$payload = array(
'objects' => array(
array('object_key' => $event_id)
)
);
$event_response = $this->doPOST('/v5/subscribe', json_encode($payload));
if (empty($event_response->objects)) {
return $event_response;
}
$event_detail = $event_response->objects[0]->value;
//Note: cruise_control_temperature is 0/32 until the event is active
$event_detail->cruise_control_temperature = $this->temperatureInUserScale($event_detail->cruise_control_temperature);
return $event_detail;
}
/**
* Set the thermostat to use ECO mode ($mode = ECO_MODE_MANUAL) or not ($mode = ECO_MODE_SCHEDULE).
*
* @param string $mode One of the ECO_MODE_* constants.
* @param string $serial_number The thermostat serial number. Defaults to the first device of the account.
*
* @return stdClass|bool The object returned by the API call, or FALSE on error.
*/
public function setEcoMode($mode, $serial_number = NULL) {
$serial_number = $this->getDefaultSerial($serial_number);
$data = array();
$data['mode'] = $mode;
$data['touched_by'] = 4;
$data['mode_update_timestamp'] = time();
$data = json_encode(array('eco' => $data));
return $this->doPOST("/v2/put/device." . $serial_number, $data);
}
/**
* (Deprecated) Change the thermostat away temperatures. This method is an alias for setEcoTemperatures().
*
* @param float $temp_low Away low temperature.
* @param float $temp_high Away high temperature.
* @param string $serial_number The thermostat serial number. Defaults to the first device of the account.
*
* @return stdClass|bool The object returned by the API call, or FALSE on error.
*
* @deprecated
* @see Nest::setEcoTemperatures()
*/
public function setAwayTemperatures($temp_low, $temp_high, $serial_number = NULL) {
return $this->setEcoTemperatures($temp_low, $temp_high, $serial_number);
}
/**
* Change the thermostat ECO temperatures.
*
* @param float|bool $temp_low ECO low temperature. Use FALSE to turn it Off (only the safety minimum temperature will apply).
* @param float|bool $temp_high ECO high temperature. Use FALSE to turn it Off (only the safety maximum temperature will apply).
* @param string $serial_number The thermostat serial number. Defaults to the first device of the account.
*
* @return stdClass|bool The object returned by the API call, or FALSE on error.
*/
public function setEcoTemperatures($temp_low, $temp_high, $serial_number = NULL) {
$serial_number = $this->getDefaultSerial($serial_number);
$temp_low = $this->temperatureInCelsius($temp_low, $serial_number);
$temp_high = $this->temperatureInCelsius($temp_high, $serial_number);
$data = array();
if ($temp_low === FALSE) {
$data['away_temperature_low_enabled'] = FALSE;
} elseif ($temp_low != NULL) {
$data['away_temperature_low_enabled'] = TRUE;
$data['away_temperature_low'] = $temp_low;
}
if ($temp_high === FALSE) {
$data['away_temperature_high_enabled'] = FALSE;
} elseif ($temp_high != NULL) {
$data['away_temperature_high_enabled'] = TRUE;
$data['away_temperature_high'] = $temp_high;
}
$data = json_encode($data);
return $this->doPOST("/v2/put/device." . $serial_number, $data);
}
/**
* Change the thermostat safety temperatures.
*
* @param float|bool $temp_low Safety low temperature. Use FALSE to turn it Off (not recommended)
* @param float|bool $temp_high Safety high temperature. Use FALSE to turn it Off (not recommended)
* @param string $serial_number The thermostat serial number. Defaults to the first device of the account.
*
* @return stdClass|bool The object returned by the API call, or FALSE on error.
*/
public function setSafetyTemperatures($temp_low, $temp_high, $serial_number = NULL) {
$serial_number = $this->getDefaultSerial($serial_number);
$temp_low = $this->temperatureInCelsius($temp_low, $serial_number);
$temp_high = $this->temperatureInCelsius($temp_high, $serial_number);
$data = array();
if ($temp_low === FALSE) {
$data['lower_safety_temp_enabled'] = FALSE;
} elseif ($temp_low != NULL) {
$data['lower_safety_temp_enabled'] = TRUE;
$data['lower_safety_temp'] = $temp_low;
}
if ($temp_high === FALSE) {
$data['upper_safety_temp_enabled'] = FALSE;
} elseif ($temp_high != NULL) {
$data['upper_safety_temp_enabled'] = TRUE;
$data['upper_safety_temp'] = $temp_high;
}
$data = json_encode($data);
return $this->doPOST("/v2/put/device." . $serial_number, $data);
}
/**
* Set the thermostat-controlled fan mode.
*
* @param string|array $mode One of the following constants: FAN_MODE_AUTO, FAN_MODE_ON, FAN_MODE_EVERY_DAY_ON or FAN_MODE_EVERY_DAY_OFF.
* @param string $serial_number The thermostat serial number. Defaults to the first device of the account.
*
* @return stdClass|bool The object returned by the API call, or FALSE on error.
*
* @throws InvalidArgumentException
*/
public function setFanMode($mode, $serial_number = NULL) {
$duty_cycle = NULL;
$timer = NULL;
if (is_array($mode)) {
$modes = $mode;
$mode = $modes[0];
if (count($modes) > 1) {
if ($mode == FAN_MODE_MINUTES_PER_HOUR) {
$duty_cycle = (int) $modes[1];
} else {
$timer = (int) $modes[1];
}
} else {
throw new InvalidArgumentException("setFanMode(array \$mode[, ...]) needs at least a mode and a value in the \$mode array.");
}
} elseif (!is_string($mode)) {
throw new InvalidArgumentException("setFanMode() can only take a string or an array as it's first parameter.");
}
return $this->_setFanMode($mode, $duty_cycle, $timer, $serial_number);
}
/**
* Set the thermostat-controlled fan to be ON for a specific number of minutes each hour.
*
* @param string|array $mode One of the FAN_MODE_MINUTES_PER_HOUR_* constants.
* @param string $serial_number The thermostat serial number. Defaults to the first device of the account.
*
* @return stdClass|bool The object returned by the API call, or FALSE on error.
*/
public function setFanModeMinutesPerHour($mode, $serial_number = NULL) {
$modes = explode(',', $mode);
$mode = $modes[0];
$duty_cycle = $modes[1];
return $this->_setFanMode($mode, $duty_cycle, NULL, $serial_number);
}
/**
* Set the thermostat-controlled fan to be ON using a timer.
*
* @param string|array $mode One of the FAN_TIMER_* constants.
* @param string $serial_number The thermostat serial number. Defaults to the first device of the account.
*
* @return stdClass|bool The object returned by the API call, or FALSE on error.
*/
public function setFanModeOnWithTimer($mode, $serial_number = NULL) {
$modes = explode(',', $mode);
$mode = $modes[0];
$timer = (int) $modes[1];
return $this->_setFanMode($mode, NULL, $timer, $serial_number);
}
/**
* Cancels the timer for the thermostat-controlled fan.
*
* @param string $serial_number The thermostat serial number. Defaults to the first device of the account.
*
* @return stdClass|bool The object returned by the API call, or FALSE on error.
*/
public function cancelFanModeOnWithTimer($serial_number = NULL) {
$serial_number = $this->getDefaultSerial($serial_number);
$data = json_encode(array('fan_timer_timeout' => 0));
return $this->doPOST("/v2/put/device." . $serial_number, $data);
}
/**
* Set the thermostat-controlled fan to run only between the specified hours.
*
* @param int $start_hour When the fan should start.
* @param int $end_hour When the fan should stop.
* @param string $serial_number The thermostat serial number. Defaults to the first device of the account.
*
* @return stdClass|bool The object returned by the API call, or FALSE on error.
*/
public function setFanEveryDaySchedule($start_hour, $end_hour, $serial_number = NULL) {
$serial_number = $this->getDefaultSerial($serial_number);
$data = json_encode(array('fan_duty_start_time' => $start_hour*3600, 'fan_duty_end_time' => $end_hour*3600));
return $this->doPOST("/v2/put/device." . $serial_number, $data);
}
/**
* Turn off the thermostat (no heating, cooling or fan).
*
* @param string $serial_number The thermostat serial number. Defaults to the first device of the account.
*
* @return stdClass|bool The object returned by the API call, or FALSE on error.
*/
public function turnOff($serial_number = NULL) {
return $this->setTargetTemperatureMode(TARGET_TEMP_MODE_OFF, 0, $serial_number);
}
/**
* Change the location (structure) to away or present. Can also set the specified thermostat to use ECO temperatures, when enabling Away mode.
*
* @param string $away_mode AWAY_MODE_ON or AWAY_MODE_OFF
* @param string $serial_number The thermostat serial number. Defaults to the first device of the account.
* @param bool $eco_when_away Specify if you want to use Eco temperatures or not, when using AWAY_MODE_ON. Default to TRUE.
*
* @return stdClass|bool The object returned by the API call, or FALSE on error.
*/
public function setAway($away_mode, $serial_number = NULL, $eco_when_away = TRUE) {
$serial_number = $this->getDefaultSerial($serial_number);
$data = json_encode(array('away' => $away_mode, 'away_timestamp' => time(), 'away_setter' => 0));
$structure_id = $this->getDeviceInfo($serial_number)->location;
if ($away_mode == AWAY_MODE_ON && $eco_when_away) {
$this->setEcoMode(ECO_MODE_MANUAL, $serial_number);
} else {
$this->setEcoMode(ECO_MODE_SCHEDULE, $serial_number);
}
return $this->doPOST("/v2/put/structure." . $structure_id, $data);
}
/**
* (Deprecated) Enable or disable Nest Sense Auto-Away.
*
* @param bool $enabled True to enable auto-away.
* @param string $serial_number The thermostat serial number. Defaults to the first device of the account.
*
* @return stdClass|bool The object returned by the API call, or FALSE on error.
*
* @deprecated Nest Sense Auto-Away is not available anymore. This now controls if the thermostat should use Eco temperatures if it detects you are away.
* @see Nest::useEcoTempWhenAway()
*/
public function setAutoAwayEnabled($enabled, $serial_number = NULL) {
return $this->useEcoTempWhenAway($enabled, $serial_number);
}
/**
* Enable or disable using Eco temperatures when you're Away.
*
* @param bool $enabled True to enable Eco temperatures when Away.
* @param string $serial_number The thermostat serial number. Defaults to the first device of the account.
*
* @return stdClass|bool The object returned by the API call, or FALSE on error.
*/
public function useEcoTempWhenAway($enabled, $serial_number = NULL) {
$serial_number = $this->getDefaultSerial($serial_number);
$data = json_encode(array('auto_away_enable' => $enabled));
return $this->doPOST("/v2/put/device." . $serial_number, $data);
}
/**
* Change the dual-fuel breakpoint temperature.
*
* @param float|string $breakpoint DUALFUEL_BREAKPOINT_ALWAYS_PRIMARY, DUALFUEL_BREAKPOINT_ALWAYS_ALT, or a temperature: thermostat will force usage of alt-heating when the outside temperature is below this value.
* @param string $serial_number The thermostat serial number. Defaults to the first device of the account.
*
* @return stdClass|bool The object returned by the API call, or FALSE on error.
*/
public function setDualFuelBreakpoint($breakpoint, $serial_number = NULL) {
$serial_number = $this->getDefaultSerial($serial_number);
if (!is_string($breakpoint)) {
$breakpoint = $this->temperatureInCelsius($breakpoint, $serial_number);
$data = json_encode(array('dual_fuel_breakpoint_override' => 'none', 'dual_fuel_breakpoint' => $breakpoint));
} else {
$data = json_encode(array('dual_fuel_breakpoint_override' => $breakpoint));
}
return $this->doPOST("/v2/put/device." . $serial_number, $data);
}
/**
* Enable or disable Nest Sense Humidifier.
*
* @param bool $enabled True to enable auto-away.
* @param string $serial_number The thermostat serial number. Defaults to the first device of the account.
*
* @return stdClass|bool The object returned by the API call, or FALSE on error.
*/
public function enableHumidifier($enabled, $serial_number = NULL) {
$serial_number = $this->getDefaultSerial($serial_number);
$data = json_encode(array('target_humidity_enabled' => ((boolean)$enabled)));
return $this->doPOST("/v2/put/device." . $serial_number, $data);
}
/**
* Change the dual-fuel breakpoint temperature.
*
* @param float $humidity The target humidity value.
* @param string $serial_number The thermostat serial number. Defaults to the first device of the account.
*
* @return stdClass|bool The object returned by the API call, or FALSE on error.
*/
public function setHumidity($humidity, $serial_number = NULL) {
$serial_number = $this->getDefaultSerial($serial_number);
$data = json_encode(array('target_humidity' => ((double)$humidity)));
return $this->doPOST("/v2/put/device." . $serial_number, $data);
}
/**
* Convert a temperature value from the device-prefered scale to Celsius.
*
* @param float $temperature The temperature to convert.
* @param string $serial_number The thermostat serial number. Defaults to the first device of the account.
*
* @return float Temperature in Celsius.
*/
public function temperatureInCelsius($temperature, $serial_number = NULL) {
$serial_number = $this->getDefaultSerial($serial_number);
$temp_scale = $this->getDeviceTemperatureScale($serial_number);
if ($temp_scale == 'F') {
return ($temperature - 32) / 1.8;
}
return $temperature;
}
/**
* Convert a temperature value from Celsius to the device-preferred scale.
*
* @param float $temperature_in_celsius The temperature to convert.
* @param string $serial_number The thermostat serial number. Defaults to the first device of the account.