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] make use of openHAB mDNS service #10076

Merged
merged 5 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 0 additions & 12 deletions bundles/org.openhab.io.homekit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ org.openhab.homekit:thermostatTargetModeCool=CoolOn
org.openhab.homekit:thermostatTargetModeHeat=HeatOn
org.openhab.homekit:thermostatTargetModeAuto=Auto
org.openhab.homekit:thermostatTargetModeOff=Off
org.openhab.homekit:networkInterface=192.168.0.6
```

The following additional settings can be added or edited in Paper UI after switching to expert mode:
Expand All @@ -103,7 +102,6 @@ org.openhab.homekit:maximumTemperature=100

| Setting | Description | Default value |
|:-------------------------|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:--------------|
| 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. | (none) |
| port | Port under which the HomeKit bridge can be reached. | 9123 |
| 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 |
Expand Down Expand Up @@ -712,16 +710,6 @@ String cooler_target_mode "Cooler Target Mode" (gCooler)
Number cooler_cool_thrs "Cooler Cool Threshold Temp [%.1f C]" (gCooler) {homekit="CoolingThresholdTemperature" [minValue=10.5, maxValue=50]}
Number cooler_heat_thrs "Cooler Heat Threshold Temp [%.1f C]" (gCooler) {homekit="HeatingThresholdTemperature" [minValue=0.5, maxValue=20]}
```


## Common Problems

**openHAB HomeKit hub shows up when I manually scan for devices, but Home app reports "can't connect to device"**

If you see this error in the Home app, and don't see any log messages, it could be because your IP address in the `networkInterface` setting is misconfigured.
The openHAB HomeKit hub is advertised via mDNS.
If you register an IP address that isn't reachable from your phone (such as `localhost`, `0.0.0.0`, `127.0.0.1`, etc.), then Home will be unable to reach openHAB.

## Additional Notes

HomeKit allows only a single pairing to be established with the bridge.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
package org.openhab.io.homekit.internal;

