forked from tonesto7/homebridge-smartthings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·141 lines (125 loc) · 5.53 KB
/
index.js
File metadata and controls
executable file
·141 lines (125 loc) · 5.53 KB
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
var smartthings = require('./lib/smartthingsapi');
var Service, Characteristic, Accessory, uuid, EnergyCharacteristics;
var SmartThingsAccessory;
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
Accessory = homebridge.hap.Accessory;
uuid = homebridge.hap.uuid;
SmartThingsAccessory = require('./accessories/smartthings')(Accessory, Service, Characteristic, uuid);
homebridge.registerPlatform("homebridge-smartthings", "SmartThings", SmartThingsPlatform);
};
function SmartThingsPlatform(log, config) {
// Load Wink Authentication From Config File
this.app_url = config["app_url"];
this.app_id = config["app_id"];
this.access_token = config["access_token"];
//This is how often it does a full refresh
this.polling_seconds = config["polling_seconds"];
if (!this.polling_seconds) this.polling_seconds=60;
//This is how often it polls for subscription data.
this.update_seconds = config["update_seconds"];
if (!this.update_seconds) this.update_seconds=1;
this.api = smartthings;
this.log = log;
this.deviceLookup = {};
this.firstpoll = true;
this.attributeLookup = {}
}
SmartThingsPlatform.prototype = {
reloadData: function (callback) {
var that = this;
var foundAccessories = [];
this.log.debug("Refreshing All Device Data");
smartthings.getDevices(function (myList) {
that.log.debug("Received All Device Data");
// success
if (myList && myList.deviceList && myList.deviceList instanceof Array) {
var populateDevices = function(devices) {
for (var i = 0; i < devices.length; i++) {
var device = devices[i];
var accessory = undefined;
if (that.deviceLookup[device.deviceid]) {
accessory = that.deviceLookup[device.deviceid];
accessory.loadData(devices[i]);
} else {
accessory = new SmartThingsAccessory(that, device);
if (accessory != undefined) {
if ((accessory.services.length<=1)||(accessory.deviceGroup=="unknown")) {
if (that.firstpoll) that.log("Device Skipped - Group " + accessory.deviceGroup + ", Name " + accessory.name+ ", ID " + accessory.deviceid+", JSON: "+ JSON.stringify(device));
} else {
that.log("Device Added - Group " + accessory.deviceGroup + ", Name " + accessory.name + ", ID " + accessory.deviceid)//+", JSON: "+ JSON.stringify(device));
that.deviceLookup[accessory.deviceid] = accessory;
foundAccessories.push(accessory);
}
}
}
}
}
if (myList && myList.location) {
that.temperature_unit = myList.location.temperature_scale;
}
populateDevices(myList.deviceList);
} else if ((!myList)||(!myList.error)) {
that.log ("Invalid Response from API call");
} else if (myList.error) {
that.log ("Error received type " + myList.type+' - '+myList.message);
} else {
that.log ("Invalid Response from API call");
}
if (callback)
callback(foundAccessories)
that.firstpoll=false;
});
},
accessories: function (callback) {
this.log("Fetching Smart Things devices.");
var that = this;
var foundAccessories = [];
this.deviceLookup = [];
this.unknownCapabilities = [];
this.knownCapabilities = ["Switch","Color Control","Battery","Polling","Lock","Refresh","Lock Codes","Sensor","Actuator",
"Configuration","Switch Level","Temperature Measurement","Motion Sensor","Color Temperature",
"Contact Sensor","Three Axis","Acceleration Sensor","Momentary","Door Control","Garage Door Control",
"Relative Humidity Measurement","Presence Sensor","Thermostat", "Energy Meter", "Power Meter",
"Thermostat Cooling Setpoint","Thermostat Mode","Thermostat Fan Mode","Thermostat Operating State",
"Thermostat Heating Setpoint","Thermostat Setpoint","Indicator"];
this.temperature_unit = 'F';
smartthings.init(this.app_url, this.app_id, this.access_token);
this.reloadData(function(foundAccessories) {
that.log("Unknown Capabilities: " + JSON.stringify(that.unknownCapabilities));
callback(foundAccessories);
setInterval(that.reloadData.bind(that), that.polling_seconds*1000);
setInterval(that.doIncrementalUpdate.bind(that), that.update_seconds*1000);
});
},
addAttributeUsage(attribute, deviceid, mycharacteristic) {
if (!this.attributeLookup[attribute])
this.attributeLookup[attribute]={};
if (!this.attributeLookup[attribute][deviceid])
this.attributeLookup[attribute][deviceid]=[];
this.attributeLookup[attribute][deviceid].push(mycharacteristic);
},
doIncrementalUpdate() {
var that=this;
var processIncrementalUpdate = function(data) {
if (data && data.attributes && data.attributes instanceof Array) {
for (var i = 0; i < data.attributes.length; i++) {
var attributeSet = data.attributes[i];
if (!((that.attributeLookup[attributeSet.attribute])&&(that.attributeLookup[attributeSet.attribute][attributeSet.device]))) return;
var myUsage = that.attributeLookup[attributeSet.attribute][attributeSet.device];
if (myUsage instanceof Array) {
for (var j = 0; j < myUsage.length; j++) {
var accessory = that.deviceLookup[attributeSet.device];
if (accessory) {
accessory.device.attributes[attributeSet.attribute]=attributeSet.value;
myUsage[j].getValue();
}
}
}
}
}
}
smartthings.getUpdates(processIncrementalUpdate);
}
};