Skip to content

Commit

Permalink
[homekit] add setting to block homekit user/pairing deletion (openhab…
Browse files Browse the repository at this point in the history
…#11731)

* add setting to block homekit user deletion and unpairing
* add logging
* remove . from settings label

Signed-off-by: Eugen Freiter <freiter@gmx.de>
  • Loading branch information
yfre committed Dec 8, 2021
1 parent 29bdba4 commit 6257c40
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
2 changes: 2 additions & 0 deletions bundles/org.openhab.io.homekit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ org.openhab.homekit:thermostatTargetModeAuto=Auto
org.openhab.homekit:thermostatTargetModeOff=Off
org.openhab.homekit:networkInterface=192.168.0.6
org.openhab.homekit:useOHmDNS=false
org.openhab.homekit:blockUserDeletion=false
org.openhab.homekit:name=openHAB
```

Expand All @@ -98,6 +99,7 @@ org.openhab.homekit:name=openHAB
| networkInterface | IP address or domain name under which the HomeKit bridge can be reached. If no value is configured, the add-on uses the first network adapter address configured for openHAB. | (none) |
| port | Port under which the HomeKit bridge can be reached. | 9123 |
| useOHmDNS | mDNS service is used to advertise openHAB as HomeKit bridge in the network so that HomeKit clients can find it. openHAB has already mDNS service running. This option defines whether the mDNS service of openHAB or a separate service should be used. | false |
| blockUserDeletion | Blocks HomeKit user deletion in openHAB and as result unpairing of devices. If you experience an issue with accessories becoming non-responsive after some time, try to enable this setting. You can also enable this setting if your HomeKit setup is done and you will not re-pair ios devices. | false |
| pin | Pin code used for pairing with iOS devices. Apparently, pin codes are provided by Apple and represent specific device types, so they cannot be chosen freely. The pin code 031-45-154 is used in sample applications and known to work. | 031-45-154 |
| startDelay | HomeKit start delay in seconds in case the number of accessories is lower than last time. This helps to avoid resetting home app in case not all items have been initialised properly before HomeKit integration start. | 30 |
| useFahrenheitTemperature | Set to true to use Fahrenheit degrees, or false to use Celsius degrees. | false |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,24 @@ public class HomekitAuthInfoImpl implements HomekitAuthInfo {
private byte[] privateKey;
private String pin;
private String setupId;
private boolean blockUserDeletion;

public HomekitAuthInfoImpl(Storage<String> storage, String pin, String setupId)
public HomekitAuthInfoImpl(Storage<String> storage, String pin, String setupId, boolean blockUserDeletion)
throws InvalidAlgorithmParameterException {
this.storage = storage;
this.pin = pin;
this.setupId = setupId;
this.blockUserDeletion = blockUserDeletion;
initializeStorage();
}

@Override
public void createUser(String username, byte[] publicKey) {
logger.trace("Create user {}", username);
logger.trace("create user {}", username);
final String userKey = createUserKey(username);
final String encodedPublicKey = Base64.getEncoder().encodeToString(publicKey);
storage.put(userKey, encodedPublicKey);
logger.trace("Stored user key {} with value {}", userKey, encodedPublicKey);
logger.trace("stored user key {} with value {}", userKey, encodedPublicKey);
}

@Override
Expand Down Expand Up @@ -113,8 +115,12 @@ public byte[] getUserPublicKey(String username) {

@Override
public void removeUser(String username) {
logger.trace("Remove user {}", username);
storage.remove(createUserKey(username));
logger.trace("remove user {}", username);
if (!this.blockUserDeletion) {
storage.remove(createUserKey(username));
} else {
logger.debug("deletion of the user was blocked by binding settings");
}
}

@Override
Expand All @@ -124,11 +130,15 @@ public boolean hasUser() {
}

public void clear() {
logger.trace("Clear all users");
for (String key : new HashSet<>(storage.getKeys())) {
if (isUserKey(key)) {
storage.remove(key);
logger.trace("clear all users");
if (!this.blockUserDeletion) {
for (String key : new HashSet<>(storage.getKeys())) {
if (isUserKey(key)) {
storage.remove(key);
}
}
} else {
logger.debug("deletion of users information was blocked by binding settings");
}
}

Expand All @@ -146,7 +156,7 @@ private void initializeStorage() throws InvalidAlgorithmParameterException {
final @Nullable Object privateKeyConfig = storage.get(STORAGE_PRIVATE_KEY);
if (mac == null) {
logger.warn(
"Could not find existing MAC in {}. Generating new MAC. This will require re-pairing of iOS devices.",
"could not find existing MAC in {}. Generating new MAC. This will require re-pairing of iOS devices.",
storage.getClass().getName());
mac = HomekitServer.generateMac();
storage.put(STORAGE_MAC, mac);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public HomekitImpl(@Reference StorageService storageService, @Reference ItemRegi
this.changeListener = new HomekitChangeListener(itemRegistry, settings, metadataRegistry, storageService);
try {
authInfo = new HomekitAuthInfoImpl(storageService.getStorage(HomekitAuthInfoImpl.STORAGE_KEY), settings.pin,
settings.setupId);
settings.setupId, settings.blockUserDeletion);
startHomekitServer();
} catch (IOException | InvalidAlgorithmParameterException e) {
logger.warn("cannot activate HomeKit binding. {}", e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class HomekitSettings {
public int startDelay = 30;
public boolean useFahrenheitTemperature = false;
public boolean useOHmDNS = false;
public boolean blockUserDeletion = false;
public String thermostatTargetModeHeat = "HeatOn";
public String thermostatTargetModeCool = "CoolOn";
public String thermostatTargetModeAuto = "Auto";
Expand Down Expand Up @@ -81,6 +82,8 @@ public boolean equals(Object obj) {
}
} else if (!useOHmDNS != other.useOHmDNS) {
return false;
} else if (!blockUserDeletion != other.blockUserDeletion) {
return false;
} else if (!pin.equals(other.pin)) {
return false;
} else if (!setupId.equals(other.setupId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,10 @@
<description>Defines whether mDNS service of openHAB or a separate instance of mDNS should be used.</description>
<default>false</default>
</parameter>
<parameter name="blockUserDeletion" type="boolean" required="false" groupName="network">
<label>Block deletion of the HomeKit user</label>
<description>Block deletion of the HomeKit user information from openHAB and the unpairing of devices</description>
<default>false</default>
</parameter>
</config-description>
</config-description:config-descriptions>

0 comments on commit 6257c40

Please sign in to comment.