Skip to content

Commit 1f41119

Browse files
Koenkkbskjon
authored and
bskjon
committed
fix(ignore): Refactor Tuya fingerprint (#8648)
1 parent c863b0a commit 1f41119

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+613
-840
lines changed

scripts/modernExtendRefactor.ts

-67
This file was deleted.

scripts/refactor.ts

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// In the root of this repo, execute: `npx ts-node scripts/refactor.ts`
2+
3+
import {assert} from 'console';
4+
5+
import {ArrayLiteralExpression, Project, QuoteKind, SyntaxKind} from 'ts-morph';
6+
7+
const project = new Project({
8+
manipulationSettings: {
9+
quoteKind: QuoteKind.Single,
10+
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: false,
11+
},
12+
});
13+
project.addSourceFilesAtPaths('src/devices/*.ts');
14+
15+
//#region Refactor modernExtend temperature() to m.temperature()
16+
project.getSourceFiles().forEach((sourceFile) => {
17+
if (sourceFile.getBaseName() === 'index.ts') return;
18+
console.log(`Handling ${sourceFile.getBaseName()}`);
19+
20+
let save = false;
21+
22+
const definitions = sourceFile
23+
.getVariableStatementOrThrow('definitions')
24+
.getDeclarations()[0]
25+
.getInitializerOrThrow()
26+
.asKindOrThrow(SyntaxKind.ArrayLiteralExpression);
27+
28+
for (const definition of definitions.getElements()) {
29+
const childs = definition.getChildrenOfKind(SyntaxKind.PropertyAssignment);
30+
const fingerprint = childs.find((c) => c.getFirstChildByKind(SyntaxKind.Identifier)?.getText() === 'fingerprint');
31+
const model = childs.find((c) => c.getFirstChildByKind(SyntaxKind.Identifier)?.getText() === 'model');
32+
const fingerprintArray = fingerprint?.getFirstChildByKind(SyntaxKind.ArrayLiteralExpression);
33+
if (fingerprintArray) {
34+
const lookup: {[s: string]: Set<string>} = {};
35+
let match = true;
36+
for (const f of fingerprintArray.getElements()) {
37+
let modelID: string | undefined;
38+
let manufacturerName: string | undefined;
39+
for (const p of f.getChildrenOfKind(SyntaxKind.PropertyAssignment)) {
40+
if (p.getName() === 'modelID') {
41+
modelID = p.getInitializer()?.getText();
42+
} else if (p.getName() === 'manufacturerName') {
43+
manufacturerName = p.getInitializer()?.getText();
44+
} else {
45+
match = false;
46+
}
47+
}
48+
49+
if (modelID && manufacturerName && manufacturerName.includes('_T')) {
50+
if (!(modelID in lookup)) lookup[modelID] = new Set();
51+
lookup[modelID].add(manufacturerName);
52+
} else {
53+
match = false;
54+
console.log(`skip ${model?.getText()} (${modelID}, ${manufacturerName} ${manufacturerName?.includes('_T')})`);
55+
break;
56+
}
57+
}
58+
59+
if (match) {
60+
if (Object.keys(lookup).length == 1) {
61+
const key = Object.keys(lookup)[0];
62+
fingerprintArray.replaceWithText(`tuya.fingerprint(${key}, [${[...lookup[key]]}])`);
63+
} else {
64+
let txt: string[] = [];
65+
for (const [modelID, manufacturers] of Object.entries(lookup)) {
66+
txt.push(`...tuya.fingerprint(${modelID}, [${[...manufacturers]}])`);
67+
}
68+
fingerprintArray.replaceWithText(`[` + txt.join(', ') + `]`);
69+
}
70+
save = true;
71+
}
72+
}
73+
}
74+
75+
if (save) {
76+
const modernExtendImport = sourceFile.getImportDeclarations().filter((d) => d.getModuleSpecifierSourceFile()?.getBaseName() === 'tuya.ts');
77+
let match = false;
78+
for (const i of modernExtendImport) {
79+
match = true;
80+
i.remove();
81+
}
82+
83+
sourceFile.addImportDeclaration({
84+
moduleSpecifier: '../lib/tuya',
85+
namespaceImport: 'tuya',
86+
});
87+
sourceFile.saveSync();
88+
}
89+
});
90+
91+
//#region Refactor modernExtend temperature() to m.temperature()
92+
// project.getSourceFiles().forEach((sourceFile) => {
93+
// if (sourceFile.getBaseName() === 'index.ts') return;
94+
// console.log(`Handling ${sourceFile.getBaseName()}`);
95+
96+
// let save = false;
97+
98+
// const modernExtendImport = sourceFile
99+
// .getImportDeclarations()
100+
// .filter((d) => d.getModuleSpecifierSourceFile()?.getBaseName() === 'modernExtend.ts');
101+
102+
// for (const i of modernExtendImport) {
103+
// for (const n of i.getNamedImports()) {
104+
// const references = sourceFile.getDescendantsOfKind(SyntaxKind.Identifier).filter((identifier) => identifier.getText() === n.getText());
105+
// references.forEach((reference) => {
106+
// if (reference.getParent().getKind() == SyntaxKind.CallExpression) {
107+
// reference.replaceWithText(`m.${reference.getText()}`);
108+
// }
109+
// });
110+
// }
111+
// }
112+
113+
// save = true;
114+
115+
// if (save) {
116+
// const modernExtendImport = sourceFile
117+
// .getImportDeclarations()
118+
// .filter((d) => d.getModuleSpecifierSourceFile()?.getBaseName() === 'modernExtend.ts');
119+
// let match = false;
120+
// for (const i of modernExtendImport) {
121+
// match = true;
122+
// i.remove();
123+
// }
124+
125+
// if (match) {
126+
// sourceFile.addImportDeclaration({
127+
// moduleSpecifier: '../lib/modernExtend',
128+
// namespaceImport: 'm',
129+
// });
130+
// }
131+
132+
// sourceFile.saveSync();
133+
// }
134+
// });

src/devices/ajax_online.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const definitions: DefinitionWithExtend[] = [
3232
extend: [m.light({colorTemp: {range: [153, 500]}, color: true})],
3333
},
3434
{
35-
fingerprint: [{modelID: 'TS0505B', manufacturerName: '_TZ3210_hzy4rjz3'}],
35+
fingerprint: tuya.fingerprint('TS0505B', ['_TZ3210_hzy4rjz3']),
3636
model: 'AJ_RGBCCT_CTRL',
3737
vendor: 'Ajax Online',
3838
description: 'Smart Zigbee LED strip RGB+CCT',

src/devices/akuvox.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import fz from '../converters/fromZigbee';
22
import * as exposes from '../lib/exposes';
33
import * as reporting from '../lib/reporting';
4+
import * as tuya from '../lib/tuya';
45
import {DefinitionWithExtend} from '../lib/types';
56

67
const e = exposes.presets;
78

89
const definitions: DefinitionWithExtend[] = [
910
{
10-
fingerprint: [{modelID: 'TS0201', manufacturerName: '_TYZB01_ujfk3xd9'}],
11+
fingerprint: tuya.fingerprint('TS0201', ['_TYZB01_ujfk3xd9']),
1112
model: 'M423-9E',
1213
vendor: 'Akuvox',
1314
description: 'Smart temperature & humidity Sensor',

src/devices/aldi.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import fz from '../converters/fromZigbee';
22
import * as exposes from '../lib/exposes';
3+
import * as tuya from '../lib/tuya';
34
import {DefinitionWithExtend} from '../lib/types';
45

56
const e = exposes.presets;
67

78
const definitions: DefinitionWithExtend[] = [
89
{
9-
fingerprint: [{modelID: 'TS1001', manufacturerName: '_TZ3000_ztrfrcsu'}],
10+
fingerprint: tuya.fingerprint('TS1001', ['_TZ3000_ztrfrcsu']),
1011
model: '141L100RC',
1112
vendor: 'Aldi',
1213
description: 'MEGOS switch and dimming light remote control',

src/devices/alecto.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import fz from '../converters/fromZigbee';
22
import * as exposes from '../lib/exposes';
33
import * as legacy from '../lib/legacy';
4+
import * as tuya from '../lib/tuya';
45
import {DefinitionWithExtend} from '../lib/types';
56

67
const e = exposes.presets;
78
const ea = exposes.access;
89

910
const definitions: DefinitionWithExtend[] = [
1011
{
11-
fingerprint: [
12-
{modelID: 'daqwrsj\u0000', manufacturerName: '_TYST11_8daqwrsj'},
13-
{modelID: 'TS0601', manufacturerName: '_TZE200_8daqwrsj'},
14-
],
12+
fingerprint: [...tuya.fingerprint('daqwrsj\u0000', ['_TYST11_8daqwrsj']), ...tuya.fingerprint('TS0601', ['_TZE200_8daqwrsj'])],
1513
model: 'SMART-HEAT10',
1614
vendor: 'Alecto',
1715
description: 'Radiator valve with thermostat',
@@ -39,10 +37,7 @@ const definitions: DefinitionWithExtend[] = [
3937
],
4038
},
4139
{
42-
fingerprint: [
43-
{modelID: 'tbrwrfv\u0000', manufacturerName: '_TYST11_qtbrwrfv'},
44-
{modelID: 'TS0601', manufacturerName: '_TZE200_qtbrwrfv'},
45-
],
40+
fingerprint: [...tuya.fingerprint('tbrwrfv\u0000', ['_TYST11_qtbrwrfv']), ...tuya.fingerprint('TS0601', ['_TZE200_qtbrwrfv'])],
4641
model: 'SMART-SMOKE10',
4742
vendor: 'Alecto',
4843
description: 'Smoke detector',

src/devices/aubess.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as tuya from '../lib/tuya';
12
import {DefinitionWithExtend} from '../lib/types';
23
import * as zosung from '../lib/zosung';
34

@@ -7,7 +8,7 @@ const ez = zosung.presetsZosung;
78

89
const definitions: DefinitionWithExtend[] = [
910
{
10-
fingerprint: [{modelID: 'TS1201', manufacturerName: '_TZ3290_acv1iuslxi3shaaj'}],
11+
fingerprint: tuya.fingerprint('TS1201', ['_TZ3290_acv1iuslxi3shaaj']),
1112
model: 'ZXZIR-02',
1213
vendor: 'Aubess',
1314
description: 'Universal smart IR remote control',

src/devices/avatto.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const e = exposes.presets;
66

77
const definitions: DefinitionWithExtend[] = [
88
{
9-
fingerprint: [{modelID: 'TS0601', manufacturerName: '_TZE204_s139roas'}],
9+
fingerprint: tuya.fingerprint('TS0601', ['_TZE204_s139roas']),
1010
model: 'ZWSH16',
1111
vendor: 'AVATTO',
1212
description: 'Smart Temperature and Humidity Detector',

src/devices/blitzwolf.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import tz from '../converters/toZigbee';
22
import * as exposes from '../lib/exposes';
33
import * as legacy from '../lib/legacy';
44
import * as m from '../lib/modernExtend';
5+
import * as tuya from '../lib/tuya';
56
import {DefinitionWithExtend} from '../lib/types';
67

78
const e = exposes.presets;
@@ -17,15 +18,15 @@ const definitions: DefinitionWithExtend[] = [
1718
exposes: [e.occupancy()],
1819
},
1920
{
20-
fingerprint: [{modelID: 'TS0003', manufacturerName: '_TYZB01_aneiicmq'}],
21+
fingerprint: tuya.fingerprint('TS0003', ['_TYZB01_aneiicmq']),
2122
model: 'BW-SS7_1gang',
2223
vendor: 'BlitzWolf',
2324
description: 'Zigbee 3.0 smart light switch module 1 gang',
2425
extend: [m.onOff()],
2526
toZigbee: [tz.TYZB01_on_off],
2627
},
2728
{
28-
fingerprint: [{modelID: 'TS0003', manufacturerName: '_TYZB01_digziiav'}],
29+
fingerprint: tuya.fingerprint('TS0003', ['_TYZB01_digziiav']),
2930
model: 'BW-SS7_2gang',
3031
vendor: 'BlitzWolf',
3132
description: 'Zigbee 3.0 smart light switch module 2 gang',

src/devices/bseed.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import * as exposes from '../lib/exposes';
22
import * as legacy from '../lib/legacy';
3+
import * as tuya from '../lib/tuya';
34
import {DefinitionWithExtend} from '../lib/types';
45

56
const e = exposes.presets;
67
const ea = exposes.access;
78

89
const definitions: DefinitionWithExtend[] = [
910
{
10-
fingerprint: [
11-
{modelID: 'TS0601', manufacturerName: '_TZE200_yenbr4om'},
12-
{modelID: 'TS0601', manufacturerName: '_TZE204_bdblidq3'},
13-
{modelID: 'TS0601', manufacturerName: '_TZE200_bdblidq3'},
14-
],
11+
fingerprint: tuya.fingerprint('TS0601', ['_TZE200_yenbr4om', '_TZE204_bdblidq3', '_TZE200_bdblidq3']),
1512
model: 'BSEED_TS0601_cover',
1613
vendor: 'BSEED',
1714
description: 'Zigbee curtain switch',

src/devices/cleverio.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import fz from '../converters/fromZigbee';
22
import tz from '../converters/toZigbee';
33
import * as exposes from '../lib/exposes';
44
import * as reporting from '../lib/reporting';
5+
import * as tuya from '../lib/tuya';
56
import {DefinitionWithExtend} from '../lib/types';
67

78
const e = exposes.presets;
89
const ea = exposes.access;
910

1011
const definitions: DefinitionWithExtend[] = [
1112
{
12-
fingerprint: [{modelID: 'TS0219', manufacturerName: '_TZ3000_vdfwjopk'}],
13+
fingerprint: tuya.fingerprint('TS0219', ['_TZ3000_vdfwjopk']),
1314
model: 'SA100',
1415
vendor: 'Cleverio',
1516
description: 'Smart siren',
@@ -28,7 +29,7 @@ const definitions: DefinitionWithExtend[] = [
2829
},
2930
},
3031
{
31-
fingerprint: [{modelID: 'SM0201', manufacturerName: '_TYZB01_lzrhtcxu'}],
32+
fingerprint: tuya.fingerprint('SM0201', ['_TYZB01_lzrhtcxu']),
3233
model: 'SS300',
3334
vendor: 'Cleverio',
3435
description: 'Temperature/humdity sensor',

src/devices/ecodim.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ const definitions: DefinitionWithExtend[] = [
213213
meta: {multiEndpoint: true},
214214
},
215215
{
216-
fingerprint: [{modelID: 'TS0501B', manufacturerName: '_TZ3210_yluvwhjc'}],
216+
fingerprint: tuya.fingerprint('TS0501B', ['_TZ3210_yluvwhjc']),
217217
model: 'ED-10042',
218218
vendor: 'EcoDim',
219219
description: 'Zigbee LED filament light dimmable E27, globe G125, flame 2200K',

src/devices/essentials.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const localValueConverters = {
6767

6868
const definitions: DefinitionWithExtend[] = [
6969
{
70-
fingerprint: [{modelID: 'TS0601', manufacturerName: '_TZE200_i48qyn9s'}],
70+
fingerprint: tuya.fingerprint('TS0601', ['_TZE200_i48qyn9s']),
7171
model: '_TZE200_i48qyn9s',
7272
vendor: 'Essentials',
7373
description: 'Thermostat Zigbee smart home',

src/devices/evanell.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ const ea = exposes.access;
99

1010
const definitions: DefinitionWithExtend[] = [
1111
{
12-
fingerprint: [
13-
{modelID: 'TS0601', manufacturerName: '_TZE200_dmfguuli'},
14-
{modelID: 'TS0601', manufacturerName: '_TZE200_rxypyjkw'},
15-
],
12+
fingerprint: tuya.fingerprint('TS0601', ['_TZE200_dmfguuli', '_TZE200_rxypyjkw']),
1613
model: 'EZ200',
1714
vendor: 'Evanell',
1815
description: 'Thermostatic radiator valve',

0 commit comments

Comments
 (0)