mirror of
				https://github.com/beestat/app.git
				synced 2025-11-03 18:37:01 -05:00 
			
		
		
		
	Had some issues with my sensor sync. Also fixed minor bug with the return value of the thermostat sync.
		
			
				
	
	
		
			265 lines
		
	
	
		
			8.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			265 lines
		
	
	
		
			8.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
/**
 | 
						|
 * An ecobee sensor. This just has a few simple properties like name,
 | 
						|
 * temperature, humidity, etc.
 | 
						|
 *
 | 
						|
 * @author Jon Ziebell
 | 
						|
 */
 | 
						|
class ecobee_sensor extends cora\crud {
 | 
						|
 | 
						|
  public static $exposed = [
 | 
						|
    'private' => [
 | 
						|
      'read_id'
 | 
						|
    ],
 | 
						|
    'public' => []
 | 
						|
  ];
 | 
						|
 | 
						|
  public static $converged = [];
 | 
						|
 | 
						|
  public static $user_locked = true;
 | 
						|
 | 
						|
  /**
 | 
						|
   * Sync sensors.
 | 
						|
   */
 | 
						|
  public function sync() {
 | 
						|
    // Get the thermostat list from ecobee with sensors. Keep this identical to
 | 
						|
    // ecobee_thermostat->sync() to leverage caching.
 | 
						|
    $response = $this->api(
 | 
						|
      'ecobee',
 | 
						|
      'ecobee_api',
 | 
						|
      [
 | 
						|
        'method' => 'GET',
 | 
						|
        'endpoint' => 'thermostat',
 | 
						|
        'arguments' => [
 | 
						|
          'body' => json_encode([
 | 
						|
            'selection' => [
 | 
						|
              'selectionType' => 'registered',
 | 
						|
              'selectionMatch' => '',
 | 
						|
              'includeRuntime' => true,
 | 
						|
              'includeExtendedRuntime' => true,
 | 
						|
              'includeElectricity' => true,
 | 
						|
              'includeSettings' => true,
 | 
						|
              'includeLocation' => true,
 | 
						|
              'includeProgram' => true,
 | 
						|
              'includeEvents' => true,
 | 
						|
              'includeDevice' => true,
 | 
						|
              'includeTechnician' => true,
 | 
						|
              'includeUtility' => true,
 | 
						|
              'includeManagement' => true,
 | 
						|
              'includeAlerts' => true,
 | 
						|
              'includeWeather' => true,
 | 
						|
              'includeHouseDetails' => true,
 | 
						|
              'includeOemCfg' => true,
 | 
						|
              'includeEquipmentStatus' => true,
 | 
						|
              'includeNotificationSettings' => true,
 | 
						|
              'includeVersion' => true,
 | 
						|
              'includePrivacy' => true,
 | 
						|
              'includeAudio' => true,
 | 
						|
              'includeSensors' => true
 | 
						|
 | 
						|
              /**
 | 
						|
               * 'includeReminders' => true
 | 
						|
               *
 | 
						|
               * While documented, this is not available for general API use
 | 
						|
               * unless you are a technician user.
 | 
						|
               *
 | 
						|
               * The reminders and the includeReminders flag are something extra
 | 
						|
               * for ecobee Technicians. It allows them to set and receive
 | 
						|
               * reminders with more detail than the usual alert reminder type.
 | 
						|
               * These reminders are only available to Technician users, which
 | 
						|
               * is why you aren't seeing any new information when you set that
 | 
						|
               * flag to true. Thanks for pointing out the lack of documentation
 | 
						|
               * regarding this. We'll get this updated as soon as possible.
 | 
						|
               *
 | 
						|
               *
 | 
						|
               * https://getsatisfaction.com/api/topics/what-does-includereminders-do-when-calling-get-thermostat?rfm=1
 | 
						|
               */
 | 
						|
 | 
						|
              /**
 | 
						|
               * 'includeSecuritySettings' => true
 | 
						|
               *
 | 
						|
               * While documented, this is not made available for general API
 | 
						|
               * use unless you are a utility. If you try to include this an
 | 
						|
               * "Authentication failed" error will be returned.
 | 
						|
               *
 | 
						|
               * Special accounts such as Utilities are permitted an alternate
 | 
						|
               * method of authorization using implicit authorization. This
 | 
						|
               * method permits the Utility application to authorize against
 | 
						|
               * their own specific account without the requirement of a PIN.
 | 
						|
               * This method is limited to special contractual obligations and
 | 
						|
               * is not available for 3rd party applications who are not
 | 
						|
               * Utilities.
 | 
						|
               *
 | 
						|
               * https://www.ecobee.com/home/developer/api/documentation/v1/objects/SecuritySettings.shtml
 | 
						|
               * https://www.ecobee.com/home/developer/api/documentation/v1/auth/auth-intro.shtml
 | 
						|
               *
 | 
						|
               */
 | 
						|
            ]
 | 
						|
          ])
 | 
						|
        ]
 | 
						|
      ]
 | 
						|
    );
 | 
						|
 | 
						|
    // Loop over the returned sensors and create/update them as necessary.
 | 
						|
    $sensor_ids_to_keep = [];
 | 
						|
    foreach($response['thermostatList'] as $thermostat_api) {
 | 
						|
      $guid = sha1($thermostat_api['identifier'] . $thermostat_api['runtime']['firstConnected']);
 | 
						|
 | 
						|
      $ecobee_thermostat = $this->api(
 | 
						|
        'ecobee_thermostat',
 | 
						|
        'get',
 | 
						|
        [
 | 
						|
          'attributes' => [
 | 
						|
            'guid' => $guid
 | 
						|
          ]
 | 
						|
        ]
 | 
						|
      );
 | 
						|
 | 
						|
      $thermostat = $this->api(
 | 
						|
        'thermostat',
 | 
						|
        'get',
 | 
						|
        [
 | 
						|
          'attributes' => [
 | 
						|
            'ecobee_thermostat_id' => $ecobee_thermostat['ecobee_thermostat_id']
 | 
						|
          ]
 | 
						|
        ]
 | 
						|
      );
 | 
						|
 | 
						|
      foreach($thermostat_api['remoteSensors'] as $api_sensor) {
 | 
						|
        $ecobee_sensor = $this->get(
 | 
						|
          [
 | 
						|
            'ecobee_thermostat_id' => $ecobee_thermostat['ecobee_thermostat_id'],
 | 
						|
            'identifier' => $api_sensor['id']
 | 
						|
          ]
 | 
						|
        );
 | 
						|
 | 
						|
        if ($ecobee_sensor !== null) {
 | 
						|
          // Sensor exists.
 | 
						|
          $sensor = $this->api(
 | 
						|
            'sensor',
 | 
						|
            'get',
 | 
						|
            [
 | 
						|
              'attributes' => [
 | 
						|
                'ecobee_sensor_id' => $ecobee_sensor['ecobee_sensor_id']
 | 
						|
              ]
 | 
						|
            ]
 | 
						|
          );
 | 
						|
        }
 | 
						|
        else {
 | 
						|
          // Sensor does not exist.
 | 
						|
          $ecobee_sensor = $this->create([
 | 
						|
            'ecobee_thermostat_id' => $ecobee_thermostat['ecobee_thermostat_id'],
 | 
						|
            'identifier' => $api_sensor['id']
 | 
						|
          ]);
 | 
						|
          $sensor = $this->api(
 | 
						|
            'sensor',
 | 
						|
            'create',
 | 
						|
            [
 | 
						|
              'attributes' => [
 | 
						|
                'ecobee_sensor_id' => $ecobee_sensor['ecobee_sensor_id'],
 | 
						|
                'thermostat_id' => $thermostat['thermostat_id']
 | 
						|
              ]
 | 
						|
            ]
 | 
						|
          );
 | 
						|
        }
 | 
						|
 | 
						|
        $sensor_ids_to_keep[] = $sensor['sensor_id'];
 | 
						|
 | 
						|
        $this->update(
 | 
						|
          [
 | 
						|
            'ecobee_sensor_id' => $ecobee_sensor['ecobee_sensor_id'],
 | 
						|
            'name' => $api_sensor['name'],
 | 
						|
            'type' => $api_sensor['type'],
 | 
						|
            'code' => (isset($api_sensor['code']) === true ? $api_sensor['code'] : null),
 | 
						|
            'in_use' => ($api_sensor['inUse'] === true ? 1 : 0),
 | 
						|
            'json_capability' => $api_sensor['capability'],
 | 
						|
            'inactive' => 0
 | 
						|
          ]
 | 
						|
        );
 | 
						|
 | 
						|
        $attributes = [];
 | 
						|
        $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;
 | 
						|
        $attributes['occupancy'] = null;
 | 
						|
        foreach($api_sensor['capability'] as $capability) {
 | 
						|
          switch($capability['type']) {
 | 
						|
            case 'temperature':
 | 
						|
              if(
 | 
						|
                is_numeric($capability['value']) === true &&
 | 
						|
                $capability['value'] <= 999.99 &&
 | 
						|
                $capability['value'] >= -999.99
 | 
						|
              ) {
 | 
						|
                $attributes['temperature'] = $capability['value'] / 10;
 | 
						|
              } else {
 | 
						|
                $attributes['temperature'] = null;
 | 
						|
              }
 | 
						|
            break;
 | 
						|
            case 'humidity':
 | 
						|
              if(
 | 
						|
                is_numeric($capability['value']) === true &&
 | 
						|
                $capability['value'] <= 100 &&
 | 
						|
                $capability['value'] >= 0
 | 
						|
              ) {
 | 
						|
                $attributes['humidity'] = $capability['value'] / 10;
 | 
						|
              } else {
 | 
						|
                $attributes['humidity'] = null;
 | 
						|
              }
 | 
						|
            break;
 | 
						|
            case 'occupancy':
 | 
						|
              $attributes['occupancy'] = $capability['value'] === "true";
 | 
						|
            break;
 | 
						|
          }
 | 
						|
 | 
						|
          // Update the sensor.
 | 
						|
          $this->api(
 | 
						|
            'sensor',
 | 
						|
            'update',
 | 
						|
            [
 | 
						|
              'attributes' => array_merge(
 | 
						|
                ['sensor_id' => $sensor['sensor_id']],
 | 
						|
                $attributes
 | 
						|
              )
 | 
						|
            ]
 | 
						|
          );
 | 
						|
        }
 | 
						|
      }
 | 
						|
    }
 | 
						|
 | 
						|
    // Inactivate any sensors that were no longer returned.
 | 
						|
    $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' => $sensor['ecobee_sensor_id'],
 | 
						|
            'inactive' => 1
 | 
						|
          ]
 | 
						|
        );
 | 
						|
 | 
						|
        $this->api(
 | 
						|
          'sensor',
 | 
						|
          'update',
 | 
						|
          [
 | 
						|
            'attributes' => [
 | 
						|
              'sensor_id' => $sensor['sensor_id'],
 | 
						|
              'inactive' => 1
 | 
						|
            ]
 | 
						|
          ]
 | 
						|
        );
 | 
						|
      } else {
 | 
						|
        $ecobee_sensor_ids_to_return[] = $sensor['ecobee_sensor_id'];
 | 
						|
      }
 | 
						|
    }
 | 
						|
 | 
						|
    return $this->read_id(['ecobee_sensor_id' => $ecobee_sensor_ids_to_return]);
 | 
						|
  }
 | 
						|
 | 
						|
}
 |