diff --git a/lib/extension/receive.ts b/lib/extension/receive.ts index 2ee663d2bd..1f4ca1422f 100755 --- a/lib/extension/receive.ts +++ b/lib/extension/receive.ts @@ -149,8 +149,8 @@ export default class Receive extends Extension { // Check if we have to debounce or throttle if (data.device.options.debounce) { this.publishDebounce(data.device, payload, data.device.options.debounce, data.device.options.debounce_ignore); - } else if (data.device.options.throttle) { - await this.publishThrottle(data.device, payload, data.device.options.throttle); + } else if (data.device.options.min_time_between_payloads) { + await this.publishThrottle(data.device, payload, data.device.options.min_time_between_payloads); } else { await this.publishEntityState(data.device, payload); } diff --git a/lib/types/types.d.ts b/lib/types/types.d.ts index b3158b4359..f9b80880a3 100644 --- a/lib/types/types.d.ts +++ b/lib/types/types.d.ts @@ -202,7 +202,7 @@ declare global { optimistic?: boolean; debounce?: number; debounce_ignore?: string[]; - throttle?: number; + min_time_between_payloads?: number; filtered_attributes?: string[]; filtered_cache?: string[]; filtered_optimistic?: string[]; diff --git a/lib/util/settings.schema.json b/lib/util/settings.schema.json index dc7ef0366a..ef6e0634b1 100644 --- a/lib/util/settings.schema.json +++ b/lib/util/settings.schema.json @@ -761,6 +761,11 @@ "title": "QoS", "description": "QoS level for MQTT messages of this device" }, + "min_time_between_payloads": { + "type": "number", + "title": "Minimum time between payloads", + "description": "The minimum time between payloads in seconds. Payloads received whilst the device is being throttled will be discarded" + }, "debounce": { "type": "number", "title": "Debounce", diff --git a/lib/util/throttler.ts b/lib/util/throttler.ts index 76ffbab034..6a1d918093 100644 --- a/lib/util/throttler.ts +++ b/lib/util/throttler.ts @@ -1,4 +1,4 @@ -export default function throttle(fn: (...args: Args) => Return, wait: number): (...args: Args) => Return | undefined { +export default function throttle(fn: (...args: Args) => Promise, wait: number): (...args: Args) => Promise { let lastCallTime = 0; return (...args: Args) => { @@ -10,5 +10,8 @@ export default function throttle(fn: (...args: A lastCallTime = now; return fn(...args); } + + // Return an empty promise in the case of a throttled call + return Promise.resolve(); }; } diff --git a/test/extensions/receive.test.ts b/test/extensions/receive.test.ts index bd5b2a37a2..b3ea54bb5c 100644 --- a/test/extensions/receive.test.ts +++ b/test/extensions/receive.test.ts @@ -352,7 +352,7 @@ describe('Extension: Receive', () => { it('Should throttle multiple messages from spamming devices', async () => { const device = devices.SPAMMER; const throttle_for_testing = 1; - settings.set(['device_options', 'throttle'], throttle_for_testing); + settings.set(['device_options', 'min_time_between_payloads'], throttle_for_testing); settings.set(['device_options', 'retain'], true); settings.set(['devices', device.ieeeAddr, 'friendly_name'], 'spammer1');