diff --git a/config.schema.json b/config.schema.json index fc51bfe..6fa16ea 100644 --- a/config.schema.json +++ b/config.schema.json @@ -64,8 +64,9 @@ "pollingInterval": { "title": "Polling Interval", "type": "number", - "description": "The frequency in seconds that the plugin polls the cloud to get device updates. Can be left empty to only fetch on request.", - "required": false + "description": "The frequency in seconds that the plugin polls the cloud to get device updates. Can be left empty to only fetch on request. Must be 300 or larger", + "required": false, + "minimum": 300 } } }, diff --git a/src/TuyaWebApi.ts b/src/TuyaWebApi.ts index 19e56f4..388e63c 100644 --- a/src/TuyaWebApi.ts +++ b/src/TuyaWebApi.ts @@ -298,7 +298,7 @@ export class TuyaWebApi { public async sendRequest> (url: AxiosRequestConfig['url'], data: AxiosRequestConfig['data'], method: AxiosRequestConfig['method']) : Promise<{ data: T & { header: TuyaHeader } }> { - this.log?.debug(`Sending HTTP ${method} request to ${url}.`); + this.log?.debug('Sending HTTP %s request to %s - Header: %s.', method, url, JSON.stringify(data.header)); const response = await axios({ baseURL: this.session?.areaBaseUrl, url, diff --git a/src/platform.ts b/src/platform.ts index ba3fe62..5bd780b 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -1,5 +1,5 @@ import {API, Characteristic, DynamicPlatformPlugin, Logger, PlatformAccessory, Service} from 'homebridge'; -import {PLATFORM_NAME, PLUGIN_NAME} from './settings'; +import {PLATFORM_NAME, PLUGIN_NAME, TUYA_DISCOVERY_TIMEOUT} from './settings'; import {TuyaDevice, TuyaDeviceType, TuyaDeviceTypes, TuyaPlatforms, TuyaWebApi} from './TuyaWebApi'; import { BaseAccessory, @@ -83,13 +83,15 @@ export class TuyaWebPlatform implements DynamicPlatformPlugin { await this.discoverDevices(); if (this.pollingInterval) { - this.log?.info('Enable cloud polling with interval %ss', this.pollingInterval); + //Tuya will probably still complain if we fetch a new request on the exact second. + const pollingInterval = Math.max(this.pollingInterval, TUYA_DISCOVERY_TIMEOUT + 5); + this.log?.info('Enable cloud polling with interval %ss', pollingInterval); // Set interval for refreshing device states setInterval(() => { this.refreshDeviceStates().catch((error) => { this.log.error(error.message); }); - }, this.pollingInterval * 1000); + }, pollingInterval * 1000); } } catch (e) { if(e instanceof AuthenticationError) { @@ -133,6 +135,8 @@ export class TuyaWebPlatform implements DynamicPlatformPlugin { return; } + devices = this.filterDeviceList(devices); + // Refresh device states for (const device of devices) { const uuid = this.api.hap.uuid.generate(device.id); @@ -183,6 +187,11 @@ export class TuyaWebPlatform implements DynamicPlatformPlugin { /* eslint-enable @typescript-eslint/no-explicit-any */ } + private filterDeviceList(devices: TuyaDevice[]) { + const whitelistedSceneIds = this.getWhitelistedSceneIds(devices); + return devices.filter(d => d.dev_type !== 'scene' || whitelistedSceneIds.includes(d.id)); + } + async discoverDevices(): Promise { let devices = await this.tuyaWebApi.discoverDevices() || []; @@ -193,8 +202,7 @@ export class TuyaWebPlatform implements DynamicPlatformPlugin { this.log.info('Device type for "%s" is overruled in config to: "%s"', defaults.device.name, defaults.device.dev_type); } - const whitelistedSceneIds = this.getWhitelistedSceneIds(devices); - devices = devices.filter(d => d.dev_type !== 'scene' || whitelistedSceneIds.includes(d.id)); + devices = this.filterDeviceList(devices); const cachedDeviceIds = [...this.accessories.keys()]; const availableDeviceIds = devices.map(d => this.generateUUID(d.id));