Skip to content

Add window support #103

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

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
* Occupancy sensor support [#59](https://github.com/hap-java/HAP-Java/pull/59)
* Leak sensors and valve support added [#52](https://github.com/hap-java/HAP-Java/pull/52)
* Notifications are batched now, when possible [#66](https://github.com/hap-java/HAP-Java/pull/66)
* Support for Doors and Windows added
Original file line number Diff line number Diff line change
@@ -1,78 +1,5 @@
package io.github.hapjava.accessories;

import io.github.hapjava.HomekitAccessory;
import io.github.hapjava.HomekitCharacteristicChangeCallback;
import io.github.hapjava.Service;
import io.github.hapjava.accessories.properties.WindowCoveringPositionState;
import io.github.hapjava.impl.services.WindowCoveringService;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;
public interface BasicWindowCovering extends Positionable {

public interface BasicWindowCovering extends HomekitAccessory {

/**
* Retrieves the current position
*
* @return a future that will contain the position as a value between 0 and 100
*/
CompletableFuture<Integer> getCurrentPosition();

/**
* Retrieves the target position
*
* @return a future that will contain the target position as a value between 0 and 100
*/
CompletableFuture<Integer> getTargetPosition();

/**
* Retrieves the state of the position: increasing, decreasing, or stopped
*
* @return a future that will contain the current state
*/
CompletableFuture<WindowCoveringPositionState> getPositionState();

/**
* Sets the target position
*
* @param position the target position to set, as a value between 1 and 100
* @return a future that completes when the change is made
* @throws Exception when the change cannot be made
*/
CompletableFuture<Void> setTargetPosition(int position) throws Exception;

/**
* Subscribes to changes in the current position.
*
* @param callback the function to call when the state changes.
*/
void subscribeCurrentPosition(HomekitCharacteristicChangeCallback callback);

/**
* Subscribes to changes in the target position.
*
* @param callback the function to call when the state changes.
*/
void subscribeTargetPosition(HomekitCharacteristicChangeCallback callback);

/**
* Subscribes to changes in the position state: increasing, decreasing, or stopped
*
* @param callback the function to call when the state changes.
*/
void subscribePositionState(HomekitCharacteristicChangeCallback callback);

/** Unsubscribes from changes in the current position. */
void unsubscribeCurrentPosition();

/** Unsubscribes from changes in the target position. */
void unsubscribeTargetPosition();

/** Unsubscribes from changes in the position state */
void unsubscribePositionState();

@Override
default Collection<Service> getServices() {
return Collections.singleton(new WindowCoveringService(this));
}
}
10 changes: 10 additions & 0 deletions src/main/java/io/github/hapjava/accessories/Door.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.github.hapjava.accessories;

/**
*
* @author Benjamin Lafois
*
*/
public interface Door extends Positionable {

}
79 changes: 79 additions & 0 deletions src/main/java/io/github/hapjava/accessories/Positionable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package io.github.hapjava.accessories;

import java.util.concurrent.CompletableFuture;

import io.github.hapjava.HomekitAccessory;
import io.github.hapjava.HomekitCharacteristicChangeCallback;
import io.github.hapjava.accessories.properties.PositionablePositionState;

/**
*
* @author Benjamin Lafois
*
* Abstraction interface to allow implementation of other positionable items such as Window, Doors, and not only
* Coverings
*
*/
public interface Positionable extends HomekitAccessory {

/**
* Retrieves the current position
*
* @return a future that will contain the position as a value between 0 and 100
*/
CompletableFuture<Integer> getCurrentPosition();

/**
* Retrieves the target position
*
* @return a future that will contain the target position as a value between 0 and 100
*/
CompletableFuture<Integer> getTargetPosition();

/**
* Retrieves the state of the position: increasing, decreasing, or stopped
*
* @return a future that will contain the current state
*/
CompletableFuture<PositionablePositionState> getPositionState();

/**
* Sets the target position
*
* @param position the target position to set, as a value between 1 and 100
* @return a future that completes when the change is made
* @throws Exception when the change cannot be made
*/
CompletableFuture<Void> setTargetPosition(int position) throws Exception;

/**
* Subscribes to changes in the current position.
*
* @param callback the function to call when the state changes.
*/
void subscribeCurrentPosition(HomekitCharacteristicChangeCallback callback);

/**
* Subscribes to changes in the target position.
*
* @param callback the function to call when the state changes.
*/
void subscribeTargetPosition(HomekitCharacteristicChangeCallback callback);

/**
* Subscribes to changes in the position state: increasing, decreasing, or stopped
*
* @param callback the function to call when the state changes.
*/
void subscribePositionState(HomekitCharacteristicChangeCallback callback);

/** Unsubscribes from changes in the current position. */
void unsubscribeCurrentPosition();

/** Unsubscribes from changes in the target position. */
void unsubscribeTargetPosition();

/** Unsubscribes from changes in the position state */
void unsubscribePositionState();

}
11 changes: 11 additions & 0 deletions src/main/java/io/github/hapjava/accessories/Window.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.hapjava.accessories;

/**
*
* @author Benjamin Lafois
* HomeKit Window
*
*/
public interface Window extends Positionable {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.github.hapjava.accessories.properties;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

import io.github.hapjava.accessories.Door;
import io.github.hapjava.accessories.WindowCovering;

/**
* The position state used by a {@link WindowCovering}, {@link Door} or {@link Window}
*
* @author Andy Lintner
*/
public enum PositionablePositionState {
DECREASING(0),
INCREASING(1),
STOPPED(2);

private static final Map<Integer, PositionablePositionState> reverse;

static {
reverse = Arrays.stream(PositionablePositionState.values()).collect(Collectors.toMap(t -> t.getCode(), t -> t));
}

public static PositionablePositionState fromCode(Integer code) {
return reverse.get(code);
}

private final int code;

private PositionablePositionState(int code) {
this.code = code;
}

public int getCode() {
return code;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
package io.github.hapjava.impl.characteristics.windowcovering;

import java.util.concurrent.CompletableFuture;

import io.github.hapjava.HomekitCharacteristicChangeCallback;
import io.github.hapjava.accessories.BasicWindowCovering;
import io.github.hapjava.accessories.Positionable;
import io.github.hapjava.characteristics.EventableCharacteristic;
import io.github.hapjava.characteristics.IntegerCharacteristic;
import java.util.concurrent.CompletableFuture;

public class CurrentPositionCharacteristic extends IntegerCharacteristic
implements EventableCharacteristic {
public class CurrentPositionCharacteristic extends IntegerCharacteristic implements EventableCharacteristic {

private final BasicWindowCovering windowCovering;
private final Positionable positionable;

public CurrentPositionCharacteristic(BasicWindowCovering windowCovering) {
super("0000006D-0000-1000-8000-0026BB765291", false, true, "The current position", 0, 100, "%");
this.windowCovering = windowCovering;
}
public CurrentPositionCharacteristic(Positionable positionable) {
super("0000006D-0000-1000-8000-0026BB765291", false, true, "The current position", 0, 100, "%");
this.positionable = positionable;
}

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

@Override
protected CompletableFuture<Integer> getValue() {
return windowCovering.getCurrentPosition();
}
@Override
protected CompletableFuture<Integer> getValue() {
return positionable.getCurrentPosition();
}

@Override
public void subscribe(HomekitCharacteristicChangeCallback callback) {
windowCovering.subscribeCurrentPosition(callback);
}
@Override
public void subscribe(HomekitCharacteristicChangeCallback callback) {
positionable.subscribeCurrentPosition(callback);
}

@Override
public void unsubscribe() {
windowCovering.unsubscribeCurrentPosition();
}
@Override
public void unsubscribe() {
positionable.unsubscribeCurrentPosition();
}
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
package io.github.hapjava.impl.characteristics.windowcovering;

import java.util.concurrent.CompletableFuture;

import io.github.hapjava.HomekitCharacteristicChangeCallback;
import io.github.hapjava.accessories.BasicWindowCovering;
import io.github.hapjava.accessories.Positionable;
import io.github.hapjava.characteristics.EnumCharacteristic;
import io.github.hapjava.characteristics.EventableCharacteristic;
import java.util.concurrent.CompletableFuture;

public class PositionStateCharacteristic extends EnumCharacteristic
implements EventableCharacteristic {
public class PositionStateCharacteristic extends EnumCharacteristic implements EventableCharacteristic {

private final BasicWindowCovering windowCovering;
private final Positionable positionable;

public PositionStateCharacteristic(BasicWindowCovering windowCovering) {
super("00000072-0000-1000-8000-0026BB765291", false, true, "The position state", 2);
this.windowCovering = windowCovering;
}
public PositionStateCharacteristic(Positionable positionable) {
super("00000072-0000-1000-8000-0026BB765291", false, true, "The position state", 2);
this.positionable = positionable;
}

@Override
protected void setValue(Integer value) throws Exception {
// Read only
}
@Override
protected void setValue(Integer value) throws Exception {
// Read only
}

@Override
protected CompletableFuture<Integer> getValue() {
return windowCovering.getPositionState().thenApply(v -> v.getCode());
}
@Override
protected CompletableFuture<Integer> getValue() {
return positionable.getPositionState().thenApply(v -> v.getCode());
}

@Override
public void subscribe(HomekitCharacteristicChangeCallback callback) {
windowCovering.subscribePositionState(callback);
}
@Override
public void subscribe(HomekitCharacteristicChangeCallback callback) {
positionable.subscribePositionState(callback);
}

@Override
public void unsubscribe() {
windowCovering.unsubscribePositionState();
}
@Override
public void unsubscribe() {
positionable.unsubscribePositionState();
}
}
Loading