-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapi.js
More file actions
261 lines (225 loc) · 6.79 KB
/
api.js
File metadata and controls
261 lines (225 loc) · 6.79 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
'use strict';
const GoveeCloudClient = require('./api/govee-api-v2');
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()
};
}
},
/**
* 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)
};
}
};