-
Notifications
You must be signed in to change notification settings - Fork 77
/
plugin.js
215 lines (187 loc) · 7.8 KB
/
plugin.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"use strict";
// var debug = require('debug')('alexaPlugin');
var AlexaLocal = require('./lib/alexaLocal.js').alexaLocal;
var alexaActions = require('./lib/alexaActions.js');
var EventEmitter = require('events').EventEmitter;
var os = require("os");
const packageConfig = require('./package.json');
let Service, Characteristic;
var options = {};
var alexaService;
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerPlatform("homebridge-alexa", "Alexa", alexaHome);
};
function alexaHome(log, config, api) {
this.log = log;
this.eventBus = new EventEmitter();
this.config = config;
this.pin = config['pin'] || "031-45-154";
this.username = config['username'] || false;
this.password = config['password'] || false;
this.filter = config['filter'];
this.beta = config['beta'] || false;
this.events = config['routines'] || false;
this.combine = config['combine'] || false;
this.oldParser = config['oldParser'] || false;
this.refresh = config['refresh'] || 60 * 15; // Value in seconds, default every 15 minute's
this.speakers = config['speakers'] || false; // Array of speaker devices
this.inputs = config['inputs'] || false; // Array of input devices
this.channel = config['channel'] || false; // Array of input devices
this.blind = config['blind'] || false; // Use range controller for Blinds
this.thermostatTurnOn = config['thermostatTurnOn'] || 0; // Default to unsupported
this.deviceListHandling = config['deviceListHandling'] || []; // Use ea
this.deviceList = config['deviceList'] || []; // Use ea
this.door = config['door'] || false; // Use mode controller for Garage Doors
this.name = config['name'] || "Alexa";
this.mergeServiceName = config['mergeServiceName'] || false;
this.CloudTransport = config['CloudTransport'] || "mqtts"; // Default to mqtts Transport
this.LegacyCloudTransport = config['LegacyCloudTransport'] || false; // Default to new Transport ( Setting from discarded beta )
var mqttKeepalive = config['keepalive'] || 5; // MQTT Connection Keepalive
if (mqttKeepalive < 60) {
this.keepalive = mqttKeepalive * 60;
} else {
this.keepalive = mqttKeepalive;
}
if (this.CloudTransport && ['mqtt', 'wss', 'mqtts'].includes(this.CloudTransport)) {
// Okay
} else {
this.log.error("ERROR: Invalid CloudTransport setting, defaulting to mqtts.");
this.CloudTransport = "mqtts";
}
// Enable config based DEBUG logging enable
this.debug = config['debug'] || false;
if (this.debug) {
let debugEnable = require('debug');
let namespaces = debugEnable.disable();
// this.log("DEBUG-1", namespaces);
if (namespaces) {
namespaces = namespaces + ',alexa*';
} else {
namespaces = 'alexa*';
}
// this.log("DEBUG-2", namespaces);
debugEnable.enable(namespaces);
}
if (!this.username || !this.password) {
this.log.error("Missing username and password");
}
if (this.oldParser) {
this.log.error("ERROR: oldParser was deprecated with version 0.5.0, defaulting to new Parser.");
}
if (api) {
this.api = api;
this.api.on('didFinishLaunching', this.didFinishLaunching.bind(this));
}
this.log.info(
'%s v%s, node %s, homebridge v%s',
packageConfig.name, packageConfig.version, process.version, api.serverVersion
);
}
alexaHome.prototype = {
accessories: function (callback) {
// this.log("Accessories");
var accessories = [];
accessories.push(new AlexaService(this.name, this.log));
callback(accessories);
}
};
alexaHome.prototype.didFinishLaunching = function () {
var host = (this.CloudTransport === 'wss' ? 'www.homebridge.ca' : 'alexa.homebridge.ca');
var reconnectPeriod = 65000; // Increased reconnect period to allow DDOS protection to reset
if (this.beta) {
host = 'clone.homebridge.ca';
}
options = {
// Shared Options
log: this.log,
debug: this.debug,
// MQTT Options
mqttURL: (this.CloudTransport === 'wss' ? "wss://" + host + "/ws" : (this.CloudTransport === 'mqtts' ? "mqtts://" + host + ":8883/" : "mqtt://" + host + ":1883/")),
transport: this.CloudTransport,
mqttOptions: {
username: this.username,
password: this.password,
reconnectPeriod: reconnectPeriod, // Increased reconnect period to allow DDOS protection to reset
keepalive: (this.CloudTransport === 'wss' ? 55 : this.keepalive), // Keep alive not used when using WSS Transport
rejectUnauthorized: false
},
// HAP Node Client options
pin: this.pin,
refresh: this.refresh,
eventBus: this.eventBus,
// HB Parser options
oldParser: this.oldParser,
combine: this.combine,
speakers: this.speakers,
filter: this.filter,
alexaService: alexaService,
Characteristic: Characteristic,
inputs: this.inputs,
channel: this.channel,
thermostatTurnOn: this.thermostatTurnOn,
blind: this.blind,
deviceListHandling: this.deviceListHandling,
deviceList: this.deviceList,
door: this.door,
mergeServiceName: this.mergeServiceName,
// Other Options
events: this.events
};
// Initialize HAP Connections
alexaActions.hapDiscovery(options);
var alexa = new AlexaLocal(options);
// Homebridge HAP Node Events
this.eventBus.on('hapEvent', alexaActions.alexaEvent.bind(this));
// Alexa mesages
this.eventBus.on('System', function (message) {
this.log.error("ERROR:", message.directive.header.message);
}.bind(this));
this.eventBus.on('Warning', function (message) {
this.log.warn("Warning:", message.directive.header.message);
}.bind(this));
this.eventBus.on('Information', function (message) {
this.log("Info:", message.directive.header.message);
}.bind(this));
this.eventBus.on('Alexa', alexaActions.alexaMessage.bind(this));
this.eventBus.on('Alexa.Discovery', alexaActions.alexaDiscovery.bind(this));
this.eventBus.on('Alexa.PowerController', alexaActions.alexaPowerController.bind(this));
this.eventBus.on('Alexa.PowerLevelController', alexaActions.alexaPowerLevelController.bind(this));
this.eventBus.on('Alexa.ColorController', alexaActions.alexaColorController.bind(this));
this.eventBus.on('Alexa.ColorTemperatureController', alexaActions.alexaColorTemperatureController.bind(this));
this.eventBus.on('Alexa.PlaybackController', alexaActions.alexaPlaybackController.bind(this));
this.eventBus.on('Alexa.Speaker', alexaActions.alexaSpeaker.bind(this));
this.eventBus.on('Alexa.ThermostatController', alexaActions.alexaThermostatController.bind(this));
this.eventBus.on('Alexa.LockController', alexaActions.alexaLockController.bind(this));
this.eventBus.on('Alexa.ChannelController', alexaActions.alexaChannelController.bind(this));
this.eventBus.on('Alexa.StepSpeaker', alexaActions.alexaStepSpeaker.bind(this));
this.eventBus.on('Alexa.InputController', alexaActions.alexaInputController.bind(this));
this.eventBus.on('Alexa.ModeController', alexaActions.alexaModeController.bind(this));
this.eventBus.on('Alexa.RangeController', alexaActions.alexaRangeController.bind(this));
};
/*
alexaHome.prototype.configureAccessory = function(accessory) {
this.log("configureAccessory");
// callback();
};
*/
function AlexaService(name, log) {
this.name = name;
this.log = log;
}
AlexaService.prototype = {
getServices: function () {
// this.log("getServices", this.name);
// Information Service
var informationService = new Service.AccessoryInformation();
var hostname = os.hostname();
informationService
.setCharacteristic(Characteristic.Manufacturer, "homebridge-alexa")
.setCharacteristic(Characteristic.SerialNumber, hostname)
.setCharacteristic(Characteristic.FirmwareRevision, require('./package.json').version);
// Thermostat Service
alexaService = new Service.ContactSensor(this.name);
return [informationService, alexaService];
}
};