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

[homekit] add support for flag "inverted" to lock accessory #10169

Merged
merged 2 commits into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion bundles/org.openhab.io.homekit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ Switch motionsensor_tampered "Motion Sensor Tampered"
| | | LockControl | Number, Switch | status of physical control lock. values: 0/OFF=CONTROL LOCK DISABLED, 1/ON=CONTROL LOCK ENABLED |
| | | CoolingThresholdTemperature | Number | maximum temperature that must be reached before cooling is turned on. min/max/step can configured at item level, e.g. minValue=10.5, maxValue=50, step=2] |
| | | HeatingThresholdTemperature | Number | minimum temperature that must be reached before heating is turned on. min/max/step can configured at item level, e.g. minValue=10.5, maxValue=50, step=2] |
| Lock | | | | A Lock Mechanism |
| Lock | | | | A Lock Mechanism. with flag [inverted="true"] the default mapping to switch ON/OFF can be inverted. |
| | LockCurrentState | | Switch, Number | current state of lock mechanism (1/ON=SECURED, 0/OFF=UNSECURED, 2=JAMMED, 3=UNKNOWN) |
| | LockTargetState | | Switch | target state of lock mechanism (ON=SECURED, OFF=UNSECURED) |
| | | Name | String | Name of the lock |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,16 @@
*
*/
public class HomekitLockImpl extends AbstractHomekitAccessoryImpl implements LockMechanismAccessory {
final OnOffType securedState;
final OnOffType unsecuredState;

public HomekitLockImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
HomekitAccessoryUpdater updater, HomekitSettings settings) {
super(taggedItem, mandatoryCharacteristics, updater, settings);
final String invertedConfig = getAccessoryConfiguration(HomekitTaggedItem.INVERTED, "false");
final boolean inverted = invertedConfig.equalsIgnoreCase("yes") || invertedConfig.equalsIgnoreCase("true");
securedState = inverted ? OnOffType.OFF : OnOffType.ON;
unsecuredState = inverted ? OnOffType.ON : OnOffType.OFF;
getServices().add(new LockMechanismService(this));
}

Expand All @@ -56,7 +62,7 @@ public CompletableFuture<LockCurrentStateEnum> getLockCurrentState() {
if (state instanceof DecimalType) {
lockState = LockCurrentStateEnum.fromCode(((DecimalType) state).intValue());
} else if (state instanceof OnOffType) {
lockState = state == OnOffType.ON ? LockCurrentStateEnum.SECURED : LockCurrentStateEnum.UNSECURED;
lockState = state == securedState? LockCurrentStateEnum.SECURED : LockCurrentStateEnum.UNSECURED;
}
}
return CompletableFuture.completedFuture(lockState);
Expand All @@ -67,7 +73,7 @@ public CompletableFuture<LockTargetStateEnum> getLockTargetState() {
final @Nullable OnOffType state = getStateAs(HomekitCharacteristicType.LOCK_TARGET_STATE, OnOffType.class);
if (state != null) {
return CompletableFuture.completedFuture(
state == OnOffType.ON ? LockTargetStateEnum.SECURED : LockTargetStateEnum.UNSECURED);
state == securedState ? LockTargetStateEnum.SECURED : LockTargetStateEnum.UNSECURED);
}
return CompletableFuture.completedFuture(LockTargetStateEnum.UNSECURED);
// Apple HAP specification has only SECURED and UNSECURED values for lock target state.
Expand All @@ -79,15 +85,13 @@ public CompletableFuture<Void> setLockTargetState(LockTargetStateEnum state) {
getItem(HomekitCharacteristicType.LOCK_TARGET_STATE, SwitchItem.class).ifPresent(item -> {
switch (state) {
case SECURED:
// Close the door
if (item instanceof SwitchItem) {
item.send(OnOffType.ON);
item.send(securedState);
}
break;
case UNSECURED:
// Open the door
if (item instanceof SwitchItem) {
item.send(OnOffType.OFF);
item.send(unsecuredState);
}
break;
default:
Expand Down