Skip to content

Commit dfc2d57

Browse files
authored
Merge pull request #59 from timcharper/tharper/new-devices
OccupancySensor support
2 parents 97ae5ad + dd21cc6 commit dfc2d57

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.beowulfe.hap.accessories;
2+
3+
import com.beowulfe.hap.HomekitAccessory;
4+
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
5+
import com.beowulfe.hap.Service;
6+
import com.beowulfe.hap.impl.services.OccupancySensorService;
7+
import java.util.Collection;
8+
import java.util.Collections;
9+
import java.util.concurrent.CompletableFuture;
10+
11+
/**
12+
* An occupancy sensor that reports whether occupancy has been detected.
13+
*
14+
* <p>Occupancy sensors that run on batteries will need to implement this interface and also
15+
* implement {@link BatteryStatusAccessory}.
16+
*
17+
* @author Tim Harper
18+
*/
19+
public interface OccupancySensor extends HomekitAccessory {
20+
/**
21+
* Retrieves the state of the occupancy sensor. If true then occupancy has been detected.
22+
*
23+
* @return a future that will contain the occupancy sensor's state
24+
*/
25+
CompletableFuture<Boolean> getOccupancyDetected();
26+
27+
@Override
28+
default Collection<Service> getServices() {
29+
return Collections.singleton(new OccupancySensorService(this));
30+
}
31+
32+
/**
33+
* Subscribes to changes in the occupancy sensor.
34+
*
35+
* @param callback the function to call when the state changes.
36+
*/
37+
void subscribeOccupancyDetected(HomekitCharacteristicChangeCallback callback);
38+
39+
/** Unsubscribes from changes in the occupancy sensor. */
40+
void unsubscribeOccupancyDetected();
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.beowulfe.hap.impl.characteristics.occupancysensor;
2+
3+
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
4+
import com.beowulfe.hap.accessories.OccupancySensor;
5+
import com.beowulfe.hap.characteristics.BooleanCharacteristic;
6+
import com.beowulfe.hap.characteristics.EventableCharacteristic;
7+
import java.util.concurrent.CompletableFuture;
8+
9+
public class OccupancyDetectedStateCharacteristic extends BooleanCharacteristic
10+
implements EventableCharacteristic {
11+
12+
private final OccupancySensor occupancySensor;
13+
14+
public OccupancyDetectedStateCharacteristic(OccupancySensor occupancySensor) {
15+
super("00000071-0000-1000-8000-0026BB765291", false, true, "Occupancy Detected");
16+
this.occupancySensor = occupancySensor;
17+
}
18+
19+
@Override
20+
protected CompletableFuture<Boolean> getValue() {
21+
return occupancySensor.getOccupancyDetected();
22+
}
23+
24+
@Override
25+
protected void setValue(Boolean value) throws Exception {
26+
// Read Only
27+
}
28+
29+
@Override
30+
public void subscribe(HomekitCharacteristicChangeCallback callback) {
31+
occupancySensor.subscribeOccupancyDetected(callback);
32+
}
33+
34+
@Override
35+
public void unsubscribe() {
36+
occupancySensor.unsubscribeOccupancyDetected();
37+
}
38+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.beowulfe.hap.impl.services;
2+
3+
import com.beowulfe.hap.accessories.OccupancySensor;
4+
import com.beowulfe.hap.impl.characteristics.occupancysensor.OccupancyDetectedStateCharacteristic;
5+
6+
public class OccupancySensorService extends AbstractServiceImpl {
7+
8+
public OccupancySensorService(OccupancySensor occupancySensor) {
9+
this(occupancySensor, occupancySensor.getLabel());
10+
}
11+
12+
public OccupancySensorService(OccupancySensor occupancySensor, String serviceName) {
13+
super("00000086-0000-1000-8000-0026BB765291", occupancySensor, serviceName);
14+
addCharacteristic(new OccupancyDetectedStateCharacteristic(occupancySensor));
15+
}
16+
}

0 commit comments

Comments
 (0)