diff --git a/lib/extension/configure.ts b/lib/extension/configure.ts index 200b275ea..39906c6b1 100644 --- a/lib/extension/configure.ts +++ b/lib/extension/configure.ts @@ -66,6 +66,9 @@ export default class Configure extends Extension { override async start(): Promise { setImmediate(async () => { for (const device of this.zigbee.devices(false)) { + // Sleep 10 seconds between configuring on startup to not DDoS the coordinator + // when many devices have to be configured. + await utils.sleep(10); await this.configure(device, 'started'); } }); diff --git a/test/homeassistant.test.js b/test/homeassistant.test.js index 6365e3a0b..bae0f8e0a 100644 --- a/test/homeassistant.test.js +++ b/test/homeassistant.test.js @@ -5,6 +5,7 @@ const logger = require('./stub/logger'); const zigbeeHerdsman = require('./stub/zigbeeHerdsman'); const flushPromises = require('./lib/flushPromises'); const MQTT = require('./stub/mqtt'); +const sleep = require('./stub/sleep'); const Controller = require('../lib/controller'); const fs = require('fs'); const path = require('path'); @@ -42,12 +43,14 @@ describe('HomeAssistant extension', () => { settings.reRead(); data.writeEmptyState(); MQTT.publish.mockClear(); + sleep.mock(); controller = new Controller(false); await controller.start(); }); afterAll(async () => { jest.useRealTimers(); + sleep.restore(); }); it('Should not have duplicate type/object_ids in a mapping', () => { diff --git a/test/otaUpdate.test.js b/test/otaUpdate.test.js index 51fcc1ad6..648e77448 100644 --- a/test/otaUpdate.test.js +++ b/test/otaUpdate.test.js @@ -2,6 +2,7 @@ const path = require('path'); const data = require('./stub/data'); const logger = require('./stub/logger'); +const sleep = require('./stub/sleep'); const zigbeeHerdsman = require('./stub/zigbeeHerdsman'); const MQTT = require('./stub/mqtt'); const settings = require('../lib/util/settings'); @@ -35,6 +36,7 @@ describe('OTA update', () => { settings.reRead(); jest.useFakeTimers(); controller = new Controller(jest.fn(), jest.fn()); + sleep.mock(); await controller.start(); await jest.runOnlyPendingTimers(); await flushPromises(); @@ -47,6 +49,7 @@ describe('OTA update', () => { afterAll(async () => { jest.useRealTimers(); + sleep.restore(); }); beforeEach(async () => { @@ -57,7 +60,7 @@ describe('OTA update', () => { MQTT.publish.mockClear(); }); - it('Should OTA update a device', async () => { + it('onlythis Should OTA update a device', async () => { const device = zigbeeHerdsman.devices.bulb; const endpoint = device.endpoints[0]; let count = 0; diff --git a/test/publish.test.js b/test/publish.test.js index 1a0f0d917..c548ebb25 100644 --- a/test/publish.test.js +++ b/test/publish.test.js @@ -1,4 +1,5 @@ const data = require('./stub/data'); +const sleep = require('./stub/sleep'); const logger = require('./stub/logger'); const zigbeeHerdsman = require('./stub/zigbeeHerdsman'); const zigbeeHerdsmanConverters = require('zigbee-herdsman-converters'); @@ -30,6 +31,7 @@ describe('Publish', () => { jest.useFakeTimers(); data.writeEmptyState(); controller = new Controller(jest.fn(), jest.fn()); + sleep.mock(); await controller.start(); await flushPromises(); }); @@ -56,6 +58,7 @@ describe('Publish', () => { afterAll(async () => { jest.runOnlyPendingTimers(); jest.useRealTimers(); + sleep.restore(); }); it('Should publish messages to zigbee devices', async () => { diff --git a/test/receive.test.js b/test/receive.test.js index a317b1eeb..f0b0e1554 100755 --- a/test/receive.test.js +++ b/test/receive.test.js @@ -1,4 +1,5 @@ const data = require('./stub/data'); +const sleep = require('./stub/sleep'); const logger = require('./stub/logger'); const stringify = require('json-stable-stringify-without-jsonify'); const zigbeeHerdsman = require('./stub/zigbeeHerdsman'); @@ -15,6 +16,7 @@ describe('Receive', () => { beforeAll(async () => { jest.useFakeTimers(); controller = new Controller(jest.fn(), jest.fn()); + sleep.mock(); await controller.start(); await jest.runOnlyPendingTimers(); await flushPromises(); @@ -30,6 +32,7 @@ describe('Receive', () => { afterAll(async () => { jest.useRealTimers(); + sleep.restore(); }); it('Should handle a zigbee message', async () => { diff --git a/test/stub/sleep.js b/test/stub/sleep.js new file mode 100644 index 000000000..0a74c3aae --- /dev/null +++ b/test/stub/sleep.js @@ -0,0 +1,10 @@ +const utils = require('../../lib/util/utils'); +const spy = jest.spyOn(utils.default, 'sleep'); + +export function mock() { + spy.mockImplementation(() => {}); +} + +export function restore() { + spy.mockRestore(); +}