Skip to content

Commit

Permalink
Add new option
Browse files Browse the repository at this point in the history
* New possibility of specifying the order of the buttons to be pressed
  • Loading branch information
denisgabriel5 committed Jun 30, 2024
1 parent 83e7e83 commit 05b535f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 20 deletions.
11 changes: 11 additions & 0 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@
"functionBody": "return model.intercomType === \"shellyUni\";"
}
},
"shellyUniButtonsOrder": {
"title": "Buttons pressing order",
"type": "string",
"required": true,
"default": "open-talk",
"placeholder": "open-talk-talk",
"description": "Some intercoms require different pressing order. The default one is pressing the Talk button, then the Open button, but others might require an additional press of the Talk or Open button.",
"condition": {
"functionBody": "return model.intercomType === \"shellyUni\";"
}
},
"shellyUniButtonsTimeout": {
"title": "Buttons timeout",
"type": "number",
Expand Down
1 change: 1 addition & 0 deletions src/intercomPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface IntercomConfig extends PlatformConfig {
shellyUniTalkUrl?: string;
shellyUniOpenUrl?: string;
shellyUniButtonsTimeout?: number;
shellyUniButtonsOrder?: string;
shellyUniRingSuppressionTimeout?: number;
}

Expand Down
61 changes: 41 additions & 20 deletions src/shellyUni/shellyUniLockMechanism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ export class ShellyUniLockMechanism {
private readonly Service: typeof Service;
private readonly Characteristic: typeof Characteristic;
private displayName = 'LockMechanism';
// private targetState: CharacteristicValue;
// private currentState: CharacteristicValue;
public service: Service;

constructor(
Expand Down Expand Up @@ -59,35 +57,58 @@ export class ShellyUniLockMechanism {
/**
* Handle requests to set the "Lock Target State" characteristic
*/
handleLockTargetStateSet(value: CharacteristicValue) {
async handleLockTargetStateSet(value: CharacteristicValue) {
this.parent.platform.log.debug('Triggered SET LockTargetState: ', value);

// the door locks automatially so we ignore locking requests altogether
if (value === this.Characteristic.LockCurrentState.SECURED) {
return;
}

// in order to open the intercom door you must press the talking button and then the open door button
// so press the talk button with the following setTimeout
setTimeout(async () => {
await axios.get(this.parent.config.shellyUniTalkUrl!);
this.parent.platform.log.debug('Intercom talk button pressed');
}, 0);

// and after roughly 1 second press the second button and also mark the intercom as open/unlocked/unsecured
setTimeout(async () => {
await axios.get(this.parent.config.shellyUniOpenUrl!);
this.parent.platform.log.debug('Intercom open button pressed');
this.service.updateCharacteristic(this.Characteristic.LockTargetState, this.Characteristic.LockCurrentState.UNSECURED);
this.service.updateCharacteristic(this.Characteristic.LockCurrentState, this.Characteristic.LockCurrentState.UNSECURED);
this.parent.platform.log.debug('Intercom opened');
}, this.parent.config.shellyUniButtonsTimeout! * 1000);

// in the end mark the intercom as closed/locked/secured
// press the buttons in the specified order
const buttons = this.parent.config.shellyUniButtonsOrder!.split('-');

for (let i = 0; i < buttons.length; i++) {
switch (buttons[i]) {
case 'open':
axios.get(this.parent.config.shellyUniOpenUrl!);
this.parent.platform.log.debug('Intercom Open button pressed');
break;

case 'talk':
axios.get(this.parent.config.shellyUniTalkUrl!);
this.parent.platform.log.debug('Intercom Talk button pressed');
break;

default:
this.parent.platform.log.error(`Button ${buttons[i]} is wrong!`);
break;
}

if (i === buttons.length - 1) {
// mark the intercom as open/unlocked/unsecured
this.service.updateCharacteristic(this.Characteristic.LockTargetState, this.Characteristic.LockCurrentState.UNSECURED);
this.service.updateCharacteristic(this.Characteristic.LockCurrentState, this.Characteristic.LockCurrentState.UNSECURED);
this.parent.platform.log.debug('Intercom opened');

break;
} else {
await this.sleep(this.parent.config.shellyUniButtonsTimeout! * 1000);
}
}

// in the end, mark the intercom as closed/locked/secured
setTimeout(() => {
this.service.updateCharacteristic(this.Characteristic.LockTargetState, this.Characteristic.LockCurrentState.SECURED);
this.service.updateCharacteristic(this.Characteristic.LockCurrentState, this.Characteristic.LockCurrentState.SECURED);
this.parent.platform.log.debug('Intercom closed');
}, this.parent.config.timeout! * 1000);
}

/**
* Delays the execution for a number of specified seconds
*/
sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
}

0 comments on commit 05b535f

Please sign in to comment.