diff --git a/api/ecobee.php b/api/ecobee.php index 8f5b937..b7c2de5 100644 --- a/api/ecobee.php +++ b/api/ecobee.php @@ -235,8 +235,7 @@ class ecobee extends external_api { if($this::$log_mysql !== 'all') { $this->log_mysql($curl_response, true); } - $this->api('ecobee_token', 'delete', $ecobee_token['ecobee_token_id']); - throw new cora\exception('Ecobee access was revoked by user.', 10508, false, null, false); + throw new cora\exception('Ecobee access was revoked by user.', 10508, false, $response['status']['message'], false); } else if (isset($response['status']) === true && $response['status']['code'] === 9) { // Invalid selection. No thermostats in selection. Ensure permissions and selection. diff --git a/api/ecobee_thermostat.php b/api/ecobee_thermostat.php index 758f237..c83e5da 100644 --- a/api/ecobee_thermostat.php +++ b/api/ecobee_thermostat.php @@ -214,25 +214,60 @@ class ecobee_thermostat extends cora\crud { $identifiers = array_unique(array_merge($registered_identifiers, $manual_identifiers)); // Get all of the thermostats from ecobee. - $response = $this->api( - 'ecobee', - 'ecobee_api', - [ - 'method' => 'GET', - 'endpoint' => 'thermostat', - 'arguments' => [ - 'body' => json_encode([ - 'selection' => array_merge( - [ - 'selectionType' => 'thermostats', - 'selectionMatch' => implode(',', $identifiers) - ], - $include - ) - ]) + try { + $response = $this->api( + 'ecobee', + 'ecobee_api', + [ + 'method' => 'GET', + 'endpoint' => 'thermostat', + 'arguments' => [ + 'body' => json_encode([ + 'selection' => array_merge( + [ + 'selectionType' => 'thermostats', + 'selectionMatch' => implode(',', $identifiers) + ], + $include + ) + ]) + ] ] - ] - ); + ); + } catch(cora\exception $e) { + if($e->getCode() === 10508) { + // If there was an authorization failure, remove those identifiers. + // These are likely thermostats that have been removed from the account + // and are no longer accessible. + + // Use regex to extract numbers + preg_match_all('/\d+/', $e->getExtraInfo(), $matches); + + $exclude_identifiers = $matches[0]; + + $identifiers = array_diff($identifiers, $exclude_identifiers); + + $response = $this->api( + 'ecobee', + 'ecobee_api', + [ + 'method' => 'GET', + 'endpoint' => 'thermostat', + 'arguments' => [ + 'body' => json_encode([ + 'selection' => array_merge( + [ + 'selectionType' => 'thermostats', + 'selectionMatch' => implode(',', $identifiers) + ], + $include + ) + ]) + ] + ] + ); + } + } // Loop over the returned thermostats and create/update them as necessary. $thermostat_ids_to_keep = [];