-
Notifications
You must be signed in to change notification settings - Fork 12
/
app.ts
288 lines (246 loc) · 9.6 KB
/
app.ts
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
import { OAuth2App } from 'homey-oauth2app';
import NodeCache from 'node-cache';
import TuyaOAuth2Client from './lib/TuyaOAuth2Client';
import * as TuyaOAuth2Util from './lib/TuyaOAuth2Util';
import TuyaOAuth2Device from './lib/TuyaOAuth2Device';
import sourceMapSupport from 'source-map-support';
import type {
TuyaDeviceDataPoint,
TuyaDeviceDataPointResponse,
TuyaScene,
TuyaStatusResponse,
} from './types/TuyaApiTypes';
import { type ArgumentAutocompleteResults } from 'homey/lib/FlowCard';
sourceMapSupport.install();
const STATUS_CACHE_KEY = 'status';
const DATAPOINT_CACHE_KEY = 'datapoint';
const SCENE_CACHE_KEY = 'scenes';
const CACHE_TTL = 30;
type DeviceArgs = { device: TuyaOAuth2Device };
type StatusCodeArgs = { code: AutoCompleteArg };
type StatusCodeState = { code: string };
type HomeyTuyaScene = Pick<TuyaScene, 'id' | 'name'>;
type AutoCompleteArg = {
name: string;
id: string;
title: string;
dataPoint: boolean;
};
module.exports = class TuyaOAuth2App extends OAuth2App {
static OAUTH2_CLIENT = TuyaOAuth2Client;
static OAUTH2_DEBUG = process.env.DEBUG === '1';
static OAUTH2_MULTI_SESSION = false; // TODO: Enable this feature & make nice pairing UI
private apiCache: NodeCache = new NodeCache({ stdTTL: CACHE_TTL });
async onOAuth2Init(): Promise<void> {
await super.onOAuth2Init();
const sendCommandRunListener = async ({
device,
code,
value,
}: {
device: TuyaOAuth2Device;
code: AutoCompleteArg;
value: unknown;
}): Promise<void> => {
if (code.dataPoint) {
await device.setDataPoint(code.id, value);
} else {
await device.sendCommand({ code: code.id, value });
}
};
const autocompleteListener = async (
query: string | undefined,
args: DeviceArgs,
filter: ({ value }: { value: unknown }) => boolean,
): Promise<ArgumentAutocompleteResults> => {
function convert(
values: TuyaStatusResponse | Array<TuyaDeviceDataPoint>,
dataPoints: boolean,
): ArgumentAutocompleteResults {
values = values.filter(filter);
const trimmedQuery = (query ?? '').trim();
if (trimmedQuery) {
values = values.filter(({ code }: { code: string }) =>
code.toLowerCase().includes(trimmedQuery.toLowerCase()),
);
}
return values.map(value => ({
name: value.code,
id: value.code,
title: value.code,
dataPoint: dataPoints,
}));
}
const deviceId = args.device.getData().deviceId;
const statusCacheKey = `${STATUS_CACHE_KEY}_${deviceId}`;
const datapointCacheKey = `${DATAPOINT_CACHE_KEY}_${deviceId}`;
if (!this.apiCache.has(statusCacheKey)) {
this.apiCache.set<TuyaStatusResponse | null>(
statusCacheKey,
await args.device.getStatus().catch(e => {
this.error(e);
return null;
}),
);
}
const status = this.apiCache.get<TuyaStatusResponse | null>(statusCacheKey);
const statusOptions = status ? convert(status, false) : [];
if (!this.apiCache.has(datapointCacheKey)) {
this.apiCache.set<TuyaDeviceDataPointResponse | null>(
datapointCacheKey,
await args.device.queryDataPoints().catch(e => {
this.error(e);
return null;
}),
);
}
const dataPoints = this.apiCache.get<TuyaDeviceDataPointResponse | null>(datapointCacheKey);
const dataPointOptions = dataPoints ? convert(dataPoints.properties, true) : [];
// Remove duplicates, preferring status options
const combinedMap: Record<string, ArgumentAutocompleteResults[number]> = {};
for (const dataPointOption of dataPointOptions) {
combinedMap[dataPointOption.name] = dataPointOption;
}
for (const statusOption of statusOptions) {
combinedMap[statusOption.name] = statusOption;
}
const possibleValues = Object.values(combinedMap);
if (possibleValues.length === 0) {
throw new Error(this.homey.__('error_retrieving_codes'));
}
return possibleValues;
};
// Register Tuya Web API Flow Cards
// Sending
this.homey.flow
.getActionCard('send_command_string')
.registerRunListener(sendCommandRunListener)
.registerArgumentAutocompleteListener('code', async (query: string, args: DeviceArgs) =>
autocompleteListener(
query,
args,
({ value }) => typeof value === 'string' && !TuyaOAuth2Util.hasJsonStructure(value),
),
);
this.homey.flow
.getActionCard('send_command_number')
.registerRunListener(sendCommandRunListener)
.registerArgumentAutocompleteListener('code', async (query: string, args: DeviceArgs) =>
autocompleteListener(query, args, ({ value }) => typeof value === 'number'),
);
this.homey.flow
.getActionCard('send_command_boolean')
.registerRunListener(sendCommandRunListener)
.registerArgumentAutocompleteListener('code', async (query: string, args: DeviceArgs) =>
autocompleteListener(query, args, ({ value }) => typeof value === 'boolean'),
);
this.homey.flow
.getActionCard('send_command_json')
.registerRunListener(
async ({ device, code, value }: { device: TuyaOAuth2Device; code: string | { id: string }; value: string }) => {
if (typeof code === 'object') code = code.id;
await device.sendCommand({
code,
value: JSON.parse(value),
});
},
)
.registerArgumentAutocompleteListener('code', async (query: string, args: DeviceArgs) =>
autocompleteListener(
query,
args,
({ value }) => typeof value === 'object' || TuyaOAuth2Util.hasJsonStructure(value),
),
);
// Receiving
this.homey.flow
.getDeviceTriggerCard('receive_status_boolean')
.registerRunListener((args: StatusCodeArgs, state: StatusCodeState) => args.code.id === state.code)
.registerArgumentAutocompleteListener('code', async (query: string, args: DeviceArgs) =>
autocompleteListener(query, args, ({ value }) => typeof value === 'boolean'),
);
this.homey.flow
.getDeviceTriggerCard('receive_status_json')
.registerRunListener((args: StatusCodeArgs, state: StatusCodeState) => args.code.id === state.code)
.registerArgumentAutocompleteListener('code', async (query: string, args: DeviceArgs) =>
autocompleteListener(
query,
args,
({ value }) => typeof value === 'object' || TuyaOAuth2Util.hasJsonStructure(value),
),
);
this.homey.flow
.getDeviceTriggerCard('receive_status_number')
.registerRunListener((args: StatusCodeArgs, state: StatusCodeState) => args.code.id === state.code)
.registerArgumentAutocompleteListener('code', async (query: string, args: DeviceArgs) =>
autocompleteListener(query, args, ({ value }) => typeof value === 'number'),
);
this.homey.flow
.getDeviceTriggerCard('receive_status_string')
.registerRunListener((args: StatusCodeArgs, state: StatusCodeState) => args.code.id === state.code)
.registerArgumentAutocompleteListener('code', async (query: string, args: DeviceArgs) =>
autocompleteListener(
query,
args,
({ value }) => typeof value === 'string' && !TuyaOAuth2Util.hasJsonStructure(value),
),
);
// Tuya scenes
this.homey.flow
.getActionCard('trigger_scene')
.registerRunListener(async (args: { scene: HomeyTuyaScene }) => {
const { scene } = args;
const client = this.getFirstSavedOAuth2Client();
await client.triggerScene(scene.id);
})
.registerArgumentAutocompleteListener('scene', async (query?: string) => {
if (!this.apiCache.has(SCENE_CACHE_KEY)) {
this.log('Retrieving available scenes');
const client = this.getFirstSavedOAuth2Client();
// Gets all homes for this user
const homes = await client.getHomes().catch(err => {
this.error(err);
throw new Error(this.homey.__('error_retrieving_scenes'));
});
// Get all scenes for this user's homes
const scenes: Array<HomeyTuyaScene> = [];
for (const home of homes) {
await client
.getScenes(home.home_id)
.then(homeScenes =>
scenes.push(
...homeScenes.list.map(scene => ({
name: scene.name,
id: scene.id,
})),
),
)
.catch(err => {
if (err.tuyaCode === 40001900) {
// Access to particular home denied, skip it
this.log('Scene home denied access', home.home_id);
return;
}
this.error(err);
throw new Error(this.homey.__('error_retrieving_scenes'));
});
}
this.apiCache.set(SCENE_CACHE_KEY, scenes);
}
const scenes = this.apiCache.get<HomeyTuyaScene[]>(SCENE_CACHE_KEY) ?? [];
const trimmedQuery = (query ?? '').trim();
if (!trimmedQuery) {
return scenes;
}
return scenes.filter(scene => scene.name.toLowerCase().includes(trimmedQuery.toLowerCase()));
});
this.log('Tuya started');
}
getFirstSavedOAuth2Client(): TuyaOAuth2Client {
const client = super.getFirstSavedOAuth2Client();
if (!client) {
throw new Error(this.homey.__('connection_failed'));
}
return client as TuyaOAuth2Client;
}
};