Skip to content

Commit 578c8be

Browse files
committed
make optional fan characteristics... optional
since I'm "removing" characteristics here, Fan actually extends the "optional" interfaces, and those optional interfaces have default "empty" implemenations, so that compatibility is maintained, but an implementor can immediately start using the new interface.
1 parent 4cdc4c9 commit 578c8be

File tree

6 files changed

+99
-62
lines changed

6 files changed

+99
-62
lines changed
Lines changed: 12 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package com.beowulfe.hap.accessories;
22

33
import com.beowulfe.hap.*;
4-
import com.beowulfe.hap.accessories.properties.RotationDirection;
4+
import com.beowulfe.hap.accessories.characteristics.RotationDirection;
5+
import com.beowulfe.hap.accessories.characteristics.RotationSpeed;
56
import com.beowulfe.hap.impl.services.FanService;
67
import java.util.Collection;
78
import java.util.Collections;
9+
import java.util.Optional;
810
import java.util.concurrent.CompletableFuture;
911

1012
/**
1113
* A fan, with power and rotational characteristics.
1214
*
1315
* @author Andy Lintner
1416
*/
15-
public interface Fan extends HomekitAccessory {
17+
public interface Fan extends HomekitAccessory, RotationDirection, RotationSpeed {
1618

1719
/**
1820
* Retrieves the current binary state of the fan's power.
@@ -21,20 +23,6 @@ public interface Fan extends HomekitAccessory {
2123
*/
2224
CompletableFuture<Boolean> getFanPower();
2325

24-
/**
25-
* Retrieves the current rotation direction of the fan.
26-
*
27-
* @return a future that will contain the direction
28-
*/
29-
CompletableFuture<RotationDirection> getRotationDirection();
30-
31-
/**
32-
* Retrieves the current speed of the fan's rotation
33-
*
34-
* @return a future that will contain the speed, expressed as an integer between 0 and 100.
35-
*/
36-
CompletableFuture<Integer> getRotationSpeed();
37-
3826
/**
3927
* Sets the binary state of the fan's power
4028
*
@@ -44,24 +32,6 @@ public interface Fan extends HomekitAccessory {
4432
*/
4533
CompletableFuture<Void> setFanPower(boolean state) throws Exception;
4634

47-
/**
48-
* Sets the rotation direction of the fan
49-
*
50-
* @param direction the direction to set
51-
* @return a future that completes when the change is made
52-
* @throws Exception when the change cannot be made
53-
*/
54-
CompletableFuture<Void> setRotationDirection(RotationDirection direction) throws Exception;
55-
56-
/**
57-
* Sets the speed of the fan's rotation
58-
*
59-
* @param speed the speed to set, expressed as an integer between 0 and 100.
60-
* @return a future that completes when the change is made
61-
* @throws Exception when the change cannot be made
62-
*/
63-
CompletableFuture<Void> setRotationSpeed(Integer speed) throws Exception;
64-
6535
@Override
6636
default Collection<Service> getServices() {
6737
return Collections.singleton(new FanService(this));
@@ -74,26 +44,16 @@ default Collection<Service> getServices() {
7444
*/
7545
void subscribeFanPower(HomekitCharacteristicChangeCallback callback);
7646

77-
/**
78-
* Subscribes to changes in the rotation direction of the fan.
79-
*
80-
* @param callback the function to call when the direction changes.
81-
*/
82-
void subscribeRotationDirection(HomekitCharacteristicChangeCallback callback);
83-
84-
/**
85-
* Subscribes to changes in the rotation speed of the fan.
86-
*
87-
* @param callback the function to call when the speed changes.
88-
*/
89-
void subscribeRotationSpeed(HomekitCharacteristicChangeCallback callback);
90-
9147
/** Unsubscribes from changes in the binary state of the fan's power. */
9248
void unsubscribeFanPower();
9349

94-
/** Unsubscribes from changes in the rotation direction of the fan. */
95-
void unsubscribeRotationDirection();
50+
/** returns the optional implementation of RotationDirection */
51+
default Optional<RotationDirection> getRotationDirectionCharacteristic() {
52+
return Optional.of(this);
53+
}
9654

97-
/** Unsubscribes from changes in the fan's rotation speed. */
98-
void unsubscribeRotationSpeed();
55+
/** returns the optional implementation of RotationSpeed */
56+
default Optional<RotationSpeed> getRotationSpeedCharacteristic() {
57+
return Optional.of(this);
58+
}
9959
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.beowulfe.hap.accessories.characteristics;
2+
3+
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
4+
import java.util.concurrent.CompletableFuture;
5+
6+
public interface RotationDirection {
7+
/**
8+
* Retrieves the current rotation direction of the fan.
9+
*
10+
* @return a future that will contain the direction
11+
*/
12+
default CompletableFuture<com.beowulfe.hap.accessories.properties.RotationDirection>
13+
getRotationDirection() {
14+
return CompletableFuture.completedFuture(
15+
com.beowulfe.hap.accessories.properties.RotationDirection.CLOCKWISE);
16+
}
17+
18+
/**
19+
* Sets the rotation direction of the fan
20+
*
21+
* @param direction the direction to set
22+
* @return a future that completes when the change is made
23+
* @throws Exception when the change cannot be made
24+
*/
25+
default CompletableFuture<Void> setRotationDirection(
26+
com.beowulfe.hap.accessories.properties.RotationDirection direction) throws Exception {
27+
return CompletableFuture.completedFuture(null);
28+
}
29+
30+
/**
31+
* Subscribes to changes in the rotation direction of the fan.
32+
*
33+
* @param callback the function to call when the direction changes.
34+
*/
35+
default void subscribeRotationDirection(HomekitCharacteristicChangeCallback callback) {}
36+
37+
/** Unsubscribes from changes in the rotation direction of the fan. */
38+
default void unsubscribeRotationDirection() {}
39+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.beowulfe.hap.accessories.characteristics;
2+
3+
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
4+
import java.util.concurrent.CompletableFuture;
5+
6+
public interface RotationSpeed {
7+
/**
8+
* Retrieves the current speed of the fan's rotation
9+
*
10+
* @return a future that will contain the speed, expressed as an integer between 0 and 100.
11+
*/
12+
default CompletableFuture<Integer> getRotationSpeed() {
13+
return CompletableFuture.completedFuture(100);
14+
}
15+
16+
/**
17+
* Sets the speed of the fan's rotation
18+
*
19+
* @param speed the speed to set, expressed as an integer between 0 and 100.
20+
* @return a future that completes when the change is made
21+
* @throws Exception when the change cannot be made
22+
*/
23+
default CompletableFuture<Void> setRotationSpeed(Integer speed) throws Exception {
24+
return CompletableFuture.completedFuture(null);
25+
}
26+
27+
/**
28+
* Subscribes to changes in the rotation speed of the fan.
29+
*
30+
* @param callback the function to call when the speed changes.
31+
*/
32+
default void subscribeRotationSpeed(HomekitCharacteristicChangeCallback callback) {}
33+
34+
/** Unsubscribes from changes in the fan's rotation speed. */
35+
default void unsubscribeRotationSpeed() {}
36+
}

src/main/java/com/beowulfe/hap/impl/characteristics/fan/RotationDirectionCharacteristic.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
package com.beowulfe.hap.impl.characteristics.fan;
22

33
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
4-
import com.beowulfe.hap.accessories.Fan;
5-
import com.beowulfe.hap.accessories.properties.RotationDirection;
4+
import com.beowulfe.hap.accessories.characteristics.RotationDirection;
65
import com.beowulfe.hap.characteristics.EnumCharacteristic;
76
import com.beowulfe.hap.characteristics.EventableCharacteristic;
87
import java.util.concurrent.CompletableFuture;
98

109
public class RotationDirectionCharacteristic extends EnumCharacteristic
1110
implements EventableCharacteristic {
1211

13-
private final Fan fan;
12+
private final RotationDirection fan;
1413

15-
public RotationDirectionCharacteristic(Fan fan) {
14+
public RotationDirectionCharacteristic(RotationDirection fan) {
1615
super("00000028-0000-1000-8000-0026BB765291", true, true, "Rotation Direction", 1);
1716
this.fan = fan;
1817
}
1918

2019
@Override
2120
protected void setValue(Integer value) throws Exception {
22-
fan.setRotationDirection(RotationDirection.fromCode(value));
21+
fan.setRotationDirection(
22+
com.beowulfe.hap.accessories.properties.RotationDirection.fromCode(value));
2323
}
2424

2525
@Override

src/main/java/com/beowulfe/hap/impl/characteristics/fan/RotationSpeedCharacteristic.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package com.beowulfe.hap.impl.characteristics.fan;
22

33
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
4-
import com.beowulfe.hap.accessories.Fan;
4+
import com.beowulfe.hap.accessories.characteristics.RotationSpeed;
55
import com.beowulfe.hap.characteristics.EventableCharacteristic;
66
import com.beowulfe.hap.characteristics.IntegerCharacteristic;
77
import java.util.concurrent.CompletableFuture;
88

99
public class RotationSpeedCharacteristic extends IntegerCharacteristic
1010
implements EventableCharacteristic {
1111

12-
private final Fan fan;
12+
private final RotationSpeed fan;
1313

14-
public RotationSpeedCharacteristic(Fan fan) {
14+
public RotationSpeedCharacteristic(RotationSpeed fan) {
1515
super("00000029-0000-1000-8000-0026BB765291", true, true, "Rotation speed", 0, 100, "%");
1616
this.fan = fan;
1717
}

src/main/java/com/beowulfe/hap/impl/services/FanService.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public FanService(Fan fan, String serviceName) {
1919
v -> fan.setFanPower(v),
2020
c -> fan.subscribeFanPower(c),
2121
() -> fan.unsubscribeFanPower()));
22-
addCharacteristic(new RotationDirectionCharacteristic(fan));
23-
addCharacteristic(new RotationSpeedCharacteristic(fan));
22+
fan.getRotationDirectionCharacteristic()
23+
.ifPresent(rotation -> addCharacteristic(new RotationDirectionCharacteristic(rotation)));
24+
fan.getRotationSpeedCharacteristic()
25+
.ifPresent(speed -> addCharacteristic(new RotationSpeedCharacteristic(speed)));
2426
}
2527
}

0 commit comments

Comments
 (0)