-
-
Notifications
You must be signed in to change notification settings - Fork 628
/
Copy pathAirConditioner_accessory.ts
159 lines (136 loc) · 6.45 KB
/
AirConditioner_accessory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// In This example we create an air conditioner Accessory that Has a Thermostat linked to a Fan Service.
// For example, I've also put a Light Service that should be hidden to represent a light in the closet that is part of the AC.
// It is to show how to hide services.
// The linking and Hiding does NOT appear to be reflected in Home
// here's a fake hardware device that we'll expose to HomeKit
import {
Accessory,
AccessoryEventTypes,
Categories,
Characteristic,
CharacteristicEventTypes,
CharacteristicGetCallback,
CharacteristicSetCallback,
CharacteristicValue,
Service,
uuid,
} from "..";
import { VoidCallback } from "../types";
const ACTest_data: Record<string, CharacteristicValue> = {
fanPowerOn: false,
rSpeed: 100,
CurrentHeatingCoolingState: 1,
TargetHeatingCoolingState: 1,
CurrentTemperature: 33,
TargetTemperature: 32,
TemperatureDisplayUnits: 1,
LightOn: false,
};
// This is the Accessory that we'll return to HAP-NodeJS that represents our fake fan.
const ACTest = exports.accessory = new Accessory("Air Conditioner", uuid.generate("hap-nodejs:accessories:airconditioner"));
// Add properties for publishing (in case we're using Core.js and not BridgedCore.js)
// @ts-expect-error: Core/BridgeCore API
ACTest.username = "1A:2B:3C:4D:5E:FF";
// @ts-expect-error: Core/BridgeCore API
ACTest.pincode = "031-45-154";
ACTest.category = Categories.THERMOSTAT;
// set some basic properties (these values are arbitrary and setting them is optional)
ACTest
.getService(Service.AccessoryInformation)!
.setCharacteristic(Characteristic.Manufacturer, "Sample Company");
// listen for the "identify" event for this Accessory
ACTest.on(AccessoryEventTypes.IDENTIFY, (paired: boolean, callback: VoidCallback) => {
console.log("Fan Identified!");
callback(); // success
});
// Add the actual Fan Service and listen for change events from iOS.
const FanService = ACTest.addService(Service.Fan, "Blower"); // services exposed to the user should have "names" like "Fake Light" for us
FanService.getCharacteristic(Characteristic.On)!
.on(CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => {
console.log("Fan Power Changed To "+value);
ACTest_data.fanPowerOn=value;
callback(); // Our fake Fan is synchronous - this value has been successfully set
});
// We want to intercept requests for our current power state so we can query the hardware itself instead of
// allowing HAP-NodeJS to return the cached Characteristic.value.
FanService.getCharacteristic(Characteristic.On)!
.on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => {
// this event is emitted when you ask Siri directly whether your fan is on or not. you might query
// the fan hardware itself to find this out, then call the callback. But if you take longer than a
// few seconds to respond, Siri will give up.
const err = null; // in case there were any problems
if (ACTest_data.fanPowerOn) {
callback(err, true);
} else {
callback(err, false);
}
});
// also add an "optional" Characteristic for speed
FanService.addCharacteristic(Characteristic.RotationSpeed)
.on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => {
callback(null, ACTest_data.rSpeed);
})
.on(CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => {
console.log("Setting fan rSpeed to %s", value);
ACTest_data.rSpeed=value;
callback();
});
const ThermostatService = ACTest.addService(Service.Thermostat, "Thermostat");
ThermostatService.addLinkedService(FanService);
ThermostatService.setPrimaryService();
ThermostatService.getCharacteristic(Characteristic.CurrentHeatingCoolingState)!
.on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => {
callback(null, ACTest_data.CurrentHeatingCoolingState);
})
.on(CharacteristicEventTypes.SET,(value: CharacteristicValue, callback: CharacteristicSetCallback) => {
ACTest_data.CurrentHeatingCoolingState=value;
console.log( "Characteristic CurrentHeatingCoolingState changed to %s",value);
callback();
});
ThermostatService.getCharacteristic(Characteristic.TargetHeatingCoolingState)!
.on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => {
callback(null, ACTest_data.TargetHeatingCoolingState);
})
.on(CharacteristicEventTypes.SET,(value: CharacteristicValue, callback: CharacteristicSetCallback) => {
ACTest_data.TargetHeatingCoolingState=value;
console.log( "Characteristic TargetHeatingCoolingState changed to %s",value);
callback();
});
ThermostatService.getCharacteristic(Characteristic.CurrentTemperature)!
.on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => {
callback(null, ACTest_data.CurrentTemperature);
})
.on(CharacteristicEventTypes.SET,(value: CharacteristicValue, callback: CharacteristicSetCallback) => {
ACTest_data.CurrentTemperature=value;
console.log( "Characteristic CurrentTemperature changed to %s",value);
callback();
});
ThermostatService.getCharacteristic(Characteristic.TargetTemperature)!
.on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => {
callback(null, ACTest_data.TargetTemperature);
})
.on(CharacteristicEventTypes.SET,(value: CharacteristicValue, callback: CharacteristicSetCallback) => {
ACTest_data.TargetTemperature=value;
console.log( "Characteristic TargetTemperature changed to %s",value);
callback();
});
ThermostatService.getCharacteristic(Characteristic.TemperatureDisplayUnits)!
.on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => {
callback(null, ACTest_data.TemperatureDisplayUnits);
})
.on(CharacteristicEventTypes.SET,(value: CharacteristicValue, callback: CharacteristicSetCallback) => {
ACTest_data.TemperatureDisplayUnits=value;
console.log( "Characteristic TemperatureDisplayUnits changed to %s",value);
callback();
});
const LightService = ACTest.addService(Service.Lightbulb, "AC Light");
LightService.getCharacteristic(Characteristic.On)!
.on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => {
callback(null, ACTest_data.LightOn);
})
.on(CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => {
ACTest_data.LightOn=value;
console.log( "Characteristic Light On changed to %s",value);
callback();
});
LightService.setHiddenService();