This repository has been archived by the owner on Jun 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
/
index.js
185 lines (155 loc) · 6.43 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
const SwitchAccessory = require('./lib/switch_accessory');
const OutletAccessory = require('./lib/outlet_accessory');
const DimmerAccessory = require('./lib/dimmer_accessory');
const LightAccessory = require('./lib/light_accessory');
const TuyaWebApi = require('./lib/tuyawebapi');
var Accessory, Service, Characteristic, UUIDGen;
module.exports = function (homebridge) {
// Accessory must be created from PlatformAccessory Constructor
Accessory = homebridge.platformAccessory;
// Service and Characteristic are from hap-nodejs
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
// For platform plugin to be considered as dynamic platform plugin,
// registerPlatform(pluginName, platformName, constructor, dynamic), dynamic must be true
homebridge.registerPlatform("homebridge-tuya-web", "TuyaWebPlatform", TuyaWebPlatform, true);
}
class TuyaWebPlatform {
constructor(log, config, api) {
this.log = log;
this.config = config;
this.pollingInterval = 10; // default 10 seconds
this.refreshInterval;
if (!config || !config.options) {
this.log('No config found, disabling plugin.')
return;
}
// Set cloud polling interval
this.pollingInterval = this.config.options.pollingInterval || 10;
// Create Tuya Web API instance
this.tuyaWebApi = new TuyaWebApi(
this.config.options.username,
this.config.options.password,
this.config.options.countryCode,
this.config.options.platform,
this.log
);
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("Initializing TuyaWebPlatform...");
// Get access token
this.tuyaWebApi.getOrRefreshToken().then((token) => {
this.tuyaWebApi.token = token;
// Start discovery for devices
this.tuyaWebApi.discoverDevices().then((devices) => {
// Add devices to Homebridge
for (const device of devices) {
this.addAccessory(device);
}
// Get device strate of all devices - once
this.refreshDeviceStates();
}).catch((error) => {
this.log.error(error);
});
// Set interval for refreshing device states
this.refreshInterval = setInterval(() => {
this.refreshDeviceStates();
}, this.pollingInterval * 1000);
}).catch((error) => {
this.log.error(error);
});
}.bind(this));
}
}
refreshDeviceStates() {
this.log.debug('Refreshing state for all devices...');
this.tuyaWebApi.getAllDeviceStates().then((devices) => {
// Refresh device states
for (const device of devices) {
const uuid = this.api.hap.uuid.generate(device.id);
const homebridgeAccessory = this.accessories.get(uuid);
if (homebridgeAccessory) {
homebridgeAccessory.controller.updateAccessory(device);
}
else {
this.log.error('Could not find accessory in dictionary');
}
}
}).catch((error) => {
this.log.error('Error retrieving devices states', error);
});
}
addAccessory(device) {
var deviceType = device.dev_type || 'switch';
this.log.info('Adding: %s (%s / %s)', device.name || 'unnamed', deviceType, device.id);
// Get UUID
const uuid = this.api.hap.uuid.generate(device.id);
const homebridgeAccessory = this.accessories.get(uuid);
// Is device type overruled in config defaults?
if (this.config.defaults) {
for (const def of this.config.defaults) {
if (def.id === device.id) {
deviceType = def.device_type || deviceType;
this.log('Device type is overruled in config to: ', deviceType);
}
}
}
// Construct new accessory
let deviceAccessory;
switch (deviceType) {
case 'light':
deviceAccessory = new LightAccessory(this, homebridgeAccessory, device);
this.accessories.set(uuid, deviceAccessory.homebridgeAccessory);
break;
case 'dimmer':
deviceAccessory = new DimmerAccessory(this, homebridgeAccessory, device);
this.accessories.set(uuid, deviceAccessory.homebridgeAccessory);
break;
case 'switch':
case 'outlet':
deviceAccessory = new OutletAccessory(this, homebridgeAccessory, device);
this.accessories.set(uuid, deviceAccessory.homebridgeAccessory);
break;
default:
this.log.warn('Could not init class for device type [%s]', deviceType);
break;
}
}
// Called from device classes
registerPlatformAccessory(platformAccessory) {
this.log.debug('Register Platform Accessory (%s)', platformAccessory.displayName);
this.api.registerPlatformAccessories('homebridge-tuya-web', 'TuyaWebPlatform', [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);
}
updateAccessoryReachability(accessory, state) {
this.log("Update Reachability [%s]", accessory.displayName, state);
accessory.updateReachability(state);
}
// Sample function to show how developer can remove accessory dynamically from outside event
removeAccessory(accessory) {
this.log("Remove Accessory [%s]", accessory.displayName);
this.api.unregisterPlatformAccessories("homebridge-tuya-web", "TuyaWebPlatform", [accessory]);
this.accessories.delete(accessory.uuid);
}
}