Skip to content

Commit

Permalink
Merge pull request #59 from timcharper/tharper/new-devices
Browse files Browse the repository at this point in the history
OccupancySensor support
  • Loading branch information
andylintner authored Feb 3, 2019
2 parents 97ae5ad + dd21cc6 commit dfc2d57
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/main/java/com/beowulfe/hap/accessories/OccupancySensor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.beowulfe.hap.accessories;

import com.beowulfe.hap.HomekitAccessory;
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
import com.beowulfe.hap.Service;
import com.beowulfe.hap.impl.services.OccupancySensorService;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;

/**
* An occupancy sensor that reports whether occupancy has been detected.
*
* <p>Occupancy sensors that run on batteries will need to implement this interface and also
* implement {@link BatteryStatusAccessory}.
*
* @author Tim Harper
*/
public interface OccupancySensor extends HomekitAccessory {
/**
* Retrieves the state of the occupancy sensor. If true then occupancy has been detected.
*
* @return a future that will contain the occupancy sensor's state
*/
CompletableFuture<Boolean> getOccupancyDetected();

@Override
default Collection<Service> getServices() {
return Collections.singleton(new OccupancySensorService(this));
}

/**
* Subscribes to changes in the occupancy sensor.
*
* @param callback the function to call when the state changes.
*/
void subscribeOccupancyDetected(HomekitCharacteristicChangeCallback callback);

/** Unsubscribes from changes in the occupancy sensor. */
void unsubscribeOccupancyDetected();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.beowulfe.hap.impl.characteristics.occupancysensor;

import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
import com.beowulfe.hap.accessories.OccupancySensor;
import com.beowulfe.hap.characteristics.BooleanCharacteristic;
import com.beowulfe.hap.characteristics.EventableCharacteristic;
import java.util.concurrent.CompletableFuture;

public class OccupancyDetectedStateCharacteristic extends BooleanCharacteristic
implements EventableCharacteristic {

private final OccupancySensor occupancySensor;

public OccupancyDetectedStateCharacteristic(OccupancySensor occupancySensor) {
super("00000071-0000-1000-8000-0026BB765291", false, true, "Occupancy Detected");
this.occupancySensor = occupancySensor;
}

@Override
protected CompletableFuture<Boolean> getValue() {
return occupancySensor.getOccupancyDetected();
}

@Override
protected void setValue(Boolean value) throws Exception {
// Read Only
}

@Override
public void subscribe(HomekitCharacteristicChangeCallback callback) {
occupancySensor.subscribeOccupancyDetected(callback);
}

@Override
public void unsubscribe() {
occupancySensor.unsubscribeOccupancyDetected();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.beowulfe.hap.impl.services;

import com.beowulfe.hap.accessories.OccupancySensor;
import com.beowulfe.hap.impl.characteristics.occupancysensor.OccupancyDetectedStateCharacteristic;

public class OccupancySensorService extends AbstractServiceImpl {

public OccupancySensorService(OccupancySensor occupancySensor) {
this(occupancySensor, occupancySensor.getLabel());
}

public OccupancySensorService(OccupancySensor occupancySensor, String serviceName) {
super("00000086-0000-1000-8000-0026BB765291", occupancySensor, serviceName);
addCharacteristic(new OccupancyDetectedStateCharacteristic(occupancySensor));
}
}

0 comments on commit dfc2d57

Please sign in to comment.