-
Notifications
You must be signed in to change notification settings - Fork 111
/
index.js
180 lines (149 loc) · 7.99 KB
/
index.js
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
'use strict';
const axios = require('axios');
const NestConnection = require('./lib/nest-connection');
let ThermostatAccessory, HomeAwayAccessory, TempSensorAccessory, ProtectAccessory, LockAccessory;
require('promise.prototype.finally').shim(Promise);
Promise.delay = function(time_ms) {
return new Promise(resolve => setTimeout(resolve, time_ms));
};
Promise.prototype.asCallback = function(callback) {
this.then(res => callback(null, res)).catch(err => callback(err));
};
Promise.prototype.return = function(val) {
this.then(function() {
return val;
});
};
class NestPlatform {
constructor(log, config, api) {
// auth info
this.config = config;
this.log = log;
this.api = api;
this.accessoryLookup = {};
this.cachedAccessories = [];
api.on('didFinishLaunching', async () => {
this.log('Fetching Nest devices.');
const generateAccessories = function(data) {
const foundAccessories = [];
const loadDevices = function(DeviceType) {
const disableFlags = {
'thermostat': 'Thermostat.Disable',
'temp_sensor': 'TempSensor.Disable',
'protect': 'Protect.Disable',
'home_away_sensor': 'HomeAway.Disable',
'lock': 'Lock.Disable'
};
const devices = (data.devices && data.devices[DeviceType.deviceGroup]) || {};
for (const deviceId of Object.keys(devices)) {
const device = devices[deviceId];
const serialNumber = device.serial_number;
if (!this.optionSet(disableFlags[DeviceType.deviceType], serialNumber, deviceId)) {
const structureId = device.structure_id;
if (this.config.structureId && this.config.structureId !== structureId) {
this.log('Skipping device ' + deviceId + ' because it is not in the required structure. Has ' + structureId + ', looking for ' + this.config.structureId + '.');
continue;
}
const structure = data.structures[structureId];
const accessory = new DeviceType(this.conn, this.log, device, structure, this);
this.accessoryLookup[deviceId] = accessory;
foundAccessories.push(accessory);
}
}
}.bind(this);
loadDevices(ThermostatAccessory);
loadDevices(HomeAwayAccessory);
loadDevices(TempSensorAccessory);
loadDevices(ProtectAccessory);
loadDevices(LockAccessory);
this.conn.accessories = this.accessoryLookup;
return foundAccessories;
}.bind(this);
const updateAccessories = function(data, accList) {
accList.map(function(acc) {
const device = data.devices[acc.deviceGroup][acc.deviceId];
if (device) {
const structureId = device.structure_id;
const structure = data.structures[structureId];
acc.updateData(device, structure);
}
});
};
const handleUpdates = function(data) {
if (Object.keys(this.accessoryLookup).length > 0) {
updateAccessories(data, this.accessoryLookup);
}
}.bind(this);
try {
this.conn = await this.setupConnection(this.optionSet('Debug.Verbose'), this.optionSet('Nest.FieldTest.Enable'));
await this.conn.subscribe(handleUpdates);
await this.conn.observe(handleUpdates);
let initialState = this.conn.apiResponseToObjectTree(this.conn.currentState);
this.accessoryLookup = generateAccessories(initialState);
this.api.unregisterPlatformAccessories('homebridge-nest', 'Nest', this.cachedAccessories);
this.cachedAccessories = [];
this.api.registerPlatformAccessories('homebridge-nest', 'Nest', this.accessoryLookup.map(el => el.accessory));
let accessoriesMounted = this.accessoryLookup.map(el => el.constructor.name);
if (this.config.readyCallback) {
axios.post(this.config.readyCallback, {
thermostat_count: accessoriesMounted.filter(el => el == 'NestThermostatAccessory').length,
tempsensor_count: accessoriesMounted.filter(el => el == 'NestTempSensorAccessory').length,
protect_count: accessoriesMounted.filter(el => el == 'NestProtectAccessory').length,
lock_count: accessoriesMounted.filter(el => el == 'NestLockAccessory').length
}).catch(() => { });
}
} catch(err) {
this.log.error(err);
this.log.error('NOTE: Because we couldn\'t connect to the Nest service, your Nest devices in HomeKit will not be responsive.');
this.cachedAccessories.forEach(accessory => {
accessory.services.forEach(service => {
service.characteristics.forEach(characteristic => {
characteristic.on('get', callback => callback('error'));
characteristic.on('set', (value, callback) => callback('error'));
characteristic.value;
});
});
});
}
});
}
configureAccessory(accessory) {
this.cachedAccessories.push(accessory);
}
optionSet(key, serialNumber, deviceId) {
return key && this.config.options && (this.config.options.includes(key) || (serialNumber && this.config.options.includes(key + '.' + serialNumber)) || (deviceId && this.config.options.includes(key + '.' + deviceId)));
}
async setupConnection(verbose, fieldTestMode) {
if (!this.config.access_token && !this.config.googleAuth && !this.config.refreshToken && (!this.config.email || !this.config.password)) {
throw('You did not specify your Nest account credentials {\'email\',\'password\'}, or an access_token, refreshToken, or googleAuth, in config.json');
}
if (this.config.googleAuth && this.config.refreshToken) {
throw('You have specified both googleAuth and refreshToken in config.json. Please pick the one you want to use, and remove the other one');
}
if (this.config.googleAuth && (!this.config.googleAuth.issueToken || !this.config.googleAuth.cookies)) { // || !this.config.googleAuth.apiKey)) {
throw('When using googleAuth, you must provide issueToken and cookies in config.json. Please see README.md for instructions');
}
const conn = new NestConnection(this.config, this.log, verbose, fieldTestMode);
if (await conn.auth()) {
return conn;
} else {
throw('Unable to authenticate with Google/Nest.');
}
}
}
module.exports = function(homebridge) {
const exportedTypes = {
Accessory: homebridge.platformAccessory,
Service: homebridge.hap.Service,
Characteristic: homebridge.hap.Characteristic,
hap: homebridge.hap,
uuid: homebridge.hap.uuid
};
require('./lib/nest-device-accessory')(exportedTypes);
ThermostatAccessory = require('./lib/nest-thermostat-accessory')();
HomeAwayAccessory = require('./lib/nest-homeaway-accessory')();
TempSensorAccessory = require('./lib/nest-tempsensor-accessory')();
ProtectAccessory = require('./lib/nest-protect-accessory')();
LockAccessory = require('./lib/nest-lock-accessory')();
homebridge.registerPlatform('homebridge-nest', 'Nest', NestPlatform);
};