Skip to content

Commit

Permalink
Improve device discovery handling
Browse files Browse the repository at this point in the history
  • Loading branch information
milo526 committed Aug 14, 2020
1 parent e5b4369 commit 7deec29
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
5 changes: 3 additions & 2 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/TuyaWebApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ export class TuyaWebApi {
public async sendRequest<T = Record<string, unknown>>
(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,
Expand Down
18 changes: 13 additions & 5 deletions src/platform.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<void> {
let devices = await this.tuyaWebApi.discoverDevices() || [];

Expand All @@ -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));
Expand Down

0 comments on commit 7deec29

Please sign in to comment.