-
Notifications
You must be signed in to change notification settings - Fork 185
/
index.js
250 lines (233 loc) · 9.64 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
const TuyaOpenAPI = require("./lib/tuyaopenapi");
const TuyaSHOpenAPI = require("./lib/tuyashopenapi");
const TuyaOpenMQ = require("./lib/tuyamqttapi");
const OutletAccessory = require('./lib/outlet_accessory');
const LightAccessory = require('./lib/light_accessory');
const SwitchAccessory = require('./lib/switch_accessory');
const SmokeSensorAccessory = require('./lib/smokesensor_accessory');
const Fanv2Accessory = require('./lib/fanv2_accessory');
const HeaterAccessory = require('./lib/heater_accessory');
const GarageDoorAccessory = require('./lib/garagedoor_accessory');
const AirPurifierAccessory = require('./lib/air_purifier_accessory')
const WindowCoveringAccessory = require('./lib/window_covering_accessory')
const ContactSensorAccessory = require('./lib/contactsensor_accessory');
const LeakSensorAccessory = require('./lib/leak_sensor_accessory')
const LogUtil = require('./util/logutil')
const DataUtil = require('./util/datautil')
var Accessory, Service, Characteristic;
module.exports = function (homebridge) {
Accessory = homebridge.platformAccessory;
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
// registerAccessory' three parameters is plugin-name, accessory-name, constructor-name
homebridge.registerPlatform('homebridge-tuya-platform', 'TuyaPlatform', TuyaPlatform, true);
}
// Accessory constructor
class TuyaPlatform {
constructor(log, config, api) {
this.log = new LogUtil(
config.options.debug,
);
this.config = config;
if (!config || !config.options) {
this.log.log('The config configuration is incorrect, disabling plugin.')
return;
}
this.deviceAccessories = new Map();
this.accessories = new Map();
if (api) {
// Save the API object as plugin needs to register new accessory via this object
this.api = api;
// Listen to event "didFinishLaunching", this means homebridge already finished loading cached accessories.
// Platform Plugin should only register new accessory that doesn't exist in homebridge after this event.
// Or start discover new accessories.
this.api.on('didFinishLaunching', function () {
this.log.log("Initializing TuyaPlatform...");
this.initTuyaSDK(config);
}.bind(this));
}
}
async initTuyaSDK(config) {
let devices
let api
if (config.options.projectType == '1') {
api = new TuyaOpenAPI(
config.options.endPoint,
config.options.accessId,
config.options.accessKey,
this.log,
);
this.tuyaOpenApi = api;
//login before everything start
await api.login(config.options.username, config.options.password);
//init Mqtt service and register some Listener
try {
devices = await api.getDeviceList();
} catch (e) {
// this.log.log(JSON.stringify(e.message));
this.log.log('Failed to get device information. Please check if the config.json is correct.')
return;
}
} else {
api = new TuyaSHOpenAPI(
config.options.accessId,
config.options.accessKey,
config.options.username,
config.options.password,
config.options.countryCode,
config.options.appSchema,
this.log,
);
this.tuyaOpenApi = api;
try {
devices = await api.getDevices()
} catch (e) {
// this.log.log(JSON.stringify(e.message));
this.log.log('Failed to get device information. Please check if the config.json is correct.')
return;
}
}
for (const device of devices) {
this.addAccessory(device);
}
const type = config.options.projectType == "1" ? "2.0" : "1.0"
let mq = new TuyaOpenMQ(api, type, this.log);
this.tuyaOpenMQ = mq;
this.tuyaOpenMQ.start();
this.tuyaOpenMQ.addMessageListener(this.onMQTTMessage.bind(this));
}
addAccessory(device) {
var deviceType = device.category;
this.log.log(`Adding: ${device.name || 'unnamed'} (${deviceType} / ${device.id})`);
// Get UUID
const uuid = this.api.hap.uuid.generate(device.id);
const homebridgeAccessory = this.accessories.get(uuid);
// Construct new accessory
let deviceAccessory;
switch (deviceType) {
case 'kj':
deviceAccessory = new AirPurifierAccessory(this, homebridgeAccessory, device);
this.accessories.set(uuid, deviceAccessory.homebridgeAccessory);
this.deviceAccessories.set(uuid, deviceAccessory);
break;
case 'dj':
case 'dd':
case 'fwd':
case 'tgq':
case 'xdd':
case 'dc':
case 'tgkg':
deviceAccessory = new LightAccessory(this, homebridgeAccessory, device);
this.accessories.set(uuid, deviceAccessory.homebridgeAccessory);
this.deviceAccessories.set(uuid, deviceAccessory);
break;
case 'cz':
case 'pc':
var deviceData = new DataUtil().getSubService(device.status)
deviceAccessory = new OutletAccessory(this, homebridgeAccessory, device, deviceData);
this.accessories.set(uuid, deviceAccessory.homebridgeAccessory);
this.deviceAccessories.set(uuid, deviceAccessory);
break;
case 'kg':
case 'tdq':
var deviceData = new DataUtil().getSubService(device.status)
deviceAccessory = new SwitchAccessory(this, homebridgeAccessory, device, deviceData);
this.accessories.set(uuid, deviceAccessory.homebridgeAccessory);
this.deviceAccessories.set(uuid, deviceAccessory);
break;
case 'fs':
case 'fskg':
deviceAccessory = new Fanv2Accessory(this, homebridgeAccessory, device);
this.accessories.set(uuid, deviceAccessory.homebridgeAccessory);
this.deviceAccessories.set(uuid, deviceAccessory);
break;
case 'ywbj':
deviceAccessory = new SmokeSensorAccessory(this, homebridgeAccessory, device);
this.accessories.set(uuid, deviceAccessory.homebridgeAccessory);
this.deviceAccessories.set(uuid, deviceAccessory);
break;
case 'qn':
deviceAccessory = new HeaterAccessory(this, homebridgeAccessory, device);
this.accessories.set(uuid, deviceAccessory.homebridgeAccessory);
this.deviceAccessories.set(uuid, deviceAccessory);
break;
case 'ckmkzq': //garage_door_opener
deviceAccessory = new GarageDoorAccessory(this, homebridgeAccessory, device);
this.accessories.set(uuid, deviceAccessory.homebridgeAccessory);
this.deviceAccessories.set(uuid, deviceAccessory);
break;
case 'cl':
deviceAccessory = new WindowCoveringAccessory(this, homebridgeAccessory, device);
this.accessories.set(uuid, deviceAccessory.homebridgeAccessory);
this.deviceAccessories.set(uuid, deviceAccessory);
break;
case 'mcs':
deviceAccessory = new ContactSensorAccessory(this, homebridgeAccessory, device);
this.accessories.set(uuid, deviceAccessory.homebridgeAccessory);
this.deviceAccessories.set(uuid, deviceAccessory);
break;
case 'rqbj':
case 'jwbj':
deviceAccessory = new LeakSensorAccessory(this, homebridgeAccessory, device);
this.accessories.set(uuid, deviceAccessory.homebridgeAccessory);
this.deviceAccessories.set(uuid, deviceAccessory);
break;
default:
break;
}
}
//Handle device deletion, addition, status update
async onMQTTMessage(message) {
if (message.bizCode) {
if (message.bizCode == 'delete') {
const uuid = this.api.hap.uuid.generate(message.devId);
const homebridgeAccessory = this.accessories.get(uuid);
this.removeAccessory(homebridgeAccessory)
} else if (message.bizCode == 'bindUser') {
let deviceInfo = await this.tuyaOpenApi.getDeviceInfo(message.bizData.devId)
let functions = await this.tuyaOpenApi.getDeviceFunctions(message.bizData.devId)
let device = Object.assign(deviceInfo, functions);
this.addAccessory(device)
}
} else {
this.refreshDeviceStates(message)
}
}
//refresh Accessorie status
async refreshDeviceStates(message) {
const uuid = this.api.hap.uuid.generate(message.devId);
const deviceAccessorie = this.deviceAccessories.get(uuid);
if (deviceAccessorie) {
deviceAccessorie.updateState(message);
}
}
// Called from device classes
registerPlatformAccessory(platformAccessory) {
this.log.log(`Register Platform Accessory ${platformAccessory.displayName}`);
this.api.registerPlatformAccessories('homebridge-tuya-platform', 'TuyaPlatform', [platformAccessory]);
}
// Function invoked when homebridge tries to restore cached accessory.
// Developer can configure accessory at here (like setup event handler).
// Update current value.
configureAccessory(accessory) {
// this.log("Configuring cached accessory [%s]", accessory.displayName, accessory.context.deviceId, accessory.UUID);
// Set the accessory to reachable if plugin can currently process the accessory,
// otherwise set to false and update the reachability later by invoking
// accessory.updateReachability()
accessory.reachable = true;
accessory.on('identify', function (paired, callback) {
// this.log.debug('[IDENTIFY][%s]', accessory.displayName);
callback();
});
this.accessories.set(accessory.UUID, accessory);
}
// Sample function to show how developer can remove accessory dynamically from outside event
removeAccessory(accessory) {
if (accessory) {
this.log.log(`Remove Accessory ${accessory}`);
this.api.unregisterPlatformAccessories("homebridge-tuya-platform", "TuyaPlatform", [accessory]);
this.accessories.delete(accessory.uuid);
this.deviceAccessories.delete(accessory.uuid);
}
}
}