import java.io.IOException;
import java.net.InetAddress;
import java.security.InvalidAlgorithmParameterException;
import java.util.ArrayList;
import java.util.Dictionary;
Expand All @@ -28,9 +27,9 @@
import org.openhab.core.common.ThreadPoolManager;
import org.openhab.core.config.core.ConfigurableService;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.io.transport.mdns.MDNSClient;
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.items.MetadataRegistry;
import org.openhab.core.net.NetworkAddressService;
import org.openhab.core.storage.StorageService;
import org.openhab.io.homekit.Homekit;
import org.osgi.framework.Constants;
Expand Down Expand Up @@ -61,27 +60,27 @@
public class HomekitImpl implements Homekit {
private final Logger logger = LoggerFactory.getLogger(HomekitImpl.class);

private final NetworkAddressService networkAddressService;
private final ConfigurationAdmin configAdmin;

private HomekitAuthInfoImpl authInfo;
private HomekitSettings settings;
private @Nullable InetAddress networkInterface;
private @Nullable HomekitServer homekitServer;
private @Nullable HomekitRoot bridge;
private MDNSClient mdnsClient;

private final HomekitChangeListener changeListener;

private final ScheduledExecutorService scheduler = ThreadPoolManager
.getScheduledPool(ThreadPoolManager.THREAD_POOL_NAME_COMMON);

@Activate
public HomekitImpl(@Reference StorageService storageService, @Reference ItemRegistry itemRegistry,
@Reference NetworkAddressService networkAddressService, @Reference MetadataRegistry metadataRegistry,
@Reference ConfigurationAdmin configAdmin, Map<String, Object> properties)
@Reference MetadataRegistry metadataRegistry, @Reference ConfigurationAdmin configAdmin,
@Reference MDNSClient mdnsClient, Map<String, Object> properties)
throws IOException, InvalidAlgorithmParameterException {
this.networkAddressService = networkAddressService;
this.configAdmin = configAdmin;
this.settings = processConfig(properties);
this.mdnsClient = mdnsClient;
this.changeListener = new HomekitChangeListener(itemRegistry, settings, metadataRegistry, storageService);
try {
authInfo = new HomekitAuthInfoImpl(storageService.getStorage(HomekitAuthInfoImpl.STORAGE_KEY), settings.pin,
Expand All @@ -107,10 +106,6 @@ private HomekitSettings processConfig(Map<String, Object> properties) {
if (props == null) { // if null, the configuration is new
props = new Hashtable<>();
}
if (settings.networkInterface == null) {
settings.networkInterface = networkAddressService.getPrimaryIpv4HostAddress();
props.put("networkInterface", settings.networkInterface);
}
if (settings.setupId == null) { // generate setupId very first time
settings.setupId = HAPSetupCodeUtils.generateSetupId();
props.put("setupId", settings.setupId);
Expand Down Expand Up @@ -139,11 +134,7 @@ protected synchronized void modified(Map<String, Object> config) {
HomekitSettings oldSettings = settings;
settings = processConfig(config);
changeListener.updateSettings(settings);
if (!oldSettings.networkInterface.equals(settings.networkInterface) || oldSettings.port != settings.port) {
// the HomeKit server settings changed. we do a complete re-init
stopHomekitServer();
startHomekitServer();
} else if (!oldSettings.name.equals(settings.name) || !oldSettings.pin.equals(settings.pin)
if (!oldSettings.name.equals(settings.name) || !oldSettings.pin.equals(settings.pin)
|| !oldSettings.setupId.equals(settings.setupId)) {
stopHomekitServer();
authInfo.setPin(settings.pin);
Expand Down Expand Up @@ -199,9 +190,12 @@ private void startBridge() throws IOException {

private void startHomekitServer() throws IOException {
if (homekitServer == null) {
networkInterface = InetAddress.getByName(settings.networkInterface);
homekitServer = new HomekitServer(networkInterface, settings.port);
startBridge();
if (!mdnsClient.getClientInstances().isEmpty()) {
homekitServer = new HomekitServer(mdnsClient.getClientInstances().iterator().next(), settings.port);
yfre marked this conversation as resolved.
Show resolved Hide resolved
startBridge();
} else {
logger.warn("openHAB MDNS service not found. HomeKit cannot be initialised.");
}
} else {
logger.warn("trying to start HomeKit server but it is already initialized");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ public class HomekitSettings {
public String qrCode;
public int startDelay = 30;
public boolean useFahrenheitTemperature = false;
public double minimumTemperature = -100;
public double maximumTemperature = 100;
public String thermostatTargetModeHeat = "HeatOn";
public String thermostatTargetModeCool = "CoolOn";
public String thermostatTargetModeAuto = "Auto";
Expand All @@ -47,17 +45,11 @@ public class HomekitSettings {
public String doorCurrentStateStopped = "STOPPED";
public String doorTargetStateClosed = "CLOSED";
public String doorTargetStateOpen = "OPEN";
public String networkInterface;

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(maximumTemperature);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(minimumTemperature);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((pin == null) ? 0 : pin.hashCode());
result = prime * result + ((setupId == null) ? 0 : setupId.hashCode());
result = prime * result + port;
Expand All @@ -81,12 +73,6 @@ public boolean equals(Object obj) {
return false;
}
HomekitSettings other = (HomekitSettings) obj;
if (Double.doubleToLongBits(maximumTemperature) != Double.doubleToLongBits(other.maximumTemperature)) {
return false;
}
if (Double.doubleToLongBits(minimumTemperature) != Double.doubleToLongBits(other.minimumTemperature)) {
return false;
}
if (pin == null) {
if (other.pin != null) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@
<label>Setup ID</label>
<description>Setup ID used for pairing using QR Code. Alphanumeric code of length 4.</description>
</parameter>
<parameter name="networkInterface" type="text" required="false" groupName="core">
<label>Network Interface</label>
<description>Defines the IP address of the network interface to expose the HomeKit integration on.</description>
</parameter>
<parameter name="startDelay" type="integer" required="true" groupName="core" unit="s">
<label>Start Delay</label>
<description>HomeKit start delay in case of item configuration differences.</description>
Expand Down