Skip to content

Commit 17f87f1

Browse files
committed
add back fanv1 service
the Home app doesn't let you customize the icon for a FanV2 (i.e. to show a ceiling fan).
1 parent 781311e commit 17f87f1

File tree

3 files changed

+117
-1
lines changed

3 files changed

+117
-1
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.github.hapjava.accessories;
2+
3+
import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
4+
import io.github.hapjava.services.Service;
5+
import io.github.hapjava.services.impl.BasicFanService;
6+
import java.util.Collection;
7+
import java.util.Collections;
8+
import java.util.concurrent.CompletableFuture;
9+
10+
/**
11+
* A fan with mandatory characteristics.
12+
*
13+
* @author Andy Lintner
14+
*/
15+
public interface BasicFanAccessory extends HomekitAccessory {
16+
/**
17+
* Mandatory: Retrieves the current power state of the fan.
18+
*
19+
* @return a future that will contain the binary state
20+
*/
21+
CompletableFuture<Boolean> isOn();
22+
23+
/**
24+
* Sets the active state of the fan
25+
*
26+
* @param state the binary state to set
27+
* @return a future that completes when the change is made
28+
* @throws Exception when the change cannot be made
29+
*/
30+
CompletableFuture<Void> setOn(boolean state) throws Exception;
31+
32+
/**
33+
* Subscribes to changes in the active state of the fan.
34+
*
35+
* @param callback the function to call when the active state changes.
36+
*/
37+
void subscribeOn(HomekitCharacteristicChangeCallback callback);
38+
39+
/** Unsubscribes from changes in the active state of the fan. */
40+
void unsubscribeOn();
41+
42+
@Override
43+
default Collection<Service> getServices() {
44+
return Collections.singleton(new BasicFanService(this));
45+
}
46+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package io.github.hapjava.services.impl;
2+
3+
import io.github.hapjava.accessories.BasicFanAccessory;
4+
import io.github.hapjava.accessories.optionalcharacteristic.AccessoryWithName;
5+
import io.github.hapjava.accessories.optionalcharacteristic.AccessoryWithRotationDirection;
6+
import io.github.hapjava.accessories.optionalcharacteristic.AccessoryWithRotationSpeed;
7+
import io.github.hapjava.characteristics.impl.common.NameCharacteristic;
8+
import io.github.hapjava.characteristics.impl.common.OnCharacteristic;
9+
import io.github.hapjava.characteristics.impl.fan.RotationDirectionCharacteristic;
10+
import io.github.hapjava.characteristics.impl.fan.RotationSpeedCharacteristic;
11+
12+
/**
13+
* This service describes a fan.
14+
*
15+
* <p>In the R1 release of the HAP specification, this is simply described as Fan. It is no longer
16+
* present in the R2 release.
17+
*/
18+
public class BasicFanService extends AbstractServiceImpl {
19+
20+
public BasicFanService(OnCharacteristic on) {
21+
super("00000040-0000-1000-8000-0026BB765291");
22+
addCharacteristic(on);
23+
}
24+
25+
public BasicFanService(BasicFanAccessory accessory) {
26+
this(
27+
new OnCharacteristic(
28+
() -> accessory.isOn(),
29+
(v) -> accessory.setOn(v),
30+
accessory::subscribeOn,
31+
accessory::unsubscribeOn));
32+
if (accessory instanceof AccessoryWithName) {
33+
addOptionalCharacteristic(new NameCharacteristic(((AccessoryWithName) accessory)::getName));
34+
}
35+
36+
if (accessory instanceof AccessoryWithRotationDirection) {
37+
addOptionalCharacteristic(
38+
new RotationDirectionCharacteristic(
39+
((AccessoryWithRotationDirection) accessory)::getRotationDirection,
40+
((AccessoryWithRotationDirection) accessory)::setRotationDirection,
41+
((AccessoryWithRotationDirection) accessory)::subscribeRotationDirection,
42+
((AccessoryWithRotationDirection) accessory)::unsubscribeRotationDirection));
43+
}
44+
if (accessory instanceof AccessoryWithRotationSpeed) {
45+
addOptionalCharacteristic(
46+
new RotationSpeedCharacteristic(
47+
((AccessoryWithRotationSpeed) accessory)::getRotationSpeed,
48+
((AccessoryWithRotationSpeed) accessory)::setRotationSpeed,
49+
((AccessoryWithRotationSpeed) accessory)::subscribeRotationSpeed,
50+
((AccessoryWithRotationSpeed) accessory)::unsubscribeRotationSpeed));
51+
}
52+
}
53+
54+
public void addOptionalCharacteristic(NameCharacteristic name) {
55+
addCharacteristic(name);
56+
}
57+
58+
public void addOptionalCharacteristic(RotationDirectionCharacteristic direction) {
59+
addCharacteristic(direction);
60+
}
61+
62+
public void addOptionalCharacteristic(RotationSpeedCharacteristic speed) {
63+
addCharacteristic(speed);
64+
}
65+
}

src/main/java/io/github/hapjava/services/impl/FanService.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
import io.github.hapjava.characteristics.impl.fan.SwingModeCharacteristic;
1818
import io.github.hapjava.characteristics.impl.fan.TargetFanStateCharacteristic;
1919

20-
/** This service describes a fan. */
20+
/**
21+
* This service describes a fan.
22+
*
23+
* <p>In the R1 release of the HAP specification, this is described as Fan v2. In the R1 release of
24+
* the HAP specification, this is simply described as Fan.
25+
*/
2126
public class FanService extends AbstractServiceImpl {
2227

2328
public FanService(ActiveCharacteristic active) {

0 commit comments

Comments
 (0)