- Carbon Dioxide Sensor
- Carbon Monoxide Sensor
+## Quick start
+
+- install homekit binding via UI
+
+- add metadata to an existing item (see [UI based configuration](#UI-based-Configuration))
+
+- go to scan QR code from UI->Setting-HomeKit Integration
+
+ 
+
+- open home app on your iPhone or iPad
+- create new home
+
+ 
+
+- add accessory
+
+ 
+
+- scan QR code from UI->Setting-HomeKit Integration
+
+ 
+
+- click "Add Anyway"
+
+ 
+
+- follow the instruction of the home app wizard
+
+ 
+
+Add metadata to more item or fine-tune your configuration using further settings
+
+
## Global Configuration
Your first step will be to create the `homekit.cfg` in your `$OPENHAB_CONF/services` folder.
A HomeKit accessory has mandatory and optional characteristics (listed below in the table).
The mapping between openHAB items and HomeKit accessory and characteristics is done by means of [metadata](https://www.openhab.org/docs/concepts/items.html#item-metadata)
-e.g.
+### UI based Configuration
+In order to add metadata to an item:
+- select desired item in mainUI
+- click on "Add Metadata"
+
+ 
+
+- select "Apple HomeKit" namespace
+
+ 
+
+- click on "HomeKit Accessory/Characteristic"
+
+ 
+
+- select required HomeKit accessory type or characteristic
+
+ 
+
+- click on "Save"
+
+
+### Textual configuration
```xtend
Switch leaksensor_metadata "Leak Sensor" {homekit="LeakSensor"}
```
import java.net.InetAddress;
import java.security.InvalidAlgorithmParameterException;
import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import org.openhab.io.homekit.Homekit;
import org.osgi.framework.Constants;
import org.osgi.framework.FrameworkUtil;
+import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import io.github.hapjava.accessories.HomekitAccessory;
import io.github.hapjava.server.impl.HomekitRoot;
import io.github.hapjava.server.impl.HomekitServer;
+import io.github.hapjava.server.impl.crypto.HAPSetupCodeUtils;
/**
* Provides access to openHAB items via the HomeKit API
*
* @author Andy Lintner - Initial contribution
*/
-@Component(service = { Homekit.class }, configurationPid = "org.openhab.homekit", property = {
+@Component(service = { Homekit.class }, configurationPid = HomekitSettings.CONFIG_PID, property = {
Constants.SERVICE_PID + "=org.openhab.homekit", "port:Integer=9123" })
@ConfigurableService(category = "io", label = "HomeKit Integration", description_uri = "io:homekit")
@NonNullByDefault
public class HomekitImpl implements Homekit {
private final Logger logger = LoggerFactory.getLogger(HomekitImpl.class);
+
private final NetworkAddressService networkAddressService;
- private final HomekitChangeListener changeListener;
+ private final ConfigurationAdmin configAdmin;
+ private HomekitAuthInfoImpl authInfo;
private HomekitSettings settings;
private @Nullable InetAddress networkInterface;
private @Nullable HomekitServer homekitServer;
private @Nullable HomekitRoot bridge;
- private final HomekitAuthInfoImpl authInfo;
+ 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, Map<String, Object> config,
- @Reference MetadataRegistry metadataRegistry) throws IOException, InvalidAlgorithmParameterException {
+ @Reference NetworkAddressService networkAddressService, @Reference MetadataRegistry metadataRegistry,
+ @Reference ConfigurationAdmin configAdmin, Map<String, Object> properties)
+ throws IOException, InvalidAlgorithmParameterException {
this.networkAddressService = networkAddressService;
- this.settings = processConfig(config);
+ this.configAdmin = configAdmin;
+ this.settings = processConfig(properties);
this.changeListener = new HomekitChangeListener(itemRegistry, settings, metadataRegistry, storageService);
- authInfo = new HomekitAuthInfoImpl(storageService.getStorage(HomekitAuthInfoImpl.STORAGE_KEY), settings.pin);
- startHomekitServer();
+ try {
+ authInfo = new HomekitAuthInfoImpl(storageService.getStorage(HomekitAuthInfoImpl.STORAGE_KEY), settings.pin,
+ settings.setupId);
+ startHomekitServer();
+ } catch (IOException | InvalidAlgorithmParameterException e) {
+ logger.warn("Cannot activate HomeKit binding. {}", e.getMessage());
+ throw e;
+ }
}
- private HomekitSettings processConfig(Map<String, Object> config) {
- HomekitSettings settings = (new Configuration(config)).as(HomekitSettings.class);
+ private HomekitSettings processConfig(Map<String, Object> properties) {
+ HomekitSettings settings = (new Configuration(properties)).as(HomekitSettings.class);
+ org.osgi.service.cm.Configuration config = null;
+ Dictionary<String, Object> props = null;
+ try {
+ config = configAdmin.getConfiguration(HomekitSettings.CONFIG_PID);
+ props = config.getProperties();
+ } catch (IOException e) {
+ logger.warn("Cannot retrieve config admin {}", e.getMessage());
+ }
+
+ 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);
+ }
+
+ // QR Code setup URI is always generated from PIN, setup ID and accessory category (1 = bridge)
+ String setupURI = HAPSetupCodeUtils.getSetupURI(settings.pin.replaceAll("-", ""), settings.setupId, 1);
+ if ((settings.qrCode == null) || (!settings.qrCode.equals(setupURI))) { // QR code was changed
+ settings.qrCode = setupURI;
+ props.put("qrCode", settings.qrCode);
+ }
+
+ if (config != null) {
+ try {
+ config.updateIfDifferent(props);
+ } catch (IOException e) {
+ logger.warn("Cannot update configuration {}", e.getMessage());
+ }
}
return settings;
}
// 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)) {
- // we change the root bridge only
- stopBridge();
- startBridge();
+ } else if (!oldSettings.name.equals(settings.name) || !oldSettings.pin.equals(settings.pin)
+ || !oldSettings.setupId.equals(settings.setupId)) {
+ stopHomekitServer();
+ authInfo.setPin(settings.pin);
+ authInfo.setSetupId(settings.setupId);
+ startHomekitServer();
}
} catch (IOException e) {
logger.warn("Could not initialize HomeKit connector: {}", e.getMessage());