Skip to content

Commit 4cdc4c9

Browse files
committed
refactor Brightness and Color into optional characteristics on base Lightbulb
maintaining compatibility with the now-deprecated DimmableLightbulb and ColorfulLightbulb interfaces
1 parent 1544779 commit 4cdc4c9

File tree

9 files changed

+148
-104
lines changed

9 files changed

+148
-104
lines changed
Lines changed: 10 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,18 @@
11
package com.beowulfe.hap.accessories;
22

3-
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
4-
import java.util.concurrent.CompletableFuture;
3+
import com.beowulfe.hap.accessories.characteristics.Color;
4+
import java.util.Optional;
55

66
/**
7-
* Extends {@link Lightbulb} with color settings. This will usually be implemented along with {@link
8-
* DimmableLightbulb}, but not necessarily so.
7+
* Extends {@link com.beowulfe.hap.accessories.Lightbulb} with color settings. This will usually be
8+
* implemented along with {@link DimmableLightbulb}, but not necessarily so.
99
*
1010
* @author Andy Lintner
1111
*/
12-
public interface ColorfulLightbulb extends Lightbulb {
13-
14-
/**
15-
* Retrieves the current hue of the light.
16-
*
17-
* @return a future that will contain the hue, expressed in arc degrees from 0 to 360.
18-
*/
19-
CompletableFuture<Double> getHue();
20-
21-
/**
22-
* Sets the current hue of the light
23-
*
24-
* @param value the hue to set, expressed in arc degrees from 0 to 360.
25-
* @return a future that completes when the hue is changed
26-
* @throws Exception when the hue cannot be changed.
27-
*/
28-
CompletableFuture<Void> setHue(Double value) throws Exception;
29-
30-
/**
31-
* Subscribes to changes in the hue of the light.
32-
*
33-
* @param callback the function to call when the state changes.
34-
*/
35-
void subscribeHue(HomekitCharacteristicChangeCallback callback);
36-
37-
/** Unsubscribes from changes in the hue of the light. */
38-
void unsubscribeHue();
39-
40-
/**
41-
* Retrieves the saturation of the light.
42-
*
43-
* @return a future that will contain the saturation, expressed as a value between 0 and 100.
44-
*/
45-
CompletableFuture<Double> getSaturation();
46-
47-
/**
48-
* Sets the saturation of the light.
49-
*
50-
* @param value the saturation to set, expressed as a value between 0 and 100.
51-
* @return a future that completes when the saturation is changed.
52-
* @throws Exception when the saturation cannot be set.
53-
*/
54-
CompletableFuture<Void> setSaturation(Double value) throws Exception;
55-
56-
/**
57-
* Subscribes to changes in the saturation of the light.
58-
*
59-
* @param callback the function to call when the state changes.
60-
*/
61-
void subscribeSaturation(HomekitCharacteristicChangeCallback callback);
62-
63-
/** Unsubscribes from changes in the saturation of the light. */
64-
void unsubscribeSaturation();
12+
@Deprecated
13+
public interface ColorfulLightbulb extends Lightbulb, Color {
14+
@Override
15+
default Optional<Color> getColorCharacteristics() {
16+
return Optional.of(this);
17+
}
6518
}
Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,17 @@
11
package com.beowulfe.hap.accessories;
22

3-
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
4-
import java.util.concurrent.CompletableFuture;
3+
import com.beowulfe.hap.accessories.characteristics.Brightness;
4+
import java.util.Optional;
55

66
/**
77
* Extends {@link Lightbulb} with brightness values.
88
*
99
* @author Andy Lintner
1010
*/
11-
public interface DimmableLightbulb extends Lightbulb {
12-
13-
/**
14-
* Retrieves the current brightness of the light
15-
*
16-
* @return a future that will contain the brightness, expressed as an integer between 0 and 100.
17-
*/
18-
CompletableFuture<Integer> getBrightness();
19-
20-
/**
21-
* Sets the current brightness of the light
22-
*
23-
* @param value the brightness, on a scale of 0 to 100, to set
24-
* @return a future that completes when the brightness is changed
25-
* @throws Exception when the brightness cannot be set
26-
*/
27-
CompletableFuture<Void> setBrightness(Integer value) throws Exception;
28-
29-
/**
30-
* Subscribes to changes in the brightness of the light.
31-
*
32-
* @param callback the function to call when the state changes.
33-
*/
34-
void subscribeBrightness(HomekitCharacteristicChangeCallback callback);
35-
36-
/** Unsubscribes from changes in the brightness of the light. */
37-
void unsubscribeBrightness();
11+
@Deprecated
12+
public interface DimmableLightbulb extends Lightbulb, Brightness {
13+
@Override
14+
default Optional<Brightness> getBrightnessCharacteristic() {
15+
return Optional.of(this);
16+
}
3817
}

src/main/java/com/beowulfe/hap/accessories/Lightbulb.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.beowulfe.hap.accessories;
22

33
import com.beowulfe.hap.*;
4+
import com.beowulfe.hap.accessories.characteristics.Brightness;
5+
import com.beowulfe.hap.accessories.characteristics.Color;
46
import com.beowulfe.hap.impl.services.LightbulbService;
57
import java.util.Collection;
68
import java.util.Collections;
9+
import java.util.Optional;
710
import java.util.concurrent.CompletableFuture;
811

912
/**
@@ -43,4 +46,16 @@ default Collection<Service> getServices() {
4346

4447
/** Unsubscribes from changes in the binary state of the light. */
4548
void unsubscribeLightbulbPowerState();
49+
50+
/** returns the optional implementation of Brightness */
51+
default Optional<Brightness> getBrightnessCharacteristic() {
52+
Optional<Brightness> result = Optional.empty();
53+
return result;
54+
}
55+
56+
/** returns the optional implementation of Color */
57+
default Optional<Color> getColorCharacteristics() {
58+
Optional<Color> result = Optional.empty();
59+
return result;
60+
}
4661
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.beowulfe.hap.accessories.characteristics;
2+
3+
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
4+
import java.util.concurrent.CompletableFuture;
5+
6+
public interface Brightness {
7+
/**
8+
* Retrieves the current brightness of the light
9+
*
10+
* @return a future that will contain the brightness, expressed as an integer between 0 and 100.
11+
*/
12+
CompletableFuture<Integer> getBrightness();
13+
14+
/**
15+
* Sets the current brightness of the light
16+
*
17+
* @param value the brightness, on a scale of 0 to 100, to set
18+
* @return a future that completes when the brightness is changed
19+
* @throws Exception when the brightness cannot be set
20+
*/
21+
CompletableFuture<Void> setBrightness(Integer value) throws Exception;
22+
23+
/**
24+
* Subscribes to changes in the brightness of the light.
25+
*
26+
* @param callback the function to call when the state changes.
27+
*/
28+
void subscribeBrightness(HomekitCharacteristicChangeCallback callback);
29+
30+
/** Unsubscribes from changes in the brightness of the light. */
31+
void unsubscribeBrightness();
32+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.beowulfe.hap.accessories.characteristics;
2+
3+
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
4+
import java.util.concurrent.CompletableFuture;
5+
6+
/**
7+
* Extends a Lightbulb with color settings. This will usually be implemented along with {@link
8+
* Brightness}, but not necessarily so.
9+
*
10+
* @author Andy Lintner
11+
*/
12+
public interface Color {
13+
/**
14+
* Retrieves the current hue of the light.
15+
*
16+
* @return a future that will contain the hue, expressed in arc degrees from 0 to 360.
17+
*/
18+
CompletableFuture<Double> getHue();
19+
20+
/**
21+
* Sets the current hue of the light
22+
*
23+
* @param value the hue to set, expressed in arc degrees from 0 to 360.
24+
* @return a future that completes when the hue is changed
25+
* @throws Exception when the hue cannot be changed.
26+
*/
27+
CompletableFuture<Void> setHue(Double value) throws Exception;
28+
29+
/**
30+
* Subscribes to changes in the hue of the light.
31+
*
32+
* @param callback the function to call when the state changes.
33+
*/
34+
void subscribeHue(HomekitCharacteristicChangeCallback callback);
35+
36+
/** Unsubscribes from changes in the hue of the light. */
37+
void unsubscribeHue();
38+
39+
/**
40+
* Retrieves the saturation of the light.
41+
*
42+
* @return a future that will contain the saturation, expressed as a value between 0 and 100.
43+
*/
44+
CompletableFuture<Double> getSaturation();
45+
46+
/**
47+
* Sets the saturation of the light.
48+
*
49+
* @param value the saturation to set, expressed as a value between 0 and 100.
50+
* @return a future that completes when the saturation is changed.
51+
* @throws Exception when the saturation cannot be set.
52+
*/
53+
CompletableFuture<Void> setSaturation(Double value) throws Exception;
54+
55+
/**
56+
* Subscribes to changes in the saturation of the light.
57+
*
58+
* @param callback the function to call when the state changes.
59+
*/
60+
void subscribeSaturation(HomekitCharacteristicChangeCallback callback);
61+
62+
/** Unsubscribes from changes in the saturation of the light. */
63+
void unsubscribeSaturation();
64+
}

src/main/java/com/beowulfe/hap/impl/characteristics/lightbulb/BrightnessCharacteristic.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.lightbulb;
22

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

99
public class BrightnessCharacteristic extends IntegerCharacteristic
1010
implements EventableCharacteristic {
1111

12-
private final DimmableLightbulb lightbulb;
12+
private final Brightness lightbulb;
1313

14-
public BrightnessCharacteristic(DimmableLightbulb lightbulb) {
14+
public BrightnessCharacteristic(Brightness lightbulb) {
1515
super(
1616
"00000008-0000-1000-8000-0026BB765291",
1717
true,

src/main/java/com/beowulfe/hap/impl/characteristics/lightbulb/HueCharacteristic.java

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

33
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
4-
import com.beowulfe.hap.accessories.ColorfulLightbulb;
4+
import com.beowulfe.hap.accessories.characteristics.Color;
55
import com.beowulfe.hap.characteristics.EventableCharacteristic;
66
import com.beowulfe.hap.characteristics.FloatCharacteristic;
77
import java.util.concurrent.CompletableFuture;
88

99
public class HueCharacteristic extends FloatCharacteristic implements EventableCharacteristic {
1010

11-
private final ColorfulLightbulb lightbulb;
11+
private final Color lightbulb;
1212

13-
public HueCharacteristic(ColorfulLightbulb lightbulb) {
13+
public HueCharacteristic(Color lightbulb) {
1414
super(
1515
"00000013-0000-1000-8000-0026BB765291",
1616
true,

src/main/java/com/beowulfe/hap/impl/characteristics/lightbulb/SaturationCharacteristic.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.lightbulb;
22

33
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
4-
import com.beowulfe.hap.accessories.ColorfulLightbulb;
4+
import com.beowulfe.hap.accessories.characteristics.Color;
55
import com.beowulfe.hap.characteristics.EventableCharacteristic;
66
import com.beowulfe.hap.characteristics.FloatCharacteristic;
77
import java.util.concurrent.CompletableFuture;
88

99
public class SaturationCharacteristic extends FloatCharacteristic
1010
implements EventableCharacteristic {
1111

12-
private final ColorfulLightbulb lightbulb;
12+
private final Color lightbulb;
1313

14-
public SaturationCharacteristic(ColorfulLightbulb lightbulb) {
14+
public SaturationCharacteristic(Color lightbulb) {
1515
super(
1616
"0000002F-0000-1000-8000-0026BB765291",
1717
true,

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.beowulfe.hap.impl.services;
22

3-
import com.beowulfe.hap.accessories.ColorfulLightbulb;
4-
import com.beowulfe.hap.accessories.DimmableLightbulb;
53
import com.beowulfe.hap.accessories.Lightbulb;
64
import com.beowulfe.hap.impl.characteristics.common.PowerStateCharacteristic;
75
import com.beowulfe.hap.impl.characteristics.lightbulb.BrightnessCharacteristic;
@@ -23,13 +21,16 @@ public LightbulbService(Lightbulb lightbulb, String serviceName) {
2321
c -> lightbulb.subscribeLightbulbPowerState(c),
2422
() -> lightbulb.unsubscribeLightbulbPowerState()));
2523

26-
if (lightbulb instanceof DimmableLightbulb) {
27-
addCharacteristic(new BrightnessCharacteristic((DimmableLightbulb) lightbulb));
28-
}
24+
lightbulb
25+
.getBrightnessCharacteristic()
26+
.ifPresent(brightness -> addCharacteristic(new BrightnessCharacteristic(brightness)));
2927

30-
if (lightbulb instanceof ColorfulLightbulb) {
31-
addCharacteristic(new HueCharacteristic((ColorfulLightbulb) lightbulb));
32-
addCharacteristic(new SaturationCharacteristic((ColorfulLightbulb) lightbulb));
33-
}
28+
lightbulb
29+
.getColorCharacteristics()
30+
.ifPresent(
31+
color -> {
32+
addCharacteristic(new HueCharacteristic(color));
33+
addCharacteristic(new SaturationCharacteristic(color));
34+
});
3435
}
3536
}

0 commit comments

Comments
 (0)