generated from homebridge/homebridge-plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplatform.ts
237 lines (189 loc) · 8.4 KB
/
platform.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import { API, DynamicPlatformPlugin, Logger, PlatformAccessory, PlatformConfig, Service, Characteristic } from 'homebridge';
import { PLATFORM_NAME, PLUGIN_NAME } from './settings';
import { EltakoSwitchAccessory } from './EltakoSwitchAccessory';
import { EltakoBlindsAccessory } from './EltakoBlindsAccessory';
import { EltakoDimmerAccessory } from './EltakoDimmerAccessory';
import { MiniSafe2Api } from './MiniSafe2Api';
import { Device } from './models';
import { getChangedDeviceAddresses } from './StateChangeDetector';
import { IUpdatableAccessory } from './IUpdatableAccessory';
/**
* HomebridgePlatform
* This class is the main constructor for your plugin, this is where you should
* parse the user config and discover/register accessories with Homebridge.
*/
export class EltakoMiniSafe2Platform implements DynamicPlatformPlugin {
public readonly Service: typeof Service = this.api.hap.Service;
public readonly Characteristic: typeof Characteristic = this.api.hap.Characteristic;
// this is used to track restored cached accessories
public readonly accessories: PlatformAccessory[] = [];
public readonly accessoryInstances: IUpdatableAccessory[] = [];
public miniSafe!: MiniSafe2Api;
public deviceStateCache: Device[] = [];
constructor(
public readonly log: Logger,
public readonly config: PlatformConfig,
public readonly api: API,
) {
this.log.debug('Finished initializing platform:', this.config.name);
// When this event is fired it means Homebridge has restored all cached accessories from disk.
// Dynamic Platform plugins should only register new accessories after this event was fired,
// in order to ensure they weren't added to homebridge already. This event can also be used
// to start discovery of new accessories.
this.api.on('didFinishLaunching', async () => {
this.log.info('Connecting to Eltako MiniSafe2 on ' + this.config.ip);
try {
this.miniSafe = new MiniSafe2Api(this.config.ip, this.config.password, this.config.accessToken);
await this.updateDeviceStateCache();
await this.discoverDevices();
} catch (e) {
if (typeof e === 'string') {
this.log.error(e);
} else if (e instanceof Error) {
this.log.error(e.message);
}
}
while (this.miniSafe) {
await this.delay(this.config.queryLoopDelay * 1000);
try {
await this.updateDeviceStateCache();
} catch (e) {
if (typeof e === 'string') {
this.log.error(e);
} else if (e instanceof Error) {
this.log.error(e.message);
}
}
}
});
}
/**
* This function is invoked when homebridge restores cached accessories from disk at startup.
* It should be used to setup event handlers for characteristics and update respective values.
*/
configureAccessory(accessory: PlatformAccessory) {
this.log.info('Loading accessory from cache:', accessory.displayName);
// add the restored accessory to the accessories cache so we can track if it has already been registered
this.accessories.push(accessory);
}
async updateDeviceStateCache() {
this.log.info('Updating device state cache ...');
const oldCache = this.deviceStateCache;
const newCache = await this.miniSafe.getStates();
this.deviceStateCache = newCache;
// find the changed devices and force them to update
const changedAddresses = getChangedDeviceAddresses(oldCache, newCache);
for (const changedAddress of changedAddresses) {
this.log.info(`Device ${changedAddress} did change`);
const uuid = this.api.hap.uuid.generate(changedAddress);
const existingAccessory = this.accessoryInstances.find(accessory => accessory.accessory.UUID === uuid);
if (existingAccessory) {
this.log.info(`Forcing update ${existingAccessory.accessory.displayName}`);
existingAccessory.update();
}
}
}
/**
* This is an example method showing how to register discovered accessories.
* Accessories must only be registered once, previously created accessories
* must not be registered again to prevent "duplicate UUID" errors.
*/
async discoverDevices() {
this.log.info('Discovering devices ...');
let systemConfig;
try {
systemConfig = await this.miniSafe.getSystems();
} catch (e) {
if (typeof e === 'string') {
this.log.error(e);
} else if (e instanceof Error) {
this.log.error(e.message);
}
}
if (!systemConfig) {
return;
}
this.log.info('Found ' + systemConfig.devices.length + ' device(s)');
// loop over the discovered devices and register each one if it has not already been registered
for (const device of systemConfig.devices) {
const deviceType = device.info.data;
if (device.info.address === '' || deviceType === '') {
this.log.warn('Could not find the unique address or device type for device: ' + JSON.stringify(device));
break;
}
// generate a unique id for the accessory this should be generated from
// something globally unique, but constant, for example, the device serial
// number or MAC address
const uuid = this.api.hap.uuid.generate(device.info.address);
// see if an accessory with the same uuid has already been registered and restored from
// the cached devices we stored in the `configureAccessory` method above
const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid);
if (existingAccessory) {
// if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
// existingAccessory.context.device = device;
// this.api.updatePlatformAccessories([existingAccessory]);
// create the accessory handler for the restored accessory
// this is imported from `platformAccessory.ts`
let instance: IUpdatableAccessory | null = null;
switch (deviceType) {
case 'eltako_blind':
case 'eltako_tf_blind': {
instance = new EltakoBlindsAccessory(this, existingAccessory);
break;
}
case 'eltako_switch': {
instance = new EltakoSwitchAccessory(this, existingAccessory);
break;
}
case 'eltako_dimmer': {
instance = new EltakoDimmerAccessory(this, existingAccessory);
break;
}
}
if (instance) {
this.accessoryInstances.push(instance);
}
// the accessory already exists
this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName);
// it is possible to remove platform accessories at any time using `api.unregisterPlatformAccessories`, eg.:
// remove platform accessories when no longer present
// this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [existingAccessory]);
// this.log.info('Removing existing accessory from cache:', existingAccessory.displayName);
} else {
// create a new accessory
const accessory = new this.api.platformAccessory(device.name, uuid);
// store a copy of the device object in the `accessory.context`
// the `context` property can be used to store any data about the accessory you may need
accessory.context.device = device;
let instance: IUpdatableAccessory | null = null;
// create the accessory handler for the newly create accessory
// this is imported from `platformAccessory.ts`
switch (deviceType) {
case 'eltako_blind':
case 'eltako_tf_blind': {
instance = new EltakoBlindsAccessory(this, accessory);
break;
}
case 'eltako_switch': {
instance = new EltakoSwitchAccessory(this, accessory);
break;
}
case 'eltako_dimmer': {
instance = new EltakoDimmerAccessory(this, accessory);
break;
}
}
if (instance) {
this.accessoryInstances.push(instance);
}
// the accessory does not yet exist, so we need to create it
this.log.info('Adding new accessory:', device.name);
// link the accessory to your platform
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
}
}
delay(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
}