generated from homebridge/homebridge-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
platformAccessory.ts
129 lines (105 loc) · 5.62 KB
/
platformAccessory.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
import { Service, PlatformAccessory } from 'homebridge';
import { Aranet4Platform } from './platform';
import { Aranet4Device, AranetData } from './aranet';
/**
* Platform Accessory
* An instance of this class is created for each accessory your platform registers
* Each accessory may expose multiple services of different service types.
*/
export class Aranet4Accessory {
// https://developers.homebridge.io/#/service/HumiditySensor
private humidityService: Service;
// https://developers.homebridge.io/#/service/TemperatureSensor
private temperatureService: Service;
// https://developers.homebridge.io/#/service/CarbonDioxideSensor
private co2Service: Service;
private readonly services: Service[];
constructor(
private readonly platform: Aranet4Platform,
private readonly accessory: PlatformAccessory,
private readonly device: Aranet4Device,
) {
// set accessory information
this.accessory.getService(this.platform.Service.AccessoryInformation)!
.setCharacteristic(this.platform.Characteristic.Manufacturer, device.info.manufacturer)
.setCharacteristic(this.platform.Characteristic.Model, device.info.modelNumber)
.setCharacteristic(this.platform.Characteristic.SerialNumber, device.info.serialNumber)
.setCharacteristic(this.platform.Characteristic.FirmwareRevision, device.info.firmwareRevision);
this.humidityService = this.accessory.getService(this.platform.Service.HumiditySensor) ||
this.accessory.addService(this.platform.Service.HumiditySensor);
this.temperatureService = this.accessory.getService(this.platform.Service.TemperatureSensor) ||
this.accessory.addService(this.platform.Service.TemperatureSensor);
this.co2Service = this.accessory.getService(this.platform.Service.CarbonDioxideSensor) ||
this.accessory.addService(this.platform.Service.CarbonDioxideSensor);
this.services = [
this.humidityService,
this.temperatureService,
this.co2Service,
];
// set the service name, this is what is displayed as the default name on the Home app
// in this example we are using the name we stored in the `accessory.context` in the `discoverDevices` method.
// this.service.setCharacteristic(this.platform.Characteristic.Name, accessory.context.device.exampleDisplayName);
// each service must implement at-minimum the "required characteristics" for the given service type
// see https://developers.homebridge.io/#/service/Lightbulb
/**
* Creating multiple services of the same type.
*
* To avoid "Cannot add a Service with the same UUID another Service without also defining a unique 'subtype' property." error,
* when creating multiple services of the same type, you need to use the following syntax to specify a name and subtype id:
* this.accessory.getService('NAME') || this.accessory.addService(this.platform.Service.Lightbulb, 'NAME', 'USER_DEFINED_SUBTYPE_ID');
*
* The USER_DEFINED_SUBTYPE must be unique to the platform accessory (if you platform exposes multiple accessories, each accessory
* can use the same sub type id.)
*/
// Example: add two "motion sensor" services to the accessory
// const motionSensorOneService = this.accessory.getService('Motion Sensor One Name') ||
// this.accessory.addService(this.platform.Service.MotionSensor, 'Motion Sensor One Name', 'YourUniqueIdentifier-1');
// const motionSensorTwoService = this.accessory.getService('Motion Sensor Two Name') ||
// this.accessory.addService(this.platform.Service.MotionSensor, 'Motion Sensor Two Name', 'YourUniqueIdentifier-2');
/**
* Updating characteristics values asynchronously.
*
* Example showing how to update the state of a Characteristic asynchronously instead
* of using the `on('get')` handlers.
* Here we change update the motion sensor trigger states on and off every 10 seconds
* the `updateCharacteristic` method.
*
*/
setInterval(async () => {
await this.updateSensorData();
}, this.platform.config.sensorDataRefreshInterval * 1000);
}
async updateSensorData() {
try {
let data: AranetData;
try {
data = await this.device.getSensorData(this.platform.config.bluetoothReadyTimeout);
} catch (err) {
this.platform.log.error('could not get sensor data ' + err);
return;
}
let batteryLevel = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
if (data.battery <= this.platform.config.batteryAlertThreshold) {
batteryLevel = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
}
this.services.forEach(s => {
s.updateCharacteristic(
this.platform.Characteristic.StatusLowBattery,
batteryLevel,
);
});
// push the new value to HomeKit
this.humidityService.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, data.humidity);
this.temperatureService.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, data.temperature);
let co2level = this.platform.Characteristic.CarbonDioxideDetected.CO2_LEVELS_NORMAL;
if (data.co2 >= this.platform.config.co2AlertThreshold) {
co2level = this.platform.Characteristic.CarbonDioxideDetected.CO2_LEVELS_ABNORMAL;
}
this.co2Service.updateCharacteristic(this.platform.Characteristic.CarbonDioxideDetected, co2level);
this.co2Service.updateCharacteristic(this.platform.Characteristic.CarbonDioxideLevel, data.co2);
this.platform.log.debug('Updated data:', data);
} catch (err) {
this.platform.log.error('could not update sensor data: ', err);
}
}
}