Skip to content

Optional characteristics for lightbulbs #70

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 1 commit 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
15 changes: 15 additions & 0 deletions src/main/java/com/beowulfe/hap/accessories/Lightbulb.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.beowulfe.hap.accessories;

import com.beowulfe.hap.*;
import com.beowulfe.hap.accessories.characteristics.Brightness;
import com.beowulfe.hap.accessories.characteristics.Color;
import com.beowulfe.hap.impl.services.LightbulbService;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

/**
Expand Down Expand Up @@ -43,4 +46,16 @@ default Collection<Service> getServices() {

/** Unsubscribes from changes in the binary state of the light. */
void unsubscribeLightbulbPowerState();

/** returns the optional implementation of Brightness */
default Optional<Brightness> getBrightnessCharacteristic() {
Optional<Brightness> result = Optional.empty();
return result;
}

/** returns the optional implementation of Color */
default Optional<Color> getColorCharacteristics() {
Optional<Color> result = Optional.empty();
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package com.beowulfe.hap.accessories;
package com.beowulfe.hap.accessories.characteristics;

import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
import java.util.concurrent.CompletableFuture;

/**
* Extends {@link Lightbulb} with brightness values.
*
* @author Andy Lintner
*/
public interface DimmableLightbulb extends Lightbulb {

public interface Brightness {
/**
* Retrieves the current brightness of the light
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.beowulfe.hap.accessories;
package com.beowulfe.hap.accessories.characteristics;

import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
import java.util.concurrent.CompletableFuture;

/**
* Extends {@link Lightbulb} with color settings. This will usually be implemented along with {@link
* DimmableLightbulb}, but not necessarily so.
* Extends a Lightbulb with color settings. This will usually be implemented along with {@link
* Brightness}, but not necessarily so.
*
* @author Andy Lintner
*/
public interface ColorfulLightbulb extends Lightbulb {

public interface Color {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like where this is going. After some thought, I wonder if this interface is probably more trouble than it's worth.

Perhaps we can just axe it and have the respective characteristics take lambda functions as appropriate. For read-only characteristics, it takes only a getter function and a subscription. For write-only, only a setter. Then the respective accessories can just return an Optional of the characteristic, with the lambda functions defined as needed?

WDYT?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I like that.

/**
* Retrieves the current hue of the light.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.beowulfe.hap.impl.characteristics.lightbulb;

import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
import com.beowulfe.hap.accessories.DimmableLightbulb;
import com.beowulfe.hap.accessories.characteristics.Brightness;
import com.beowulfe.hap.characteristics.EventableCharacteristic;
import com.beowulfe.hap.characteristics.IntegerCharacteristic;
import java.util.concurrent.CompletableFuture;

public class BrightnessCharacteristic extends IntegerCharacteristic
implements EventableCharacteristic {

private final DimmableLightbulb lightbulb;
private final Brightness lightbulb;

public BrightnessCharacteristic(DimmableLightbulb lightbulb) {
public BrightnessCharacteristic(Brightness lightbulb) {
super(
"00000008-0000-1000-8000-0026BB765291",
true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.beowulfe.hap.impl.characteristics.lightbulb;

import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
import com.beowulfe.hap.accessories.ColorfulLightbulb;
import com.beowulfe.hap.accessories.characteristics.Color;
import com.beowulfe.hap.characteristics.EventableCharacteristic;
import com.beowulfe.hap.characteristics.FloatCharacteristic;
import java.util.concurrent.CompletableFuture;

public class HueCharacteristic extends FloatCharacteristic implements EventableCharacteristic {

private final ColorfulLightbulb lightbulb;
private final Color lightbulb;

public HueCharacteristic(ColorfulLightbulb lightbulb) {
public HueCharacteristic(Color lightbulb) {
super(
"00000013-0000-1000-8000-0026BB765291",
true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.beowulfe.hap.impl.characteristics.lightbulb;

import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
import com.beowulfe.hap.accessories.ColorfulLightbulb;
import com.beowulfe.hap.accessories.characteristics.Color;
import com.beowulfe.hap.characteristics.EventableCharacteristic;
import com.beowulfe.hap.characteristics.FloatCharacteristic;
import java.util.concurrent.CompletableFuture;

public class SaturationCharacteristic extends FloatCharacteristic
implements EventableCharacteristic {

private final ColorfulLightbulb lightbulb;
private final Color lightbulb;

public SaturationCharacteristic(ColorfulLightbulb lightbulb) {
public SaturationCharacteristic(Color lightbulb) {
super(
"0000002F-0000-1000-8000-0026BB765291",
true,
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/com/beowulfe/hap/impl/services/LightbulbService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.beowulfe.hap.impl.services;

import com.beowulfe.hap.accessories.ColorfulLightbulb;
import com.beowulfe.hap.accessories.DimmableLightbulb;
import com.beowulfe.hap.accessories.Lightbulb;
import com.beowulfe.hap.impl.characteristics.common.PowerStateCharacteristic;
import com.beowulfe.hap.impl.characteristics.lightbulb.BrightnessCharacteristic;
Expand All @@ -23,13 +21,16 @@ public LightbulbService(Lightbulb lightbulb, String serviceName) {
c -> lightbulb.subscribeLightbulbPowerState(c),
() -> lightbulb.unsubscribeLightbulbPowerState()));

if (lightbulb instanceof DimmableLightbulb) {
addCharacteristic(new BrightnessCharacteristic((DimmableLightbulb) lightbulb));
}
lightbulb
.getBrightnessCharacteristic()
.ifPresent(brightness -> addCharacteristic(new BrightnessCharacteristic(brightness)));

if (lightbulb instanceof ColorfulLightbulb) {
addCharacteristic(new HueCharacteristic((ColorfulLightbulb) lightbulb));
addCharacteristic(new SaturationCharacteristic((ColorfulLightbulb) lightbulb));
}
lightbulb
.getColorCharacteristics()
.ifPresent(
color -> {
addCharacteristic(new HueCharacteristic(color));
addCharacteristic(new SaturationCharacteristic(color));
});
}
}