Skip to content

Commit e9aee4c

Browse files
committed
fix: Fix configure on startup crashing coordinator #20526
1 parent e6bcf91 commit e9aee4c

File tree

6 files changed

+26
-1
lines changed

6 files changed

+26
-1
lines changed

lib/extension/configure.ts

+3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ export default class Configure extends Extension {
6666
override async start(): Promise<void> {
6767
setImmediate(async () => {
6868
for (const device of this.zigbee.devices(false)) {
69+
// Sleep 10 seconds between configuring on startup to not DDoS the coordinator
70+
// when many devices have to be configured.
71+
await utils.sleep(10);
6972
await this.configure(device, 'started');
7073
}
7174
});

test/homeassistant.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const logger = require('./stub/logger');
55
const zigbeeHerdsman = require('./stub/zigbeeHerdsman');
66
const flushPromises = require('./lib/flushPromises');
77
const MQTT = require('./stub/mqtt');
8+
const sleep = require('./stub/sleep');
89
const Controller = require('../lib/controller');
910
const fs = require('fs');
1011
const path = require('path');
@@ -42,12 +43,14 @@ describe('HomeAssistant extension', () => {
4243
settings.reRead();
4344
data.writeEmptyState();
4445
MQTT.publish.mockClear();
46+
sleep.mock();
4547
controller = new Controller(false);
4648
await controller.start();
4749
});
4850

4951
afterAll(async () => {
5052
jest.useRealTimers();
53+
sleep.restore();
5154
});
5255

5356
it('Should not have duplicate type/object_ids in a mapping', () => {

test/otaUpdate.test.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const path = require('path');
22

33
const data = require('./stub/data');
44
const logger = require('./stub/logger');
5+
const sleep = require('./stub/sleep');
56
const zigbeeHerdsman = require('./stub/zigbeeHerdsman');
67
const MQTT = require('./stub/mqtt');
78
const settings = require('../lib/util/settings');
@@ -35,6 +36,7 @@ describe('OTA update', () => {
3536
settings.reRead();
3637
jest.useFakeTimers();
3738
controller = new Controller(jest.fn(), jest.fn());
39+
sleep.mock();
3840
await controller.start();
3941
await jest.runOnlyPendingTimers();
4042
await flushPromises();
@@ -47,6 +49,7 @@ describe('OTA update', () => {
4749

4850
afterAll(async () => {
4951
jest.useRealTimers();
52+
sleep.restore();
5053
});
5154

5255
beforeEach(async () => {
@@ -57,7 +60,7 @@ describe('OTA update', () => {
5760
MQTT.publish.mockClear();
5861
});
5962

60-
it('Should OTA update a device', async () => {
63+
it('onlythis Should OTA update a device', async () => {
6164
const device = zigbeeHerdsman.devices.bulb;
6265
const endpoint = device.endpoints[0];
6366
let count = 0;

test/publish.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const data = require('./stub/data');
2+
const sleep = require('./stub/sleep');
23
const logger = require('./stub/logger');
34
const zigbeeHerdsman = require('./stub/zigbeeHerdsman');
45
const zigbeeHerdsmanConverters = require('zigbee-herdsman-converters');
@@ -30,6 +31,7 @@ describe('Publish', () => {
3031
jest.useFakeTimers();
3132
data.writeEmptyState();
3233
controller = new Controller(jest.fn(), jest.fn());
34+
sleep.mock();
3335
await controller.start();
3436
await flushPromises();
3537
});
@@ -56,6 +58,7 @@ describe('Publish', () => {
5658
afterAll(async () => {
5759
jest.runOnlyPendingTimers();
5860
jest.useRealTimers();
61+
sleep.restore();
5962
});
6063

6164
it('Should publish messages to zigbee devices', async () => {

test/receive.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const data = require('./stub/data');
2+
const sleep = require('./stub/sleep');
23
const logger = require('./stub/logger');
34
const stringify = require('json-stable-stringify-without-jsonify');
45
const zigbeeHerdsman = require('./stub/zigbeeHerdsman');
@@ -15,6 +16,7 @@ describe('Receive', () => {
1516
beforeAll(async () => {
1617
jest.useFakeTimers();
1718
controller = new Controller(jest.fn(), jest.fn());
19+
sleep.mock();
1820
await controller.start();
1921
await jest.runOnlyPendingTimers();
2022
await flushPromises();
@@ -30,6 +32,7 @@ describe('Receive', () => {
3032

3133
afterAll(async () => {
3234
jest.useRealTimers();
35+
sleep.restore();
3336
});
3437

3538
it('Should handle a zigbee message', async () => {

test/stub/sleep.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const utils = require('../../lib/util/utils');
2+
const spy = jest.spyOn(utils.default, 'sleep');
3+
4+
export function mock() {
5+
spy.mockImplementation(() => {});
6+
}
7+
8+
export function restore() {
9+
spy.mockRestore();
10+
}

0 commit comments

Comments
 (0)