-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapi.js
More file actions
459 lines (407 loc) · 13.6 KB
/
Copy pathapi.js
File metadata and controls
459 lines (407 loc) · 13.6 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
'use strict';
const GoveeCloudClient = require('./api/govee-api-v2');
const { parseByModel, isKnownSensorModel } = require('./lib/ble-parser');
// Extract the Govee model code (e.g. "H5075", "H612F", "H614B") from a BLE
// local name like "Govee_H5075_54F6". Govee's model IDs are H + 4
// alphanumeric characters — not always all digits — so we accept [0-9A-Z]
// after the H, not just \d. Returns null when the name doesn't match.
function extractGoveeModel(localName) {
if (!localName) return null;
const match = /^Govee_(H[0-9A-Z]{4})/i.exec(localName);
return match ? match[1].toUpperCase() : null;
}
module.exports = {
/**
* Get the local API client status
* Called from settings page to display UDP client status
*/
async getLocalApiStatus({ homey }) {
const client = homey.app.localApiClient;
if (!client) {
return {
initialized: false,
ready: false,
error: 'Local API client not initialized',
devices: [],
deviceCount: 0
};
}
const initError = client.getInitError();
const isReady = client.isClientReady();
const devices = client.localDevices || [];
// Map devices to a safe format for the settings page
const deviceList = devices.map(device => ({
id: device.deviceID,
model: device.model,
ip: device.ip,
isOn: device.state?.isOn === 1,
brightness: device.state?.brightness || 0,
hasReceivedUpdates: device.state?.hasReceivedUpdates || false
}));
return {
initialized: true,
ready: isReady,
error: initError ? initError.message : null,
devices: deviceList,
deviceCount: deviceList.length,
udpPort: 4002,
multicastAddress: '239.255.255.250',
discoveryInterval: 30000
};
},
/**
* Trigger a manual discovery scan
*/
async triggerDiscovery({ homey }) {
const client = homey.app.localApiClient;
if (!client) {
return { success: false, error: 'Local API client not initialized' };
}
if (!client.isClientReady()) {
return { success: false, error: 'Local API client not ready - ' + (client.getInitError()?.message || 'still initializing') };
}
client.triggerDiscovery();
// Wait a moment and return updated device count
await new Promise(resolve => setTimeout(resolve, 3000));
return {
success: true,
deviceCount: client.localDevices.length,
message: `Discovery triggered. Found ${client.localDevices.length} device(s).`
};
},
/**
* Reinitialize the local API client
* Useful when the UDP socket needs to be recreated after an error
*/
async reinitializeLocalApi({ homey }) {
const client = homey.app.localApiClient;
if (!client) {
// Try to create a new client
const gv = require('./api/govee-localapi');
try {
homey.app.localApiClient = new gv.GoveeClient();
// Wait for initialization
await new Promise(resolve => setTimeout(resolve, 5000));
const newClient = homey.app.localApiClient;
if (newClient.isClientReady()) {
return { success: true, message: 'Local API client initialized successfully' };
} else {
return {
success: false,
error: newClient.getInitError()?.message || 'Initialization timeout'
};
}
} catch (err) {
return { success: false, error: err.message };
}
}
// Reinitialize existing client
const success = await client.reinitialize();
if (success) {
// Trigger discovery after successful reinit
client.triggerDiscovery();
await new Promise(resolve => setTimeout(resolve, 3000));
return {
success: true,
message: `Reinitialized successfully. Found ${client.localDevices.length} device(s).`,
deviceCount: client.localDevices.length
};
} else {
return {
success: false,
error: client.getInitError()?.message || 'Reinitialization failed'
};
}
},
/**
* Get detailed diagnostics for the local API client
*/
async getLocalApiDiagnostics({ homey }) {
const client = homey.app.localApiClient;
if (!client) {
return {
initialized: false,
diagnostics: null
};
}
return {
initialized: true,
diagnostics: client.getDiagnostics()
};
},
/**
* Test Cloud API connection and retrieve device list
* Returns full API response for debugging/support purposes
*/
async testCloudApi({ homey }) {
const apiKey = homey.settings.get('api_key');
if (!apiKey) {
return {
success: false,
error: 'No API key configured. Please enter your Govee API key in the Cloud API tab.',
rawResponse: null,
devices: [],
deviceCount: 0,
timestamp: new Date().toISOString()
};
}
try {
const client = new GoveeCloudClient.GoveeClient({ api_key: apiKey });
const response = await client.deviceList();
// Extract device list from response
const devices = response.data || [];
// Map devices to a simplified format for display
const deviceList = devices.map(device => ({
id: device.device,
model: device.sku,
name: device.deviceName || device.device,
type: device.type || 'Unknown'
}));
return {
success: true,
error: null,
rawResponse: JSON.stringify(response, null, 2),
devices: deviceList,
deviceCount: deviceList.length,
timestamp: new Date().toISOString()
};
} catch (err) {
return {
success: false,
error: err.message || 'Unknown error occurred',
rawResponse: JSON.stringify({ error: err.message, stack: err.stack }, null, 2),
devices: [],
deviceCount: 0,
timestamp: new Date().toISOString()
};
}
},
/**
* Test Cloud API ping endpoint
*/
async pingCloudApi({ homey }) {
const apiKey = homey.settings.get('api_key');
if (!apiKey) {
return {
success: false,
error: 'No API key configured',
timestamp: new Date().toISOString()
};
}
try {
const client = new GoveeCloudClient.GoveeClient({ api_key: apiKey });
const response = await client.ping();
return {
success: true,
error: null,
rawResponse: JSON.stringify(response, null, 2),
timestamp: new Date().toISOString()
};
} catch (err) {
return {
success: false,
error: err.message || 'Unknown error occurred',
rawResponse: JSON.stringify({ error: err.message }, null, 2),
timestamp: new Date().toISOString()
};
}
},
/**
* Poll the raw cloud state for a single device.
* Useful for support diagnostics: reporters can hit one button per device
* in the settings page and share the exact response Govee returned,
* instead of hoping the right poll ends up in a log window.
*/
async testDeviceState({ homey, query }) {
const apiKey = homey.settings.get('api_key');
if (!apiKey) {
return {
success: false,
error: 'No API key configured. Add your Govee API key in the Cloud API tab first.',
rawResponse: null,
timestamp: new Date().toISOString()
};
}
const sku = query && query.sku;
const device = query && query.device;
if (!sku || !device) {
return {
success: false,
error: 'sku and device query parameters are required',
rawResponse: null,
timestamp: new Date().toISOString()
};
}
try {
const client = new GoveeCloudClient.GoveeClient({ api_key: apiKey });
const payload = await client.state(sku, device);
return {
success: true,
error: null,
sku,
device,
payload,
rawResponse: JSON.stringify(payload, null, 2),
timestamp: new Date().toISOString()
};
} catch (err) {
return {
success: false,
error: err.message || 'Unknown error occurred',
sku,
device,
rawResponse: JSON.stringify({ error: err.message, stack: err.stack }, null, 2),
timestamp: new Date().toISOString()
};
}
},
/**
* Scan for Govee BLE sensors in range and dump their raw advertisement
* bytes + the current parser output. Used from the settings page so
* reporters can share exact hex bytes with support in one click,
* instead of trying to time an app log capture around a 30-second poll
* interval. Handles all sensor models supported by lib/ble-parser.js.
*/
async scanBleSensors({ homey }) {
// Wrap the entire body so any unexpected throw still returns a well-formed
// {success, error} object instead of letting Homey's wrapper produce an
// opaque "Cannot read properties of undefined" style error to the client.
try {
if (!homey || !homey.ble || typeof homey.ble.discover !== 'function') {
return {
success: false,
error: 'BLE manager not available on this Homey (homey.ble is undefined). Check that the app has the homey:wireless:ble permission and is running on a Homey with a BLE radio.',
timestamp: new Date().toISOString(),
sensors: [],
};
}
let advertisements;
try {
advertisements = await homey.ble.discover();
} catch (err) {
return {
success: false,
error: 'homey.ble.discover() threw: ' + (err && err.message ? err.message : String(err)),
timestamp: new Date().toISOString(),
sensors: [],
};
}
if (!Array.isArray(advertisements)) advertisements = [];
const govee = advertisements.filter(adv => {
if (!adv) return false;
const name = (adv && adv.localName) || '';
return typeof name === 'string' && name.indexOf('Govee_') === 0;
});
const sensors = govee.map(adv => {
try {
const model = extractGoveeModel(adv.localName);
// Collect every candidate buffer the driver would try to parse:
// one entry per serviceData UUID plus the manufacturerData blob.
const buffers = [];
if (adv.serviceData && typeof adv.serviceData === 'object') {
for (const [uuid, value] of Object.entries(adv.serviceData)) {
if (Buffer.isBuffer(value)) {
buffers.push({
source: 'serviceData:' + uuid,
hex: value.toString('hex'),
length: value.length,
});
}
}
}
if (adv.manufacturerData && Buffer.isBuffer(adv.manufacturerData)) {
buffers.push({
source: 'manufacturerData',
hex: adv.manufacturerData.toString('hex'),
length: adv.manufacturerData.length,
});
}
// Try each buffer through parseByModel. Govee ships new sensor
// models continuously and our hardcoded list will always lag, so
// we always attempt to parse — but we flag isKnownSensor in the
// response so the UI can label unknown-model results as
// "speculative" (parseStandardFormat's fallback will produce
// plausible-looking temp/humid values from arbitrary bytes).
let parsed = null;
let parsedFrom = null;
let parseError = null;
const isSensor = isKnownSensorModel(model);
if (model) {
for (const b of buffers) {
try {
const r = parseByModel(model, Buffer.from(b.hex, 'hex'));
if (r) {
parsed = r;
parsedFrom = b.source;
break;
}
} catch (err) {
parseError = err && err.message ? err.message : String(err);
}
}
}
return {
address: adv.address || null,
localName: adv.localName || null,
model,
isKnownSensor: isSensor,
rssi: typeof adv.rssi === 'number' ? adv.rssi : null,
buffers,
parsed,
parsedFrom,
parseError,
};
} catch (err) {
return {
address: (adv && adv.address) || null,
localName: (adv && adv.localName) || null,
model: null,
buffers: [],
parsed: null,
parseError: 'per-advertisement processing failed: ' + (err && err.message ? err.message : String(err)),
};
}
});
return {
success: true,
timestamp: new Date().toISOString(),
totalAdvertisements: advertisements.length,
count: sensors.length,
sensors,
};
} catch (err) {
return {
success: false,
error: 'scanBleSensors crashed: ' + (err && err.message ? err.message : String(err)),
timestamp: new Date().toISOString(),
sensors: [],
};
}
},
/**
* Check if a device IP would be reachable from Homey's network interfaces
* Useful for debugging why local discovery might not find certain devices
*/
async checkDeviceReachability({ homey, query }) {
const client = homey.app.localApiClient;
if (!client) {
return {
success: false,
error: 'Local API client not initialized'
};
}
const deviceIp = query?.ip;
if (!deviceIp) {
// Return general network info
const diagnostics = client.getDiagnostics();
return {
success: true,
network: diagnostics.network
};
}
return {
success: true,
reachability: client.checkDeviceReachability(deviceIp)
};
}
};