Skip to content

Commit c3908f2

Browse files
committed
Added support for carbon monoxide sensor and smoke sensor.
1 parent f4eef87 commit c3908f2

File tree

8 files changed

+253
-0
lines changed

8 files changed

+253
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.beowulfe.hap.accessories;
2+
3+
import com.beowulfe.hap.HomekitAccessory;
4+
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
5+
import com.beowulfe.hap.Service;
6+
import com.beowulfe.hap.accessories.properties.CarbonMonoxideDetectedState;
7+
import com.beowulfe.hap.impl.services.CarbonMonoxideSensorService;
8+
9+
import java.util.Collection;
10+
import java.util.Collections;
11+
import java.util.concurrent.CompletableFuture;
12+
13+
/**
14+
* <p>A carbon monoxide sensor reports whether carbon monoxide has been detected or not.</p>
15+
*
16+
* <p>Carbon monoxide sensors that run on batteries will need to implement this interface
17+
* and also implement {@link BatteryAccessory}.</p>
18+
*
19+
* @author Gaston Dombiak
20+
*/
21+
public interface CarbonMonoxideSensor extends HomekitAccessory {
22+
23+
/**
24+
* Retrieves the state of the sensor that indicates if carbon monoxide has been detected.
25+
*
26+
* @return a future that will contain the carbon monoxide sensor's state
27+
*/
28+
CompletableFuture<CarbonMonoxideDetectedState> getCarbonMonoxideDetectedState();
29+
30+
@Override
31+
default Collection<Service> getServices() {
32+
return Collections.singleton(new CarbonMonoxideSensorService(this));
33+
}
34+
35+
/**
36+
* Subscribes to changes in the carbon monoxide's state.
37+
*
38+
* @param callback the function to call when the state changes.
39+
*/
40+
void subscribeCarbonMonoxideDetectedState(HomekitCharacteristicChangeCallback callback);
41+
42+
/**
43+
* Unsubscribes from changes in the carbon monoxide's state.
44+
*/
45+
void unsubscribeCarbonMonoxideDetectedState();
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.beowulfe.hap.accessories;
2+
3+
import com.beowulfe.hap.HomekitAccessory;
4+
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
5+
import com.beowulfe.hap.Service;
6+
import com.beowulfe.hap.accessories.properties.SmokeDetectedState;
7+
import com.beowulfe.hap.impl.services.SmokeSensorService;
8+
9+
import java.util.Collection;
10+
import java.util.Collections;
11+
import java.util.concurrent.CompletableFuture;
12+
13+
/**
14+
* <p>A smoke sensor reports whether smoke has been detected or not.</p>
15+
*
16+
* <p>Smoke sensors that run on batteries will need to implement this interface
17+
* and also implement {@link BatteryAccessory}.</p>
18+
*
19+
* @author Gaston Dombiak
20+
*/
21+
public interface SmokeSensor extends HomekitAccessory {
22+
23+
/**
24+
* Retrieves the state of the smoke sensor. This is whether smoke has been detected or not.
25+
*
26+
* @return a future that will contain the smoke sensor's state
27+
*/
28+
CompletableFuture<SmokeDetectedState> getSmokeDetectedState();
29+
30+
@Override
31+
default Collection<Service> getServices() {
32+
return Collections.singleton(new SmokeSensorService(this));
33+
}
34+
35+
/**
36+
* Subscribes to changes in the smoke sensor's state.
37+
*
38+
* @param callback the function to call when the state changes.
39+
*/
40+
void subscribeSmokeDetectedState(HomekitCharacteristicChangeCallback callback);
41+
42+
/**
43+
* Unsubscribes from changes in the smoke sensor's state.
44+
*/
45+
void unsubscribeSmokeDetectedState();
46+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.beowulfe.hap.accessories.properties;
2+
3+
import java.util.Arrays;
4+
import java.util.Map;
5+
import java.util.stream.Collectors;
6+
7+
public enum CarbonMonoxideDetectedState {
8+
9+
NORMAL(0),
10+
ABNORMAL(1);
11+
12+
private final static Map<Integer, CarbonMonoxideDetectedState> reverse;
13+
static {
14+
reverse = Arrays.stream(CarbonMonoxideDetectedState.values()).collect(Collectors.toMap(CarbonMonoxideDetectedState::getCode, t -> t));
15+
}
16+
17+
public static CarbonMonoxideDetectedState fromCode(Integer code) {
18+
return reverse.get(code);
19+
}
20+
21+
private final int code;
22+
23+
CarbonMonoxideDetectedState(int code) {
24+
this.code = code;
25+
}
26+
27+
public int getCode() {
28+
return code;
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.beowulfe.hap.accessories.properties;
2+
3+
import java.util.Arrays;
4+
import java.util.Map;
5+
import java.util.stream.Collectors;
6+
7+
public enum SmokeDetectedState {
8+
9+
NOT_DETECTED(0),
10+
DETECTED(1);
11+
12+
private final static Map<Integer, SmokeDetectedState> reverse;
13+
static {
14+
reverse = Arrays.stream(SmokeDetectedState.values()).collect(Collectors.toMap(SmokeDetectedState::getCode, t -> t));
15+
}
16+
17+
public static SmokeDetectedState fromCode(Integer code) {
18+
return reverse.get(code);
19+
}
20+
21+
private final int code;
22+
23+
SmokeDetectedState(int code) {
24+
this.code = code;
25+
}
26+
27+
public int getCode() {
28+
return code;
29+
}
30+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.beowulfe.hap.impl.characteristics.carbonmonoxide.Carbon;
2+
3+
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
4+
import com.beowulfe.hap.accessories.CarbonMonoxideSensor;
5+
import com.beowulfe.hap.accessories.properties.CarbonMonoxideDetectedState;
6+
import com.beowulfe.hap.characteristics.EnumCharacteristic;
7+
import com.beowulfe.hap.characteristics.EventableCharacteristic;
8+
9+
import java.util.concurrent.CompletableFuture;
10+
11+
public class CarbonMonoxideDetectedCharacteristic extends EnumCharacteristic implements EventableCharacteristic {
12+
13+
private final CarbonMonoxideSensor carbonMonoxideSensor;
14+
15+
public CarbonMonoxideDetectedCharacteristic(CarbonMonoxideSensor carbonMonoxideSensor) {
16+
super("00000069-0000-1000-8000-0026BB765291", false, true, "Carbon Monoxide Detected", 1);
17+
this.carbonMonoxideSensor = carbonMonoxideSensor;
18+
}
19+
20+
@Override
21+
protected CompletableFuture<Integer> getValue() {
22+
return carbonMonoxideSensor.getCarbonMonoxideDetectedState().thenApply(CarbonMonoxideDetectedState::getCode);
23+
}
24+
25+
@Override
26+
protected void setValue(Integer value) throws Exception {
27+
//Read Only
28+
}
29+
30+
@Override
31+
public void subscribe(HomekitCharacteristicChangeCallback callback) {
32+
carbonMonoxideSensor.subscribeCarbonMonoxideDetectedState(callback);
33+
}
34+
35+
@Override
36+
public void unsubscribe() {
37+
carbonMonoxideSensor.unsubscribeCarbonMonoxideDetectedState();
38+
}}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.beowulfe.hap.impl.characteristics.smokesensor;
2+
3+
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
4+
import com.beowulfe.hap.accessories.SmokeSensor;
5+
import com.beowulfe.hap.accessories.properties.SmokeDetectedState;
6+
import com.beowulfe.hap.characteristics.EnumCharacteristic;
7+
import com.beowulfe.hap.characteristics.EventableCharacteristic;
8+
9+
import java.util.concurrent.CompletableFuture;
10+
11+
public class SmokeDetectedCharacteristic extends EnumCharacteristic implements EventableCharacteristic {
12+
13+
private final SmokeSensor smokeSensor;
14+
15+
public SmokeDetectedCharacteristic(SmokeSensor smokeSensor) {
16+
super("00000076-0000-1000-8000-0026BB765291", false, true, "Smoke Detected", 1);
17+
this.smokeSensor = smokeSensor;
18+
}
19+
20+
@Override
21+
protected CompletableFuture<Integer> getValue() {
22+
return smokeSensor.getSmokeDetectedState().thenApply(SmokeDetectedState::getCode);
23+
}
24+
25+
@Override
26+
protected void setValue(Integer value) throws Exception {
27+
//Read Only
28+
}
29+
30+
@Override
31+
public void subscribe(HomekitCharacteristicChangeCallback callback) {
32+
smokeSensor.subscribeSmokeDetectedState(callback);
33+
}
34+
35+
@Override
36+
public void unsubscribe() {
37+
smokeSensor.unsubscribeSmokeDetectedState();
38+
}
39+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.beowulfe.hap.impl.services;
2+
3+
import com.beowulfe.hap.accessories.CarbonMonoxideSensor;
4+
import com.beowulfe.hap.impl.characteristics.carbonmonoxide.Carbon.CarbonMonoxideDetectedCharacteristic;
5+
6+
public class CarbonMonoxideSensorService extends AbstractServiceImpl {
7+
8+
public CarbonMonoxideSensorService(CarbonMonoxideSensor carbonMonoxideSensor) {
9+
super("0000007F-0000-1000-8000-0026BB765291", carbonMonoxideSensor);
10+
addCharacteristic(new CarbonMonoxideDetectedCharacteristic(carbonMonoxideSensor));
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.beowulfe.hap.impl.services;
2+
3+
import com.beowulfe.hap.accessories.SmokeSensor;
4+
import com.beowulfe.hap.impl.characteristics.smokesensor.SmokeDetectedCharacteristic;
5+
6+
public class SmokeSensorService extends AbstractServiceImpl {
7+
8+
public SmokeSensorService(SmokeSensor smokeSensor) {
9+
super("00000087-0000-1000-8000-0026BB765291", smokeSensor);
10+
addCharacteristic(new SmokeDetectedCharacteristic(smokeSensor));
11+
}
12+
}

0 commit comments

Comments
 (0)