From ed62657e54f28700acc6b9918a9affad2bb43ad6 Mon Sep 17 00:00:00 2001 From: Jon Ziebell Date: Mon, 10 Jun 2019 21:00:57 -0400 Subject: [PATCH] Fixed #125 Removed sensors are not being properly removed Had some issues with my sensor sync. Also fixed minor bug with the return value of the thermostat sync. --- api/ecobee_sensor.php | 20 +++++++++++--------- api/ecobee_thermostat.php | 9 +++++---- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/api/ecobee_sensor.php b/api/ecobee_sensor.php index 3e21d0a..64acfc4 100644 --- a/api/ecobee_sensor.php +++ b/api/ecobee_sensor.php @@ -102,7 +102,7 @@ class ecobee_sensor extends cora\crud { ); // Loop over the returned sensors and create/update them as necessary. - $ecobee_sensor_ids_to_keep = []; + $sensor_ids_to_keep = []; foreach($response['thermostatList'] as $thermostat_api) { $guid = sha1($thermostat_api['identifier'] . $thermostat_api['runtime']['firstConnected']); @@ -164,7 +164,7 @@ class ecobee_sensor extends cora\crud { ); } - $ecobee_sensor_ids_to_keep[] = $ecobee_sensor['ecobee_sensor_id']; + $sensor_ids_to_keep[] = $sensor['sensor_id']; $this->update( [ @@ -182,6 +182,7 @@ class ecobee_sensor extends cora\crud { $attributes['name'] = $api_sensor['name']; $attributes['type'] = $api_sensor['type']; $attributes['in_use'] = $api_sensor['inUse']; + $attributes['inactive'] = 0; $attributes['temperature'] = null; $attributes['humidity'] = null; @@ -231,12 +232,13 @@ class ecobee_sensor extends cora\crud { } // Inactivate any sensors that were no longer returned. - $ecobee_sensors = $this->read(); - foreach($ecobee_sensors as $ecobee_sensor) { - if(in_array($ecobee_sensor['ecobee_sensor_id'], $ecobee_sensor_ids_to_keep) === false) { + $sensors = $this->api('sensor', 'read'); + $ecobee_sensor_ids_to_return = []; + foreach($sensors as $sensor) { + if(in_array($sensor['sensor_id'], $sensor_ids_to_keep) === false) { $this->update( [ - 'ecobee_sensor_id' => $ecobee_sensor['ecobee_sensor_id'], + 'ecobee_sensor_id' => $sensor['ecobee_sensor_id'], 'inactive' => 1 ] ); @@ -251,12 +253,12 @@ class ecobee_sensor extends cora\crud { ] ] ); + } else { + $ecobee_sensor_ids_to_return[] = $sensor['ecobee_sensor_id']; } } - $return = $this->read_id(['ecobee_sensor_id' => $ecobee_sensor_ids_to_keep]); - - return $return; + return $this->read_id(['ecobee_sensor_id' => $ecobee_sensor_ids_to_return]); } } diff --git a/api/ecobee_thermostat.php b/api/ecobee_thermostat.php index eaaaea5..b2c7d55 100644 --- a/api/ecobee_thermostat.php +++ b/api/ecobee_thermostat.php @@ -102,7 +102,7 @@ class ecobee_thermostat extends cora\crud { ); // Loop over the returned thermostats and create/update them as necessary. - $ecobee_thermostat_ids_to_keep = []; + $thermostat_ids_to_keep = []; foreach($response['thermostatList'] as $api_thermostat) { $guid = sha1($api_thermostat['identifier'] . $api_thermostat['runtime']['firstConnected']); @@ -141,7 +141,6 @@ class ecobee_thermostat extends cora\crud { ); } - // $ecobee_thermostat_ids_to_keep[] = $ecobee_thermostat['ecobee_thermostat_id']; $thermostat_ids_to_keep[] = $thermostat['thermostat_id']; $ecobee_thermostat = $this->update( @@ -262,6 +261,7 @@ class ecobee_thermostat extends cora\crud { // Inactivate any ecobee_thermostats that were no longer returned. $thermostats = $this->api('thermostat', 'read'); + $ecobee_thermostat_ids_to_return = []; foreach($thermostats as $thermostat) { if(in_array($thermostat['thermostat_id'], $thermostat_ids_to_keep) === false) { $this->update( @@ -281,11 +281,12 @@ class ecobee_thermostat extends cora\crud { ], ] ); - + } else { + $ecobee_thermostat_ids_to_return[] = $thermostat['ecobee_thermostat_id']; } } - return $this->read_id(['ecobee_thermostat_id' => $ecobee_thermostat_ids_to_keep]); + return $this->read_id(['ecobee_thermostat_id' => $ecobee_thermostat_ids_to_return]); } /**