From 69fe0a37d90c234ee98e41e6682564196a71ffd7 Mon Sep 17 00:00:00 2001 From: Julian <53574915+JulianLepinski@users.noreply.github.com> Date: Wed, 16 Nov 2022 00:29:34 +0000 Subject: [PATCH 1/3] adding support for whitelisting homes --- README.md | 1 + config.schema.json | 10 +++++++++- src/config.ts | 2 ++ src/platform.ts | 11 ++++++++++- 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 33d39745..55d9d5c1 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,7 @@ Before configuration, please goto [Tuya IoT Platform](https://iot.tuya.com) - `options.username` - **required** : Username - `options.password` - **required** : Password - `options.appSchema` - **required** : App schema. 'tuyaSmart' for Tuya Smart App, 'smartlife' for Smart Life App. +- `options.homeWhitelist` - **optional**: If present, only includes devices matching this Home ID value. **Note:** - The app account can't be used in multiple HomeBridge/HomeAssistant instance at the same time! Please consider using different app accounts instead. diff --git a/config.schema.json b/config.schema.json index 95a9372d..1766ba44 100644 --- a/config.schema.json +++ b/config.schema.json @@ -81,7 +81,15 @@ "condition": { "functionBody": "return model.options.projectType === '2';" } - } + }, + "homeWhitelist": { + "title": "Whitelisted Home ID", + "type": "integer", + "required": false, + "condition": { + "functionBody": "return model.options.projectType === '2';" + } + } } } } diff --git a/src/config.ts b/src/config.ts index 89458d22..03aa575a 100644 --- a/src/config.ts +++ b/src/config.ts @@ -17,6 +17,7 @@ export interface TuyaPlatformHomeConfigOptions { username: string; password: string; appSchema: string; + homeWhitelist: number; } export type TuyaPlatformConfigOptions = TuyaPlatformCustomConfigOptions | TuyaPlatformHomeConfigOptions; @@ -41,5 +42,6 @@ export const homeOptionsSchema = { username: { type: 'string', required: true }, password: { type: 'string', required: true }, appSchema: { 'type': 'string', required: true }, + homeWhitelist: { 'type': 'integer', 'minimum': 1}, }, }; diff --git a/src/platform.ts b/src/platform.ts index 772ff31d..013997c9 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -254,7 +254,16 @@ export class TuyaPlatform implements DynamicPlatformPlugin { const homeIDList: number[] = []; for (const { home_id, name } of res.result) { this.log.info(`Got home_id=${home_id}, name=${name}`); - homeIDList.push(home_id); + if (this.options.homeWhitelist) { + if (home_id === this.options.homeWhitelist) { + this.log.info(`Matched home_id=${home_id} to whitelist; adding to homeIDList`); + homeIDList.push(home_id); + } else { + this.log.info(`Did not match home_id=${home_id} to whitelist; not adding to homeIDList`); + } + } else { + homeIDList.push(home_id); + } } if (homeIDList.length === 0) { From 9fbd546d271948a0b50a8b018145aa1e19032dbd Mon Sep 17 00:00:00 2001 From: Julian <53574915+JulianLepinski@users.noreply.github.com> Date: Wed, 16 Nov 2022 03:31:42 +0000 Subject: [PATCH 2/3] changing homeWhitelist to an array --- README.md | 2 +- config.schema.json | 11 ++++++----- src/config.ts | 4 ++-- src/platform.ts | 6 +++--- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 55d9d5c1..cdd5b0d1 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ Before configuration, please goto [Tuya IoT Platform](https://iot.tuya.com) - `options.username` - **required** : Username - `options.password` - **required** : Password - `options.appSchema` - **required** : App schema. 'tuyaSmart' for Tuya Smart App, 'smartlife' for Smart Life App. -- `options.homeWhitelist` - **optional**: If present, only includes devices matching this Home ID value. +- `options.homeWhitelist` - **optional**: An array of integer home ID values to whitelist. If present, only includes devices matching this Home ID value. **Note:** - The app account can't be used in multiple HomeBridge/HomeAssistant instance at the same time! Please consider using different app accounts instead. diff --git a/config.schema.json b/config.schema.json index 1766ba44..d02cf715 100644 --- a/config.schema.json +++ b/config.schema.json @@ -83,11 +83,12 @@ } }, "homeWhitelist": { - "title": "Whitelisted Home ID", - "type": "integer", - "required": false, - "condition": { - "functionBody": "return model.options.projectType === '2';" + "title": "Whitelisted Home IDs", + "description": "An optional list of Home IDs to match. If blank, all homes are matched.", + "type": "array", + "items": { + "title": "Home ID", + "type": "integer" } } } diff --git a/src/config.ts b/src/config.ts index 03aa575a..332f6dcb 100644 --- a/src/config.ts +++ b/src/config.ts @@ -17,7 +17,7 @@ export interface TuyaPlatformHomeConfigOptions { username: string; password: string; appSchema: string; - homeWhitelist: number; + homeWhitelist: Array; } export type TuyaPlatformConfigOptions = TuyaPlatformCustomConfigOptions | TuyaPlatformHomeConfigOptions; @@ -42,6 +42,6 @@ export const homeOptionsSchema = { username: { type: 'string', required: true }, password: { type: 'string', required: true }, appSchema: { 'type': 'string', required: true }, - homeWhitelist: { 'type': 'integer', 'minimum': 1}, + homeWhitelist: { 'type': 'array'}, }, }; diff --git a/src/platform.ts b/src/platform.ts index 013997c9..e02c81e7 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -255,11 +255,11 @@ export class TuyaPlatform implements DynamicPlatformPlugin { for (const { home_id, name } of res.result) { this.log.info(`Got home_id=${home_id}, name=${name}`); if (this.options.homeWhitelist) { - if (home_id === this.options.homeWhitelist) { - this.log.info(`Matched home_id=${home_id} to whitelist; adding to homeIDList`); + if (this.options.homeWhitelist.includes(home_id)) { + this.log.info(`Found home_id=${home_id} in whitelist; including devices from this home.`); homeIDList.push(home_id); } else { - this.log.info(`Did not match home_id=${home_id} to whitelist; not adding to homeIDList`); + this.log.info(`Did not find home_id=${home_id} in whitelist; excluding devices from this home.`); } } else { homeIDList.push(home_id); From 797be318765bd2b220abf43d877b4f91af4b3928 Mon Sep 17 00:00:00 2001 From: gaosen <0x5e@sina.cn> Date: Wed, 16 Nov 2022 11:53:15 +0800 Subject: [PATCH 3/3] Update config.schema.json --- config.schema.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/config.schema.json b/config.schema.json index d02cf715..6fc10e88 100644 --- a/config.schema.json +++ b/config.schema.json @@ -89,6 +89,9 @@ "items": { "title": "Home ID", "type": "integer" + }, + "condition": { + "functionBody": "return model.options.projectType === '2';" } } }