Skip to content

Commit

Permalink
fix(ignore): Prepare for large scale refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Nov 30, 2023
1 parent 66bfa64 commit 87d6359
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 45 deletions.
74 changes: 74 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"jest": "^29.7.0",
"rimraf": "^5.0.5",
"ts-jest": "^29.1.1",
"ts-morph": "^20.0.0",
"typescript": "^5.3.2"
}
}
58 changes: 58 additions & 0 deletions scripts/modernExtendRefactor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {Project, QuoteKind, SyntaxKind} from 'ts-morph';

const project = new Project({
manipulationSettings: {
quoteKind: QuoteKind.Single,
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: false,
},
});
project.addSourceFilesAtPaths('src/devices/*.ts');

let totalDefinitions = 0;
let totalDefinitionsWithModernExtend = 0;

project.getSourceFiles().forEach((sourceFile) => {
if (sourceFile.getBaseName() === 'index.ts') return;

let changed = false;
const definitions = sourceFile.getVariableStatementOrThrow('definitions').getDescendantsOfKind(SyntaxKind.ObjectLiteralExpression);
totalDefinitions += definitions.length;
const type = 'light';

for (const definition of definitions) {
const childs = definition.getChildrenOfKind(SyntaxKind.PropertyAssignment);
const model = childs.find((c) => c.getFirstChildByKind(SyntaxKind.Identifier)?.getText() === 'model');
const extend = childs.find((c) => c.getFirstChildByKind(SyntaxKind.Identifier)?.getText() === 'extend');
const configure = childs.find((c) => c.getFirstChildByKind(SyntaxKind.Identifier)?.getText() === 'configure');
const fromZigbee = childs.find((c) => c.getFirstChildByKind(SyntaxKind.Identifier)?.getText() === 'fromZigbee');
const toZigbee = childs.find((c) => c.getFirstChildByKind(SyntaxKind.Identifier)?.getText() === 'toZigbee');
const meta = childs.find((c) => c.getFirstChildByKind(SyntaxKind.Identifier)?.getText() === 'meta');

if (extend?.getFullText().includes('extend: extend.light_onoff_brightness()') && !meta && !fromZigbee && !toZigbee && !configure) {
extend.replaceWithText(`extend: [${type}()]`);
console.log(`Updated ${model?.getFullText().trim()}`);
changed = true;
totalDefinitionsWithModernExtend += 1;
} else if (extend?.getFirstChildByKind(SyntaxKind.ArrayLiteralExpression)) {
totalDefinitionsWithModernExtend += 1;
}
}

if (changed) {
const modernExtendImport = sourceFile.getImportDeclarations()
.find((d) => d.getModuleSpecifierSourceFile()?.getBaseName() === 'modernExtend.ts');
if (!modernExtendImport) {
sourceFile.addImportDeclaration({moduleSpecifier: '../lib/modernExtend', namedImports: [type]});
} else {
if (!modernExtendImport.getNamedImports().find((i) => i.getName() === type)) {
modernExtendImport.addNamedImport(type);
}
}

sourceFile.saveSync();
}
});

console.log(
`${totalDefinitionsWithModernExtend} out of ${totalDefinitions} use modern extend (${totalDefinitionsWithModernExtend / totalDefinitions}%)`,
);
18 changes: 9 additions & 9 deletions src/devices/sonoff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import tz from '../converters/toZigbee';
import * as constants from '../lib/constants';
import * as reporting from '../lib/reporting';
import extend from '../lib/extend';
import * as modernExtend from '../lib/modernExtend';
import {binary, numeric} from '../lib/modernExtend';
import {Definition, Fz, KeyValue} from '../lib/types';

const e = exposes.presets;
Expand Down Expand Up @@ -387,23 +387,23 @@ const definitions: Definition[] = [
tz.thermostat_running_state,
],
extend: [
modernExtend.binary({
binary({
name: 'child_lock',
cluster: 0xFC11,
attribute: {id: 0x0000, type: 0x10},
description: 'Enables/disables physical input on the device',
valueOn: ['LOCK', 0x01],
valueOff: ['UNLOCK', 0x00],
}),
modernExtend.binary({
binary({
name: 'open_window',
cluster: 0xFC11,
attribute: {id: 0x6000, type: 0x10},
description: 'Automatically turns off the radiator when local temperature drops by more than 1.5°C in 4.5 minutes.',
valueOn: ['ON', 0x01],
valueOff: ['OFF', 0x00],
}),
modernExtend.numeric({
numeric({
name: 'frost_protection_temperature',
cluster: 0xFC11,
attribute: {id: 0x6002, type: 0x29},
Expand All @@ -415,37 +415,37 @@ const definitions: Definition[] = [
unit: '°C',
scale: 100,
}),
modernExtend.numeric({
numeric({
name: 'idle_steps',
cluster: 0xFC11,
attribute: {id: 0x6003, type: 0x21},
description: 'Number of steps used for calibration (no-load steps)',
readOnly: true,
}),
modernExtend.numeric({
numeric({
name: 'closing_steps',
cluster: 0xFC11,
attribute: {id: 0x6004, type: 0x21},
description: 'Number of steps it takes to close the valve',
readOnly: true,
}),
modernExtend.numeric({
numeric({
name: 'valve_opening_limit_voltage',
cluster: 0xFC11,
attribute: {id: 0x6005, type: 0x21},
description: 'Valve opening limit voltage',
unit: 'mV',
readOnly: true,
}),
modernExtend.numeric({
numeric({
name: 'valve_closing_limit_voltage',
cluster: 0xFC11,
attribute: {id: 0x6006, type: 0x21},
description: 'Valve closing limit voltage',
unit: 'mV',
readOnly: true,
}),
modernExtend.numeric({
numeric({
name: 'valve_motor_running_voltage',
cluster: 0xFC11,
attribute: {id: 0x6007, type: 0x21},
Expand Down
6 changes: 3 additions & 3 deletions src/devices/sowilo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Definition} from '../lib/types';
import extend from '../lib/extend';

const definition: Definition[] = [
const definitions: Definition[] = [
{
zigbeeModel: ['L258'],
model: 'L258',
Expand All @@ -12,5 +12,5 @@ const definition: Definition[] = [
},
];

export default definition;
module.exports = definition;
export default definitions;
module.exports = definitions;

0 comments on commit 87d6359

Please sign in to comment.