Skip to content

Commit

Permalink
Delay marking unoccupied
Browse files Browse the repository at this point in the history
Resolves #2
  • Loading branch information
apexskier committed Jan 20, 2024
1 parent d93d82d commit 9872e58
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 15 deletions.
8 changes: 8 additions & 0 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@
"default": 5000,
"required": true
},
"occupancyTimeout": {
"title": "Occupancy Timeout",
"type": "number",
"description": "Time in milliseconds to wait before setting unoccupied (approximately). See https://github.com/apexskier/homebridge-eero-presence/issues/2 for more info.",
"minimum": 1,
"default": 20000,
"required": true
},
"enableStatusLightAccessories": {
"title": "Enable Status Light Accessories",
"type": "boolean",
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface Config {
userToken: string;
occupancyTimeout?: number;
pollTime?: number;
network?: string;
minSignal?: number;
Expand Down
60 changes: 46 additions & 14 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,29 @@ export class EeroPresenceHomebridgePlatform implements DynamicPlatformPlugin {
});
}

private lastOccupied = new Map<string, Date>();

private async checkOccupancy(deviceEndpoint: string) {
const { data: devices } = await (
const { data: devices } = (await (
await fetch(`https://api-user.e2ro.com${deviceEndpoint}`, {
method: "GET",
headers: {
"content-type": "application/json",
cookie: `s=${this.config.userToken}`,
},
})
).json();
).json()) as {
data: ReadonlyArray<{
connected: boolean;
connection_type: string;
connectivity: { score: number };
device_type: string;
display_name: string;
source: { serial_number: string; location: string };
}>;
};

const now = new Date();

const connectedDevices = devices
.filter(
Expand All @@ -170,11 +183,9 @@ export class EeroPresenceHomebridgePlatform implements DynamicPlatformPlugin {
.filter(
({ connectivity: { score } }) => score > (this.config.minSignal || 0.7),
);
const connectedEeros = new Set(
connectedDevices.map(({ source: { serial_number } }) =>
this.api.hap.uuid.generate(serial_number),
),
);
connectedDevices.forEach(({ source: { serial_number } }) => {
this.lastOccupied.set(this.api.hap.uuid.generate(serial_number), now);
});
this.log.debug(
"connected devices:",
connectedDevices
Expand All @@ -184,15 +195,36 @@ export class EeroPresenceHomebridgePlatform implements DynamicPlatformPlugin {
)
.join(", "),
);

this.accessories.forEach((accessory) => {
accessory
?.getService(this.Service.OccupancySensor)
?.updateCharacteristic(
const status =
now.getTime() -
(this.lastOccupied.get(accessory.UUID)?.getTime() ?? 0) <
(this.config.occupancyTimeout ?? 20000)
? this.Characteristic.OccupancyDetected.OCCUPANCY_DETECTED
: this.Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED;
const occupancyService = accessory.getService(
this.Service.OccupancySensor,
);
if (occupancyService) {
const detected = occupancyService.getCharacteristic(
this.Characteristic.OccupancyDetected,
connectedEeros.has(accessory.UUID)
? this.Characteristic.OccupancyDetected.OCCUPANCY_DETECTED
: this.Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED,
);
).value;
if (detected !== status) {
this.log.info(
`${accessory.displayName} now ${
status ===
this.Characteristic.OccupancyDetected.OCCUPANCY_DETECTED
? "occupied"
: "unoccupied"
}`,
);
occupancyService?.updateCharacteristic(
this.Characteristic.OccupancyDetected,
status,
);
}
}
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/platformAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class EeroPresencePlatformAccessory {
}

async fetch(input: RequestInfo | URL, init?: RequestInit) {
let response;
let response: Response;
try {
response = await fetch(input, {
...init,
Expand Down

0 comments on commit 9872e58

Please sign in to comment.