Skip to content

Commit

Permalink
Don't use state from cache if we want to force a refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
karlvr committed Nov 19, 2022
1 parent 6f697d0 commit 09ef460
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-llamas-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"homebridge-roomba2": patch
---

Don't use state from cache if we want to force a refresh
19 changes: 12 additions & 7 deletions src/accessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,14 @@ export default class RoombaAccessory implements AccessoryPlugin {
return services;
}

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

/* Force a refresh of state so we pick up the new state quickly */
this.refreshState();
this.refreshState(true);

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

/* Force a refresh of state so we pick up the new state quickly */
this.refreshState();
this.refreshState(true);

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

/* Force a refresh of state so we pick up the new state quickly */
this.refreshState();
this.refreshState(true);

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

/* Force a refresh of state so we pick up the new state quickly */
this.refreshState();
this.refreshState(true);

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

/* Force a refresh of state so we pick up the new state quickly */
this.refreshState();
this.refreshState(true);

break;
case "run":
Expand Down

0 comments on commit 09ef460

Please sign in to comment.