generated from homebridge/homebridge-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
platformAccessory.ts
130 lines (102 loc) · 5.46 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
130
import { Service, PlatformAccessory, CharacteristicValue, CharacteristicSetCallback, CharacteristicGetCallback } from 'homebridge';
import { ExampleHomebridgePlatform } from './platform';
/**
* 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 ExamplePlatformAccessory {
private service: Service;
/**
* These are just used to create a working example
* You should implement your own code to track the state of your accessory
*/
private exampleStates = {
On: false,
Brightness: 100,
}
constructor(
private readonly platform: ExampleHomebridgePlatform,
private readonly accessory: PlatformAccessory,
) {
// set accessory information
this.accessory.getService(this.platform.Service.AccessoryInformation)!
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'Default-Manufacturer')
.setCharacteristic(this.platform.Characteristic.Model, 'Default-Model')
.setCharacteristic(this.platform.Characteristic.SerialNumber, 'Default-Serial');
// get the LightBulb service if it exists, otherwise create a new LightBulb service
// you can create multiple services for each accessory
this.service = this.accessory.getService(this.platform.Service.Lightbulb) || this.accessory.addService(this.platform.Service.Lightbulb);
// 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');
// 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
// register handlers for the On/Off Characteristic
this.service.getCharacteristic(this.platform.Characteristic.On)
.on('set', this.setOn.bind(this)) // SET - bind to the `setOn` method below
.on('get', this.getOn.bind(this)); // GET - bind to the `getOn` method below
// register handlers for the Brightness Characteristic
this.service.getCharacteristic(this.platform.Characteristic.Brightness)
.on('set', this.setBrightness.bind(this)); // SET - bind to the 'setBrightness` method below
// EXAMPLE ONLY
// Example showing how to update the state of a Characteristic asynchronously instead
// of using the `on('get')` handlers.
//
// Here we change update the brightness to a random value every 5 seconds using
// the `updateCharacteristic` method.
setInterval(() => {
// assign the current brightness a random value between 0 and 100
const currentBrightness = Math.floor(Math.random() * 100);
// push the new value to HomeKit
this.service.updateCharacteristic(this.platform.Characteristic.Brightness, currentBrightness);
this.platform.log.debug('Pushed updated current Brightness state to HomeKit:', currentBrightness);
}, 10000);
}
/**
* Handle "SET" requests from HomeKit
* These are sent when the user changes the state of an accessory, for example, turning on a Light bulb.
*/
setOn(value: CharacteristicValue, callback: CharacteristicSetCallback) {
// implement your own code to turn your device on/off
this.exampleStates.On = value as boolean;
this.platform.log.debug('Set Characteristic On ->', value);
// you must call the callback function
callback(null);
}
/**
* Handle the "GET" requests from HomeKit
* These are sent when HomeKit wants to know the current state of the accessory, for example, checking if a Light bulb is on.
*
* GET requests should return as fast as possbile. A long delay here will result in
* HomeKit being unresponsive and a bad user experience in general.
*
* If your device takes time to respond you should update the status of your device
* asynchronously instead using the `updateCharacteristic` method instead.
* @example
* this.service.updateCharacteristic(this.platform.Characteristic.On, true)
*/
getOn(callback: CharacteristicGetCallback) {
// implement your own code to check if the device is on
const isOn = this.exampleStates.On;
this.platform.log.debug('Get Characteristic On ->', isOn);
// you must call the callback function
// the first argument should be null if there were no errors
// the second argument should be the value to return
callback(null, isOn);
}
/**
* Handle "SET" requests from HomeKit
* These are sent when the user changes the state of an accessory, for example, changing the Brightness
*/
setBrightness(value: CharacteristicValue, callback: CharacteristicSetCallback) {
// implement your own code to set the brightness
this.exampleStates.Brightness = value as number;
this.platform.log.debug('Set Characteristic Brightness -> ', value);
// you must call the callback function
callback(null);
}
}