Skip to content

Commit

Permalink
[homekit] Added support for contact sensors (openhab#4521)
Browse files Browse the repository at this point in the history
* [homekit] Added support for contact sensors
* Changed SwitchItem to ContactItem
* Updated documentation for ContactItem

Signed-off-by: Philipp Arndt <[email protected]> (github: philipparndt)
Signed-off-by: Maximilian Hess <[email protected]>
  • Loading branch information
philipparndt authored and ne0h committed Sep 15, 2019
1 parent 5a136ab commit 445a14e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions addons/io/org.openhab.io.homekit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ A full list of supported accessory types can be found in the table below.
|-------------------- |---------------------------- |----------------------- |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Lighting | | Switch, Dimmer, Color | A lightbulb, switchable, dimmable or rgb |
| Switchable | | Switch, Dimmer, Color | An accessory that can be turned off and on. While similar to a lightbulb, this will be presented differently in the Siri grammar and iOS apps |
| ContactSensor | | Contact | An accessory with on/off state that can be viewed in homekit but not changed such as a contact sensor for a door or window |
| CurrentTemperature | | Number | An accessory that provides a single read-only temperature value. The units default to celsius but can be overridden globally using the useFahrenheitTemperature global property |
| CurrentHumidity | | Number | An accessory that provides a single read-only value indicating the relative humidity. |
| Thermostat | | Group | A thermostat requires all child tags defined below |
Expand Down
1 change: 1 addition & 0 deletions addons/io/org.openhab.io.homekit/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ bin.includes = META-INF/,\
lib/org.zeromq.curve25519-java-0.1.0.jar,\
NOTICE,\
ESH-INF/

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public enum HomekitDeviceType {
SWITCH("Switchable"),
TEMPERATURE_SENSOR("CurrentTemperature"),
THERMOSTAT("Thermostat"),
COLORFUL_LIGHTBULB("ColorfulLighting");
COLORFUL_LIGHTBULB("ColorfulLighting"),
CONTACT_SENSOR("ContactSensor");

private static final Map<String, HomekitDeviceType> TAG_MAP = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public static HomekitAccessory create(HomekitTaggedItem taggedItem, ItemRegistry

case HUMIDITY_SENSOR:
return new HomekitHumiditySensorImpl(taggedItem, itemRegistry, updater);

case CONTACT_SENSOR:
return new HomekitContactSensorImpl(taggedItem, itemRegistry, updater);
}

throw new IllegalArgumentException("Unknown homekit type: " + taggedItem.getDeviceType());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Copyright (c) 2010-2019 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.io.homekit.internal.accessories;

import java.util.concurrent.CompletableFuture;

import org.eclipse.smarthome.core.items.ItemRegistry;
import org.eclipse.smarthome.core.library.items.ContactItem;
import org.eclipse.smarthome.core.library.types.OpenClosedType;
import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
import org.openhab.io.homekit.internal.HomekitTaggedItem;

import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
import com.beowulfe.hap.accessories.ContactSensor;
import com.beowulfe.hap.accessories.properties.ContactState;

/**
* Implements Contact sensor using an Item that provides an On/Off state.
*
* @author Philipp Arndt - Initial contribution
*/
public class HomekitContactSensorImpl extends AbstractHomekitAccessoryImpl<ContactItem> implements ContactSensor {

public HomekitContactSensorImpl(HomekitTaggedItem taggedItem, ItemRegistry itemRegistry,
HomekitAccessoryUpdater updater) {
super(taggedItem, itemRegistry, updater, ContactItem.class);
}

@Override
public CompletableFuture<ContactState> getCurrentState() {
OpenClosedType state = getItem().getStateAs(OpenClosedType.class);
return CompletableFuture
.completedFuture(state == OpenClosedType.CLOSED ? ContactState.DETECTED : ContactState.NOT_DETECTED);
}

@Override
public void subscribeContactState(HomekitCharacteristicChangeCallback callback) {
getUpdater().subscribe(getItem(), callback);
}

@Override
public void unsubscribeContactState() {
getUpdater().unsubscribe(getItem());
}

}

0 comments on commit 445a14e

Please sign in to comment.