Skip to content

Commit

Permalink
Ping 2 times when device is unavailable where second attempt has reco…
Browse files Browse the repository at this point in the history
…very enabled. #6281
  • Loading branch information
Koenkk committed Aug 18, 2021
1 parent 288c0b7 commit f9451ff
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/extension/availabilityNew.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ class AvailabilityNew extends ExtensionTS {

const re = this.pingQueue[0];
let pingedSuccessfully = false;
const attempts = this.isAvailable(re) ? 3 : 1;
const available = this.availabilityCache[re.device.ieeeAddr] || this.isAvailable(re);
const attempts = available ? 2 : 1;
for (let i = 0; i < attempts; i++) {
try {
await re.device.ping();
// Enable recovery if device is marked as available and first ping fails.
const disableRecovery = !(i == 1 && available);
await re.device.ping(disableRecovery);
pingedSuccessfully = true;
logger.debug(`Succesfully pinged '${re.name}' (attempt ${i + 1}/${attempts})`);
break;
Expand Down
4 changes: 4 additions & 0 deletions test/availabilityNew.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ describe('Availability', () => {

await advancedTime(minutes(7));
expect(devices.bulb_color.ping).toHaveBeenCalledTimes(1);
expect(devices.bulb_color.ping).toHaveBeenNthCalledWith(1, true);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability',
'offline', {retain: true, qos: 0}, expect.any(Function));
});
Expand All @@ -104,6 +105,7 @@ describe('Availability', () => {

await advancedTime(minutes(10));
expect(devices.bulb_color.ping).toHaveBeenCalledTimes(1);
expect(devices.bulb_color.ping).toHaveBeenNthCalledWith(1, true);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability',
'offline', {retain: true, qos: 0}, expect.any(Function));
});
Expand All @@ -123,6 +125,8 @@ describe('Availability', () => {
devices.bulb_color.lastSeen = Date.now() + minutes(10);
await advancedTime(minutes(10));
expect(devices.bulb_color.ping).toHaveBeenCalledTimes(2);
expect(devices.bulb_color.ping).toHaveBeenNthCalledWith(1, true);
expect(devices.bulb_color.ping).toHaveBeenNthCalledWith(2, false);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability',
'offline', {retain: true, qos: 0}, expect.any(Function));
});
Expand Down

0 comments on commit f9451ff

Please sign in to comment.