Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Remove legacy extend support #7200

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 42 additions & 70 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,86 +93,58 @@ function validateDefinition(definition: Definition) {

function processExtensions(definition: Definition): Definition {
if ('extend' in definition) {
if (Array.isArray(definition.extend)) {
// Modern extend, merges properties, e.g. when both extend and definition has toZigbee, toZigbee will be combined
let {extend, toZigbee, fromZigbee, exposes, meta, endpoint, configure: definitionConfigure, onEvent, ota, ...definitionWithoutExtend} = definition;
if (typeof exposes === 'function') {
assert.fail(`'${definition.model}' has function exposes which is not allowed`);
}
if (!Array.isArray(definition.extend)) {
assert.fail(`'${definition.model}' has legacy extend which is not supported anymore`);
}
// Modern extend, merges properties, e.g. when both extend and definition has toZigbee, toZigbee will be combined
let {extend, toZigbee, fromZigbee, exposes, meta, endpoint, configure: definitionConfigure, onEvent, ota, ...definitionWithoutExtend} = definition;
if (typeof exposes === 'function') {
assert.fail(`'${definition.model}' has function exposes which is not allowed`);
}

toZigbee = [...toZigbee ?? []];
fromZigbee = [...fromZigbee ?? []];
exposes = [...exposes ?? []];
const configures: Configure[] = definitionConfigure ? [definitionConfigure] : [];
toZigbee = [...toZigbee ?? []];
fromZigbee = [...fromZigbee ?? []];
exposes = [...exposes ?? []];
const configures: Configure[] = definitionConfigure ? [definitionConfigure] : [];

for (const ext of extend) {
if (!ext.isModernExtend) {
assert.fail(`'${definition.model}' has legacy extend in modern extend`);
}
if (ext.toZigbee) toZigbee.push(...ext.toZigbee);
if (ext.fromZigbee) fromZigbee.push(...ext.fromZigbee);
if (ext.exposes) exposes.push(...ext.exposes);
if (ext.meta) meta = {...ext.meta, ...meta};
if (ext.configure) configures.push(ext.configure);
if (ext.ota) {
if (ota) {
assert.fail(`'${definition.model}' has multiple 'ota', this is not allowed`);
}
ota = ext.ota;
}
if (ext.endpoint) {
if (endpoint) {
assert.fail(`'${definition.model}' has multiple 'endpoint', this is not allowed`);
}
endpoint = ext.endpoint;
for (const ext of extend) {
if (!ext.isModernExtend) {
assert.fail(`'${definition.model}' has legacy extend in modern extend`);
}
if (ext.toZigbee) toZigbee.push(...ext.toZigbee);
if (ext.fromZigbee) fromZigbee.push(...ext.fromZigbee);
if (ext.exposes) exposes.push(...ext.exposes);
if (ext.meta) meta = {...ext.meta, ...meta};
if (ext.configure) configures.push(ext.configure);
if (ext.ota) {
if (ota) {
assert.fail(`'${definition.model}' has multiple 'ota', this is not allowed`);
}
if (ext.onEvent) {
if (onEvent) {
assert.fail(`'${definition.model}' has multiple 'onEvent', this is not allowed`);
}
onEvent = ext.onEvent;
ota = ext.ota;
}
if (ext.endpoint) {
if (endpoint) {
assert.fail(`'${definition.model}' has multiple 'endpoint', this is not allowed`);
}
endpoint = ext.endpoint;
}

let configure: Configure = null;
if (configures.length !== 0) {
configure = async (device, coordinatorEndpoint, logger) => {
for (const func of configures) {
await func(device, coordinatorEndpoint, logger);
}
if (ext.onEvent) {
if (onEvent) {
assert.fail(`'${definition.model}' has multiple 'onEvent', this is not allowed`);
}
onEvent = ext.onEvent;
}
definition = {toZigbee, fromZigbee, exposes, meta, configure, endpoint, onEvent, ota, ...definitionWithoutExtend};
} else {
// Legacy extend, overrides properties, e.g. when both extend and definition has toZigbee, definition toZigbee will be used
const {extend, ...definitionWithoutExtend} = definition;
}

if (extend.isModernExtend) {
assert.fail(`'${definition.model}' has modern extend in legacy extend`);
}
if (extend.configure && definition.configure) {
assert.fail(`'${definition.model}' has configure in extend and definition, this is not allowed`);
}
if (extend.ota && definition.ota) {
assert.fail(`'${definition.model}' has OTA in extend and definition, this is not allowed`);
}
if (extend.onEvent && definition.onEvent) {
assert.fail(`'${definition.model}' has onEvent in extend and definition, this is not allowed`);
}
if (typeof definition.exposes === 'function') {
assert.fail(`'${definition.model}' has function exposes which is not allowed`);
let configure: Configure = null;
if (configures.length !== 0) {
configure = async (device, coordinatorEndpoint, logger) => {
for (const func of configures) {
await func(device, coordinatorEndpoint, logger);
}
}

const toZigbee = [...definition.toZigbee ?? [], ...extend.toZigbee];
const fromZigbee = [...definition.fromZigbee ?? [], ...extend.fromZigbee];
const exposes = [...definition.exposes ?? [], ...extend.exposes];
const meta = extend.meta || definitionWithoutExtend.meta ? {
...extend.meta,
...definitionWithoutExtend.meta,
} : undefined;

definition = {...extend, toZigbee, fromZigbee, exposes, meta, ...definitionWithoutExtend};
}
definition = {toZigbee, fromZigbee, exposes, meta, configure, endpoint, onEvent, ota, ...definitionWithoutExtend};
}

return definition
Expand Down
23 changes: 0 additions & 23 deletions src/lib/extend.ts

This file was deleted.

18 changes: 1 addition & 17 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,6 @@ export interface DefinitionMeta {

export type Configure = (device: Zh.Device, coordinatorEndpoint: Zh.Endpoint, logger: Logger) => Promise<void>;
export type OnEvent = (type: OnEventType, data: OnEventData, device: Zh.Device, settings: KeyValue, state: KeyValue) => Promise<void>;
export interface Extend {
fromZigbee: Fz.Converter[],
toZigbee: Tz.Converter[],
exposes: Expose[],
configure?: Configure,
meta?: DefinitionMeta,
ota?: DefinitionOta,
onEvent?: OnEvent,
isModernExtend?: false,
}

export interface ModernExtend {
fromZigbee?: Fz.Converter[],
Expand Down Expand Up @@ -208,7 +198,7 @@ export type Definition = {
ota?: DefinitionOta,
generated?: boolean,
} & ({ zigbeeModel: string[] } | { fingerprint: Fingerprint[] })
& ({ extend: Extend | ModernExtend[], fromZigbee?: Fz.Converter[], toZigbee?: Tz.Converter[],
& ({ extend: ModernExtend[], fromZigbee?: Fz.Converter[], toZigbee?: Tz.Converter[],
exposes?: (Expose[] | ((device: Zh.Device | undefined, options: KeyValue | undefined) => Expose[])) } |
{
fromZigbee: Fz.Converter[], toZigbee: Tz.Converter[],
Expand Down Expand Up @@ -276,12 +266,6 @@ export namespace Tuya {
export type MetaTuyaDataPoints = MetaTuyaDataPointsSingle[];
}

export namespace Extend {
export interface options_switch {
disablePowerOnBehavior?: boolean, toZigbee?: Tz.Converter[], fromZigbee?: Fz.Converter[], exposes?: Expose[]
}
}

export namespace Ota {
export type OnProgress = (progress: number, remaining: number) => void;
export interface Version {imageType: number, manufacturerCode: number, fileVersion: number}
Expand Down
7 changes: 3 additions & 4 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,12 +577,11 @@ describe('index.js', () => {
expect(payload).toStrictEqual({temperature: 3.7});

// For multi endpoint property
const SPP04G = index.findByModel('SPP04G');
expect(SPP04G.options.map((t) => t.name)).toStrictEqual(['power_calibration', 'power_precision','current_calibration', 'current_precision',
'voltage_calibration', 'voltage_precision', 'energy_calibration', 'energy_precision', 'state_action']);
const AUA1ZBDSS = index.findByModel('AU-A1ZBDSS');
expect(AUA1ZBDSS.options.map((t) => t.name)).toStrictEqual(['power_calibration', 'power_precision', 'transition', 'state_action']);
payload = {power_left: 5.31};
options = {power_calibration: 100, power_precision: 0}; // calibration for power is percentual
index.postProcessConvertedFromZigbeeMessage(SPP04G, payload, options);
index.postProcessConvertedFromZigbeeMessage(AUA1ZBDSS, payload, options);
expect(payload).toStrictEqual({power_left: 11});

const TS011F_plug_1 = index.definitions.find((d) => d.model == 'TS011F_plug_1');
Expand Down
Loading