forked from home-assistant/homebridge-homeassistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
198 lines (166 loc) · 8.01 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
var Service, Characteristic;
var url = require('url')
, request = require('request')
, EventSource = require('eventsource');
var communicationError = new Error('Can not communicate with Home Assistant.');
var HomeAssistantBinarySensorFactory;
var HomeAssistantCoverFactory;
var HomeAssistantFan;
var HomeAssistantLight;
var HomeAssistantLock;
var HomeAssistantMediaPlayer;
var HomeAssistantSensorFactory;
var HomeAssistantSwitch;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
HomeAssistantLight = require('./accessories/light')(Service, Characteristic, communicationError);
HomeAssistantSwitch = require('./accessories/switch')(Service, Characteristic, communicationError);
HomeAssistantLock = require('./accessories/lock')(Service, Characteristic, communicationError);
HomeAssistantMediaPlayer = require('./accessories/media_player')(Service, Characteristic, communicationError);
HomeAssistantFan = require('./accessories/fan')(Service, Characteristic, communicationError);
HomeAssistantCoverFactory = require('./accessories/cover')(Service, Characteristic, communicationError);
HomeAssistantSensorFactory = require('./accessories/sensor')(Service, Characteristic, communicationError);
HomeAssistantBinarySensorFactory = require('./accessories/binary_sensor')(Service, Characteristic, communicationError);
homebridge.registerPlatform('homebridge-homeassistant', 'HomeAssistant', HomeAssistantPlatform, false);
};
function HomeAssistantPlatform(log, config, api){
// auth info
this.host = config.host;
this.password = config.password;
this.supportedTypes = config.supported_types || ['binary_sensor', 'cover', 'fan', 'input_boolean', 'light', 'lock', 'media_player', 'scene', 'sensor', 'switch'];
this.foundAccessories = [];
this.logging = config.logging || true;
this.log = log;
if (api) {
// Save the API object as plugin needs to register new accessory via this object.
this.api = api;
}
var es = new EventSource(config.host + '/api/stream?api_password=' + encodeURIComponent(this.password));
es.addEventListener('message', function(e) {
if (this.logging)
this.log('Received event: ' + e.data);
if (e.data == 'ping')
return;
var data = JSON.parse(e.data);
if (data.event_type != 'state_changed')
return;
var numAccessories = this.foundAccessories.length;
for (var i = 0; i < numAccessories; i++) {
var accessory = this.foundAccessories[i];
if (accessory.entity_id == data.data.entity_id && accessory.onEvent)
accessory.onEvent(data.data.old_state, data.data.new_state);
}
}.bind(this));
}
HomeAssistantPlatform.prototype = {
_request: function(method, path, options, callback) {
var requestURL = this.host + '/api' + path;
options = options || {};
options.query = options.query || {};
var reqOpts = {
url: url.parse(requestURL),
method: method || 'GET',
qs: options.query,
body: JSON.stringify(options.body),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'x-ha-access': this.password
}
};
request(reqOpts, function onResponse(error, response, body) {
if (error) {
callback(error, response);
return;
}
if (response.statusCode === 401) {
callback(new Error('You are not authenticated'), response);
return;
}
callback(error, response, JSON.parse(body));
});
},
fetchState: function(entity_id, callback){
this._request('GET', '/states/' + entity_id, {}, function(error, response, data){
if (error) {
callback(null);
}else{
callback(data);
}
});
},
callService: function(domain, service, service_data, callback){
var options = {};
options.body = service_data;
this._request('POST', '/services/' + domain + '/' + service, options, function(error, response, data){
if (error) {
callback(null);
}else{
callback(data);
}
});
},
accessories: function(callback) {
this.log('Fetching HomeAssistant devices.');
var that = this;
this._request('GET', '/states', {}, function(error, response, data){
if (error) {
that.log('Failed getting devices: ' + error + '. Retrying...');
setTimeout(function() { that.accessories(callback); }, 5000);
return;
}
for (var i = 0; i < data.length; i++) {
var entity = data[i];
var entity_type = entity.entity_id.split('.')[0];
// ignore devices that are not in the list of supported types
if (that.supportedTypes.indexOf(entity_type) == -1) {
continue;
}
// ignore hidden devices
if (entity.attributes && entity.attributes.hidden) {
continue;
}
// ignore homebridge hidden devices
if (entity.attributes && entity.attributes.homebridge_hidden) {
continue;
}
// support providing custom names
if (entity.attributes && entity.attributes.homebridge_name) {
entity.attributes.friendly_name = entity.attributes.homebridge_name;
}
var accessory = null;
if (entity_type == 'light') {
accessory = new HomeAssistantLight(that.log, entity, that);
}else if (entity_type == 'switch'){
accessory = new HomeAssistantSwitch(that.log, entity, that);
}else if (entity_type == 'lock'){
accessory = new HomeAssistantLock(that.log, entity, that);
}else if (entity_type == 'garage_door'){
that.log.error('Garage_doors are no longer supported by homebridge-homeassistant. Please upgrade to a newer version of Home Assistant to continue using this entity (with the new cover component).');
}else if (entity_type == 'scene'){
accessory = new HomeAssistantSwitch(that.log, entity, that, 'scene');
}else if (entity_type == 'rollershutter'){
that.log.error('Rollershutters are no longer supported by homebridge-homeassistant. Please upgrade to a newer version of Home Assistant to continue using this entity (with the new cover component).');
}else if (entity_type == 'media_player' && entity.attributes && entity.attributes.supported_media_commands){
accessory = new HomeAssistantMediaPlayer(that.log, entity, that);
}else if (entity_type == 'input_boolean'){
accessory = new HomeAssistantSwitch(that.log, entity, that, 'input_boolean');
}else if (entity_type == 'fan'){
accessory = new HomeAssistantFan(that.log, entity, that);
}else if (entity_type == 'cover'){
accessory = HomeAssistantCoverFactory(that.log, entity, that);
}else if (entity_type == 'sensor'){
accessory = HomeAssistantSensorFactory(that.log, entity, that);
}else if (entity_type == 'binary_sensor' && entity.attributes && entity.attributes.sensor_class) {
accessory = HomeAssistantBinarySensorFactory(that.log, entity, that);
}
if (accessory) {
that.foundAccessories.push(accessory);
}
}
callback(that.foundAccessories);
});
}
};
module.exports.platform = HomeAssistantPlatform;