Skip to content

OccupancySensor support #59

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

Merged
merged 1 commit into from
Feb 3, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
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));
}
}