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

Strict config validate for deviceOverrides #278

Merged
merged 1 commit into from
Apr 13, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
- Support Fan light temperature and color. (#184)
- Support Humidifier light. (#184)
- Expose energy usage for outlets/switches. (#190) Thanks @lstrojny for the contribution
- Strict config validate for `deviceOverrides`.


## [1.6.0] - (2022.12.3)
Expand Down
22 changes: 11 additions & 11 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import { TuyaDeviceSchemaProperty, TuyaDeviceSchemaType } from './device/TuyaDev

export interface TuyaPlatformDeviceSchemaConfig {
code: string;
newCode: string;
type: TuyaDeviceSchemaType;
property: TuyaDeviceSchemaProperty;
onGet: string;
onSet: string;
hidden: boolean;
newCode?: string;
type?: TuyaDeviceSchemaType;
property?: TuyaDeviceSchemaProperty;
onGet?: string;
onSet?: string;
hidden?: boolean;
}

export interface TuyaPlatformDeviceConfig {
id: string;
category: string;
schema: Array<TuyaPlatformDeviceSchemaConfig>;
category?: string;
schema?: Array<TuyaPlatformDeviceSchemaConfig>;
}

export interface TuyaPlatformCustomConfigOptions {
Expand All @@ -24,7 +24,7 @@ export interface TuyaPlatformCustomConfigOptions {
accessKey: string;
username: string;
password: string;
deviceOverrides: Array<TuyaPlatformDeviceConfig>;
deviceOverrides?: Array<TuyaPlatformDeviceConfig>;
}

export interface TuyaPlatformHomeConfigOptions {
Expand All @@ -36,8 +36,8 @@ export interface TuyaPlatformHomeConfigOptions {
username: string;
password: string;
appSchema: string;
homeWhitelist: Array<number>;
deviceOverrides: Array<TuyaPlatformDeviceConfig>;
homeWhitelist?: Array<number>;
deviceOverrides?: Array<TuyaPlatformDeviceConfig>;
}

export type TuyaPlatformConfigOptions = TuyaPlatformCustomConfigOptions | TuyaPlatformHomeConfigOptions;
Expand Down
71 changes: 60 additions & 11 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,70 @@ export class TuyaPlatform implements DynamicPlatformPlugin {
public deviceManager?: TuyaDeviceManager;
public accessoryHandlers: BaseAccessory[] = [];

validate(config) {
validate() {
let result;
if (!config.options) {
this.log.warn('Not configured, exit.');
if (!this.options) {
this.log.error('Not configured, exit.');
return false;
} else if (config.options.projectType === '1') {
result = new Validator().validate(config.options, customOptionsSchema);
} else if (config.options.projectType === '2') {
result = new Validator().validate(config.options, homeOptionsSchema);
} else if (this.options.projectType === '1') {
result = new Validator().validate(this.options, customOptionsSchema);
} else if (this.options.projectType === '2') {
result = new Validator().validate(this.options, homeOptionsSchema);
} else {
this.log.warn(`Unsupported projectType: ${config.options.projectType}, exit.`);
this.log.error(`Unsupported projectType: ${this.options['projectType']}, exit.`);
return false;
}
result.errors.forEach(error => this.log.error(error.stack));
return result.errors.length === 0;
if (result.errors.length > 0) {
return false;
}

if (!this.validateDeviceOverrides() || !this.validateSchema()) {
return false;
}

return true;
}

validateDeviceOverrides() {
const idMap = new Map();
for (const item of this.options.deviceOverrides!) {
if (idMap.has(item.id)) {
idMap.get(item.id)?.push(item);
} else {
idMap.set(item.id, [item]);
}
}
for (const items of idMap.values()) {
if (items.length > 1) {
this.log.error('"deviceOverrides" conflict, "id" must be unique: %o.', items);
return false;
}
}
return true;
}

validateSchema() {
for (const deviceOverride of this.options.deviceOverrides!) {
if (!deviceOverride.schema) {
continue;
}
const idMap = new Map();
for (const item of deviceOverride.schema) {
if (idMap.has(item.code)) {
idMap.get(item.code)?.push(item);
} else {
idMap.set(item.code, [item]);
}
}
for (const items of idMap.values()) {
if (items.length > 1) {
this.log.error('"schema" conflict, "code" must be unique: %o.', items);
return false;
}
}
}
return true;
}

constructor(
Expand All @@ -55,7 +104,7 @@ export class TuyaPlatform implements DynamicPlatformPlugin {
public readonly api: API,
) {

if (!this.validate(config)) {
if (!this.validate()) {
return;
}

Expand All @@ -66,7 +115,7 @@ export class TuyaPlatform implements DynamicPlatformPlugin {
// in order to ensure they weren't added to homebridge already. This event can also be used
// to start discovery of new accessories.
this.api.on('didFinishLaunching', async () => {
log.debug('Executed didFinishLaunching callback');
this.log.debug('Executed didFinishLaunching callback');
// run the method to discover / register your devices as accessories
await this.initDevices();
});
Expand Down