Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fix autoclose bug in Home and add support of virtual close (or not) #153

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,17 @@ Value is in milliseconds:
}
```

You can also configure an `autoCloseDelay`
You can also configure an `autoCloseDelay` and `autoCloseVirtual` to physically or virtually close the garage door.

```json
{
"platforms": [
{
"settings": {
"1529094720": {"autoCloseDelay": 300000} // 5 minutes
"1529094720": {
"autoCloseDelay": 300000, // 5 minutes
"autoCloseVirtual": true
}
}
}
]
Expand Down
12 changes: 8 additions & 4 deletions src/accessories/garageDoorOpener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {Characteristic, Service} from 'src/config/hap';
type GarageDoorOpenerSettings = {
delay?: number;
autoCloseDelay?: number;
autoCloseVirtual?: boolean;
};
type GarageDoorOpenerState = {
currentDoorState: number;
Expand All @@ -52,7 +53,7 @@ export const setupGarageDoorOpener = (

const {deviceId, endpointId, state, settings} = context;

const {delay: garageDoorDelay = DEFAULT_GARAGE_DOOR_DELAY, autoCloseDelay} = settings;
const {delay: garageDoorDelay = DEFAULT_GARAGE_DOOR_DELAY, autoCloseDelay, autoCloseVirtual} = settings;

const assignState = (update: Partial<GarageDoorOpenerState>): void => {
Object.assign(state, update);
Expand Down Expand Up @@ -208,7 +209,9 @@ export const setupGarageDoorOpener = (
callback();
return;
}
await toggleGarageDoor();
if (targetDoorState == TargetDoorState.OPEN || (targetDoorState == TargetDoorState.CLOSED && !autoCloseVirtual)) {
await toggleGarageDoor();
}
assignCurrentDoorState(nextCurrentDoorState);

// Handle Stopped state, if we are stopped, wait one second and trigger again to reverse course
Expand Down Expand Up @@ -238,15 +241,16 @@ export const setupGarageDoorOpener = (
assignCurrentDoorState(CurrentDoorState.OPEN);
if (autoCloseDelay) {
await waitFor(`${deviceId}.pending`, autoCloseDelay);
assignCurrentDoorState(CurrentDoorState.CLOSED);
const targetDoorState = service.getCharacteristic(Characteristic.TargetDoorState);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would trigger a new emit and re run in .on(CharacteristicEventTypes.SET

targetDoorState.setValue(Characteristic.TargetDoorState.CLOSED);
}
} catch (err) {
// debug(`Aborted OPEN update with delay=${chalkNumber(delay)}`);
}
break;
}
case CurrentDoorState.CLOSING: {
const delay = (state.computedPosition * garageDoorDelay) / 100;
const delay = autoCloseDelay && autoCloseVirtual ? 1 * 1000 : (state.computedPosition * garageDoorDelay) / 100;
// debug(`delay=${chalkNumber(delay)}`);
try {
await waitFor(`${deviceId}.pending`, delay);
Expand Down