-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from timcharper/tharper/new-devices
OccupancySensor support
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
src/main/java/com/beowulfe/hap/accessories/OccupancySensor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
38 changes: 38 additions & 0 deletions
38
...owulfe/hap/impl/characteristics/occupancysensor/OccupancyDetectedStateCharacteristic.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/beowulfe/hap/impl/services/OccupancySensorService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |