Skip to content

Commit c8dcefd

Browse files
committed
Added support for security system.
1 parent 1c1e0a3 commit c8dcefd

File tree

8 files changed

+382
-0
lines changed

8 files changed

+382
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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.CurrentSecuritySystemState;
7+
import com.beowulfe.hap.accessories.properties.SecuritySystemAlarmType;
8+
import com.beowulfe.hap.accessories.properties.TargetSecuritySystemState;
9+
import com.beowulfe.hap.impl.services.SecuritySystemService;
10+
11+
import java.util.Collection;
12+
import java.util.Collections;
13+
import java.util.concurrent.CompletableFuture;
14+
15+
/**
16+
* <p>A security system that can be armed so that when a contact sensor is opened or a motion
17+
* sensor detects movement, then a siren could be fired off. There are different modes for arming
18+
* the system. See {@link TargetSecuritySystemState} for more information.</p>
19+
*
20+
* @author Gaston Dombiak
21+
*/
22+
public interface SecuritySystem extends HomekitAccessory {
23+
24+
/**
25+
* Retrieves the current state of the security system. The state describes if the system
26+
* is armed in any of its variations; or if the alarm has been triggered; or if the system
27+
* is disarmed.
28+
*
29+
* @return current state of the security system.
30+
*/
31+
CompletableFuture<CurrentSecuritySystemState> getCurrentSecuritySystemState();
32+
33+
/**
34+
* Subscribes to changes to the state of the security system.
35+
*
36+
* @param callback the function to call when the state changes.
37+
*/
38+
void subscribeCurrentSecuritySystemState(HomekitCharacteristicChangeCallback callback);
39+
40+
/**
41+
* Unsubscribes from changes in the state of the security system.
42+
*/
43+
void unsubscribeCurrentSecuritySystemState();
44+
45+
/**
46+
* Sets the state of the security system. The security system could be armed in any
47+
* of its variations or disarmed.
48+
*
49+
* @param state target state of the security system.
50+
* @throws Exception when the change cannot be made.
51+
*/
52+
void setTargetSecuritySystemState(TargetSecuritySystemState state) throws Exception;
53+
54+
/**
55+
* Retrieves the pending, but not yet completed, state of the security system.
56+
*
57+
* @return target state of the security system.
58+
*/
59+
CompletableFuture<TargetSecuritySystemState> getTargetSecuritySystemState();
60+
61+
/**
62+
* Subscribes to changes in the pending, but not yet completed, state of the security system.
63+
*
64+
* @param callback the function to call when the state changes.
65+
*/
66+
void subscribeTargetSecuritySystemState(HomekitCharacteristicChangeCallback callback);
67+
68+
/**
69+
* Unsubscribes from changes in the pending, but not yet completed, state of the security system.
70+
*/
71+
void unsubscribeTargetSecuritySystemState();
72+
73+
/**
74+
* Retrieves the alarm type of the security system.
75+
*
76+
* @return alarm type of the security system.
77+
*/
78+
CompletableFuture<SecuritySystemAlarmType> getAlarmTypeState();
79+
80+
/**
81+
* Subscribes to changes to the alarm type of the security system.
82+
*
83+
* @param callback the function to call when the alarm type changes.
84+
*/
85+
void subscribeAlarmTypeState(HomekitCharacteristicChangeCallback callback);
86+
87+
/**
88+
* Unsubscribes from changes in the alarm type of the security system.
89+
*/
90+
void unsubscribeAlarmTypeState();
91+
92+
@Override
93+
default Collection<Service> getServices() {
94+
return Collections.singleton(new SecuritySystemService(this));
95+
}
96+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.beowulfe.hap.accessories.properties;
2+
3+
import com.beowulfe.hap.accessories.SecuritySystem;
4+
5+
import java.util.Arrays;
6+
import java.util.Map;
7+
import java.util.stream.Collectors;
8+
9+
/**
10+
* The current state of a {@link SecuritySystem}. Unlike {@link TargetSecuritySystemState}, this enum
11+
* includes a triggered state.
12+
*
13+
* @author Gaston Dombiak
14+
*/
15+
public enum CurrentSecuritySystemState {
16+
17+
/**
18+
* The home is occupied and residents are active.
19+
*/
20+
STAY_ARM(0),
21+
/**
22+
* The home is unoccupied.
23+
*/
24+
AWAY_ARM(1),
25+
/**
26+
* The home is occupied and residents are sleeping.
27+
*/
28+
NIGHT_ARM(2),
29+
/**
30+
* The security system is disarmed.
31+
*/
32+
DISARMED(3),
33+
/**
34+
* The security system is triggered.
35+
*/
36+
TRIGGERED(4);
37+
38+
private final static Map<Integer, CurrentSecuritySystemState> reverse;
39+
static {
40+
reverse = Arrays.stream(CurrentSecuritySystemState.values()).collect(Collectors
41+
.toMap(CurrentSecuritySystemState::getCode, t -> t));
42+
}
43+
44+
public static CurrentSecuritySystemState fromCode(Integer code) {
45+
return reverse.get(code);
46+
}
47+
48+
private final int code;
49+
50+
CurrentSecuritySystemState(int code) {
51+
this.code = code;
52+
}
53+
54+
public int getCode() {
55+
return code;
56+
}
57+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.beowulfe.hap.accessories.properties;
2+
3+
import com.beowulfe.hap.accessories.SecuritySystem;
4+
5+
import java.util.Arrays;
6+
import java.util.Map;
7+
import java.util.stream.Collectors;
8+
9+
/**
10+
* Type of alarm of a {@link SecuritySystem}.
11+
*
12+
* @author Gaston Dombiak
13+
*/
14+
public enum SecuritySystemAlarmType {
15+
16+
/**
17+
* Alarm conditions are cleared
18+
*/
19+
CLEARED(0),
20+
/**
21+
* Alarm type is not known
22+
*/
23+
UNKNOWN(1);
24+
25+
private final static Map<Integer, SecuritySystemAlarmType> reverse;
26+
static {
27+
reverse = Arrays.stream(SecuritySystemAlarmType.values()).collect(Collectors
28+
.toMap(SecuritySystemAlarmType::getCode, t -> t));
29+
}
30+
31+
public static SecuritySystemAlarmType fromCode(Integer code) {
32+
return reverse.get(code);
33+
}
34+
35+
private final int code;
36+
37+
SecuritySystemAlarmType(int code) {
38+
this.code = code;
39+
}
40+
41+
public int getCode() {
42+
return code;
43+
}
44+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.beowulfe.hap.accessories.properties;
2+
3+
import com.beowulfe.hap.accessories.SecuritySystem;
4+
5+
import java.util.Arrays;
6+
import java.util.Map;
7+
import java.util.stream.Collectors;
8+
9+
/**
10+
* The target state of a {@link SecuritySystem}.
11+
*
12+
* @author Gaston Dombiak
13+
*/
14+
public enum TargetSecuritySystemState {
15+
16+
/**
17+
* The home is occupied and residents are active.
18+
*/
19+
STAY_ARM(0),
20+
/**
21+
* The home is unoccupied.
22+
*/
23+
AWAY_ARM(1),
24+
/**
25+
* The home is occupied and residents are sleeping.
26+
*/
27+
NIGHT_ARM(2),
28+
/**
29+
* The security system is disarmed.
30+
*/
31+
DISARMED(3);
32+
33+
private final static Map<Integer, TargetSecuritySystemState> reverse;
34+
static {
35+
reverse = Arrays.stream(TargetSecuritySystemState.values()).collect(Collectors
36+
.toMap(TargetSecuritySystemState::getCode, t -> t));
37+
}
38+
39+
public static TargetSecuritySystemState fromCode(Integer code) {
40+
return reverse.get(code);
41+
}
42+
43+
private final int code;
44+
45+
TargetSecuritySystemState(int code) {
46+
this.code = code;
47+
}
48+
49+
public int getCode() {
50+
return code;
51+
}
52+
}
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.securitysystem;
2+
3+
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
4+
import com.beowulfe.hap.accessories.SecuritySystem;
5+
import com.beowulfe.hap.accessories.properties.CurrentSecuritySystemState;
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 CurrentSecuritySystemStateCharacteristic extends EnumCharacteristic implements EventableCharacteristic {
12+
13+
private final SecuritySystem securitySystem;
14+
15+
public CurrentSecuritySystemStateCharacteristic(SecuritySystem securitySystem) {
16+
super("00000066-0000-1000-8000-0026BB765291", false, true, "Current security system state", 4);
17+
this.securitySystem = securitySystem;
18+
}
19+
20+
@Override
21+
protected CompletableFuture<Integer> getValue() {
22+
return securitySystem.getCurrentSecuritySystemState().thenApply(CurrentSecuritySystemState::getCode);
23+
}
24+
25+
@Override
26+
protected void setValue(Integer value) throws Exception {
27+
//Not writable
28+
}
29+
30+
@Override
31+
public void subscribe(HomekitCharacteristicChangeCallback callback) {
32+
securitySystem.subscribeCurrentSecuritySystemState(callback);
33+
}
34+
35+
@Override
36+
public void unsubscribe() {
37+
securitySystem.unsubscribeCurrentSecuritySystemState();
38+
}
39+
}
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.securitysystem;
2+
3+
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
4+
import com.beowulfe.hap.accessories.SecuritySystem;
5+
import com.beowulfe.hap.accessories.properties.SecuritySystemAlarmType;
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 SecuritySystemAlarmTypeCharacteristic extends EnumCharacteristic implements EventableCharacteristic {
12+
13+
private final SecuritySystem securitySystem;
14+
15+
public SecuritySystemAlarmTypeCharacteristic(SecuritySystem securitySystem) {
16+
super("0000008E-0000-1000-8000-0026BB765291", false, true, "Security system alarm type", 1);
17+
this.securitySystem = securitySystem;
18+
}
19+
20+
@Override
21+
protected CompletableFuture<Integer> getValue() {
22+
return securitySystem.getAlarmTypeState().thenApply(SecuritySystemAlarmType::getCode);
23+
}
24+
25+
@Override
26+
protected void setValue(Integer value) throws Exception {
27+
//Not writable
28+
}
29+
30+
@Override
31+
public void subscribe(HomekitCharacteristicChangeCallback callback) {
32+
securitySystem.subscribeAlarmTypeState(callback);
33+
}
34+
35+
@Override
36+
public void unsubscribe() {
37+
securitySystem.unsubscribeAlarmTypeState();
38+
}
39+
}
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.securitysystem;
2+
3+
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
4+
import com.beowulfe.hap.accessories.SecuritySystem;
5+
import com.beowulfe.hap.accessories.properties.TargetSecuritySystemState;
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 TargetSecuritySystemStateCharacteristic extends EnumCharacteristic implements EventableCharacteristic {
12+
13+
private final SecuritySystem securitySystem;
14+
15+
public TargetSecuritySystemStateCharacteristic(SecuritySystem securitySystem) {
16+
super("00000067-0000-1000-8000-0026BB765291", true, true, "Target security system state", 3);
17+
this.securitySystem = securitySystem;
18+
}
19+
20+
@Override
21+
protected CompletableFuture<Integer> getValue() {
22+
return securitySystem.getTargetSecuritySystemState().thenApply(TargetSecuritySystemState::getCode);
23+
}
24+
25+
@Override
26+
protected void setValue(Integer value) throws Exception {
27+
securitySystem.setTargetSecuritySystemState(TargetSecuritySystemState.fromCode(value));
28+
}
29+
30+
@Override
31+
public void subscribe(HomekitCharacteristicChangeCallback callback) {
32+
securitySystem.subscribeTargetSecuritySystemState(callback);
33+
}
34+
35+
@Override
36+
public void unsubscribe() {
37+
securitySystem.unsubscribeTargetSecuritySystemState();
38+
}
39+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.beowulfe.hap.impl.services;
2+
3+
import com.beowulfe.hap.accessories.SecuritySystem;
4+
import com.beowulfe.hap.impl.characteristics.securitysystem.CurrentSecuritySystemStateCharacteristic;
5+
import com.beowulfe.hap.impl.characteristics.securitysystem.SecuritySystemAlarmTypeCharacteristic;
6+
import com.beowulfe.hap.impl.characteristics.securitysystem.TargetSecuritySystemStateCharacteristic;
7+
8+
public class SecuritySystemService extends AbstractServiceImpl {
9+
10+
public SecuritySystemService(SecuritySystem securitySystem) {
11+
super("0000007E-0000-1000-8000-0026BB765291", securitySystem);
12+
addCharacteristic(new CurrentSecuritySystemStateCharacteristic(securitySystem));
13+
addCharacteristic(new TargetSecuritySystemStateCharacteristic(securitySystem));
14+
addCharacteristic(new SecuritySystemAlarmTypeCharacteristic(securitySystem));
15+
}
16+
}

0 commit comments

Comments
 (0)