Skip to content
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
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# HAP-Java 2.0.9
* Fix Set Duration and Remaining Duration characteristics to advertise format `uint32` instead of `int` [#202](https://github.com/hap-java/HAP-Java/issues/202)

# HAP-Java 2.0.8
* Updated bouncy castle to 1.83
* Updated netty to 4.1.126.Final
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,55 @@ public IntegerCharacteristic(
Optional<ExceptionalConsumer<Integer>> setter,
Optional<Consumer<HomekitCharacteristicChangeCallback>> subscriber,
Optional<Runnable> unsubscriber) {
this(
type,
"int",
description,
minValue,
maxValue,
unit,
getter,
setter,
subscriber,
unsubscriber);
}

/**
* Constructor for characteristics whose HAP format differs from the default "int", e.g. "uint8",
* "uint16" or "uint32". Values are still handled as Java {@link Integer}, so the supported range
* must fit into a signed 32-bit integer.
*
* @param type a string containing a UUID that indicates the type of characteristic. Apple defines
* a set of these, however implementors can create their own as well.
* @param format the HAP format to advertise, e.g. "int" or "uint32".
* @param description a description of the characteristic to be passed to the consuming device.
* @param minValue the minimum supported value.
* @param maxValue the maximum supported value
* @param unit a description of the unit this characteristic supports.
* @param getter getter for value
* @param setter setter for value
* @param subscriber subscribers to changes
* @param unsubscriber unsubscribers to changes
*/
protected IntegerCharacteristic(
String type,
String format,
String description,
int minValue,
int maxValue,
String unit,
Optional<Supplier<CompletableFuture<Integer>>> getter,
Optional<ExceptionalConsumer<Integer>> setter,
Optional<Consumer<HomekitCharacteristicChangeCallback>> subscriber,
Optional<Runnable> unsubscriber) {
super(
type, "int", description, getter.isPresent(), setter.isPresent(), subscriber, unsubscriber);
type,
format,
description,
getter.isPresent(),
setter.isPresent(),
subscriber,
unsubscriber);
this.minValue = minValue;
this.maxValue = maxValue;
this.unit = unit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public RemainingDurationCharacteristic(
Runnable unsubscriber) {
super(
"000000D4-0000-1000-8000-0026BB765291",
"uint32",
"remaining duration",
minValue,
maxValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public SetDurationCharacteristic(
Runnable unsubscriber) {
super(
"000000D3-0000-1000-8000-0026BB765291",
"uint32",
"set duration",
minValue,
maxValue,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.github.hapjava.characteristics.impl.valve;

import static org.assertj.core.api.Assertions.assertThat;

import io.github.hapjava.characteristics.impl.windowcovering.CurrentPositionCharacteristic;
import java.util.concurrent.CompletableFuture;
import javax.json.JsonObject;
import org.junit.Test;

/** Regression tests for https://github.com/hap-java/HAP-Java/issues/202 */
public class DurationCharacteristicFormatTest {

@Test
public void setDurationAdvertisesUint32() {
SetDurationCharacteristic characteristic =
new SetDurationCharacteristic(
() -> CompletableFuture.completedFuture(300), value -> {}, callback -> {}, () -> {});

JsonObject json = characteristic.toJson(12).join();

assertThat(json.getString("type")).isEqualTo("D3");
assertThat(json.getString("format")).isEqualTo("uint32");
assertThat(json.getInt("value")).isEqualTo(300);
assertThat(json.getInt("minValue")).isEqualTo(0);
assertThat(json.getInt("maxValue")).isEqualTo(3600);
assertThat(json.getInt("minStep")).isEqualTo(1);
assertThat(json.getString("unit")).isEqualTo("s");
}

@Test
public void remainingDurationAdvertisesUint32() {
RemainingDurationCharacteristic characteristic =
new RemainingDurationCharacteristic(
() -> CompletableFuture.completedFuture(180), callback -> {}, () -> {});

JsonObject json = characteristic.toJson(13).join();

assertThat(json.getString("type")).isEqualTo("D4");
assertThat(json.getString("format")).isEqualTo("uint32");
assertThat(json.getInt("value")).isEqualTo(180);
assertThat(json.getInt("minValue")).isEqualTo(0);
assertThat(json.getInt("maxValue")).isEqualTo(3600);
assertThat(json.getString("unit")).isEqualTo("s");
}

@Test
public void otherIntegerCharacteristicsKeepIntFormat() {
CurrentPositionCharacteristic characteristic =
new CurrentPositionCharacteristic(
() -> CompletableFuture.completedFuture(50), callback -> {}, () -> {});

JsonObject json = characteristic.toJson(14).join();

assertThat(json.getString("format")).isEqualTo("int");
}
}
Loading