Skip to content

Commit 09ef460

Browse files
committed
Don't use state from cache if we want to force a refresh
1 parent 6f697d0 commit 09ef460

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

.changeset/angry-llamas-move.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"homebridge-roomba2": patch
3+
---
4+
5+
Don't use state from cache if we want to force a refresh

src/accessory.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,14 @@ export default class RoombaAccessory implements AccessoryPlugin {
260260
return services;
261261
}
262262

263-
private refreshState() {
263+
/**
264+
* Refresh our knowledge of Roomba's state by connecting to Roomba and getting its status.
265+
* @param force whether to force a refresh, or whether it's okay to use a recent cached state.
266+
* @returns `true` if the state was refreshed, or `false` if a recent cached state is available instead.
267+
*/
268+
private refreshState(force?: boolean): boolean {
264269
const now = Date.now();
265-
if (now - this.lastRefreshState < REFRESH_STATE_COALESCE_MILLIS) {
270+
if (!force && now - this.lastRefreshState < REFRESH_STATE_COALESCE_MILLIS) {
266271
return false;
267272
}
268273
this.lastRefreshState = now;
@@ -450,7 +455,7 @@ export default class RoombaAccessory implements AccessoryPlugin {
450455
callback();
451456

452457
/* Force a refresh of state so we pick up the new state quickly */
453-
this.refreshState();
458+
this.refreshState(true);
454459

455460
/* After sending an action to Roomba, we start watching to ensure HomeKit has up to date status */
456461
this.startWatching();
@@ -481,7 +486,7 @@ export default class RoombaAccessory implements AccessoryPlugin {
481486
callback();
482487

483488
/* Force a refresh of state so we pick up the new state quickly */
484-
this.refreshState();
489+
this.refreshState(true);
485490

486491
if (this.stopBehaviour === "home") {
487492
this.log.debug("Roomba paused, returning to Dock");
@@ -496,7 +501,7 @@ export default class RoombaAccessory implements AccessoryPlugin {
496501
callback();
497502

498503
/* Force a refresh of state so we pick up the new state quickly */
499-
this.refreshState();
504+
this.refreshState(true);
500505

501506
this.log.debug("Roomba paused");
502507
} else if (state.charging) {
@@ -538,7 +543,7 @@ export default class RoombaAccessory implements AccessoryPlugin {
538543
callback();
539544

540545
/* Force a refresh of state so we pick up the new state quickly */
541-
this.refreshState();
546+
this.refreshState(true);
542547

543548
/* After sending an action to Roomba, we start watching to ensure HomeKit has up to date status */
544549
this.startWatching();
@@ -563,7 +568,7 @@ export default class RoombaAccessory implements AccessoryPlugin {
563568
this.log.debug("Roomba docking");
564569

565570
/* Force a refresh of state so we pick up the new state quickly */
566-
this.refreshState();
571+
this.refreshState(true);
567572

568573
break;
569574
case "run":

0 commit comments

Comments
 (0)