generated from homebridge/homebridge-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlights.ts
115 lines (104 loc) · 5.26 KB
/
lights.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
import { Logger, Service, PlatformAccessory, CharacteristicValue, PlatformConfig } from 'homebridge';
import { RiseGardenPlatform } from './platform';
import { RiseGardenAPI } from './api';
/**
* 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 RiseGardenLights {
private service: Service;
constructor(
private readonly platform: RiseGardenPlatform,
private readonly accessory: PlatformAccessory,
private readonly config: PlatformConfig,
private readonly log: Logger,
) {
this.log.debug('initializing RiseGardenLights');
this.accessory.getService(this.platform.Service.AccessoryInformation)!
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'Rise-Gardens')
.setCharacteristic(this.platform.Characteristic.Model, 'Floor-Unit')
.setCharacteristic(this.platform.Characteristic.SerialNumber, 'Default-Serial');
this.service = this.accessory.getService(this.platform.Service.Lightbulb) || this.accessory.addService(this.platform.Service.Lightbulb);
this.service.setCharacteristic(this.platform.Characteristic.Name, accessory.context.device.name);
// 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)
.onSet(this.setOn.bind(this)) // SET - bind to the `setOn` method below
.onGet(this.getOn.bind(this)); // GET - bind to the `getOn` method below
// register handlers for the Brightness Characteristic
this.service.getCharacteristic(this.platform.Characteristic.Brightness)
.onSet(this.setBrightness.bind(this)) // SET - bind to the 'setBrightness` method below
.onGet(this.getBrightness.bind(this)); // GET - bind to the 'getBrightness' method below
}
/**
* Handle "SET" requests from HomeKit
* These are sent when the user changes the state of an accessory, for example, turning on a Light bulb.
*/
async setOn(value: CharacteristicValue): Promise<void> {
this.log.debug('called setOn:', value);
const target = value ? 100 : 0;
this.setBrightness(target);
}
/**
* 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)
*/
async getOn(): Promise<CharacteristicValue> {
this.log.debug('called getOn');
try {
const api = new RiseGardenAPI(this.config, this.log);
const isOn = await api.getLightLevel(this.accessory.context.device.id) > 0;
this.log.debug('Get Characteristic On ->', isOn);
return isOn;
} catch (err) {
this.log.info('Error getting light state via Rise API');
this.log.debug('Error getting on:', err);
// if you need to return an error to show the device as "Not Responding" in the Home app:
throw new this.platform.api.hap.HapStatusError(this.platform.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE);
}
}
/**
* Handle "SET" requests from HomeKit
* These are sent when the user changes the state of an accessory, for example, changing the Brightness
*/
async setBrightness(value: CharacteristicValue): Promise<void> {
this.log.debug('called setBrightness:', value);
try {
const api = new RiseGardenAPI(this.config, this.log);
await api.setLightLevel(this.accessory.context.device.id, value as number);
} catch (err) {
this.log.info('Error setting brightness via Rise API');
this.log.debug('Error setting brightness:', err);
// if you need to return an error to show the device as "Not Responding" in the Home app:
throw new this.platform.api.hap.HapStatusError(this.platform.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE);
}
}
/**
* 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.
*/
async getBrightness(): Promise<CharacteristicValue> {
this.log.debug('called getBrightness');
try {
const api = new RiseGardenAPI(this.config, this.log);
const brightness = await api.getLightLevel(this.accessory.context.device.id);
this.log.debug('Get Characteristic Brightness ->', brightness);
return brightness;
} catch (err) {
this.log.info('Error getting light brightness via Rise API');
this.log.debug('Error getting brightness:', err);
// if you need to return an error to show the device as "Not Responding" in the Home app:
throw new this.platform.api.hap.HapStatusError(this.platform.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE);
}
}
}