Skip to content

Commit

Permalink
Rename throttle setting, expose in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
gracianodias3 committed Jan 13, 2025
1 parent 453979c commit 1d8b00a
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/extension/receive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down
5 changes: 5 additions & 0 deletions lib/util/settings.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion lib/util/throttler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function throttle<Args extends unknown[], Return>(fn: (...args: Args) => Return, wait: number): (...args: Args) => Return | undefined {
export default function throttle<Args extends unknown[]>(fn: (...args: Args) => Promise<void>, wait: number): (...args: Args) => Promise<void> {
let lastCallTime = 0;

return (...args: Args) => {
Expand All @@ -10,5 +10,8 @@ export default function throttle<Args extends unknown[], Return>(fn: (...args: A
lastCallTime = now;
return fn(...args);
}

// Return an empty promise in the case of a throttled call
return Promise.resolve();
};
}
2 changes: 1 addition & 1 deletion test/extensions/receive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down

0 comments on commit 1d8b00a

Please sign in to comment.