/
platform.ts
190 lines (162 loc) · 4.95 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
import {
API,
DynamicPlatformPlugin,
Logger,
PlatformAccessory,
PlatformConfig,
Service,
Characteristic,
UnknownContext,
} from 'homebridge';
import ping from 'ping';
import { inspect } from 'util';
import { PLATFORM_NAME, PLUGIN_NAME } from './settings';
import { IrBlaster } from './IrBlaster';
import {
LightbulbAccessory,
LightbulbAccessoryConfig,
ILightbulbAccessoryContext,
} from './platformLightbulb';
import {
FanAccessory,
FanAccessoryConfig,
IFanAccessoryContext,
} from './platformFan';
export type BlasterConfig = {
uniqueId: string;
displayName: string;
address: string;
};
interface IRBlasterPlatformConfig extends PlatformConfig {
blasters?: BlasterConfig[];
accessoires?: (LightbulbAccessoryConfig | FanAccessoryConfig)[];
}
/**
* @todo platform should be using StaticPlatformPlugin?
*/
export class ExampleHomebridgePlatform implements DynamicPlatformPlugin {
public readonly Service: typeof Service = this.api.hap.Service;
public readonly Characteristic: typeof Characteristic =
this.api.hap.Characteristic;
public readonly blasters: IrBlaster[] = [];
public readonly accessories: PlatformAccessory<{
irBlaster: IrBlaster;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
commands: any;
}>[] = [];
constructor(
public readonly log: Logger,
public readonly config: IRBlasterPlatformConfig,
public readonly api: API
) {
this.log.debug('Finished initializing platform:', this.config);
this.blasters = (this.config.blasters ?? []).map(
(blasterConfig) => new IrBlaster(log, blasterConfig)
);
this.api.on('didFinishLaunching', () => {
log.debug('Executed didFinishLaunching callback');
this.discoverDevices();
});
}
configureAccessory(
accessory: PlatformAccessory<
ILightbulbAccessoryContext | IFanAccessoryContext
>
) {
this.log.info('Loading accessory from cache:', accessory.displayName);
this.accessories.push(accessory);
}
async discoverIrBlasters() {
for (const blaster of this.blasters) {
this.log.debug('Discovering IR Blaster:', blaster.config.displayName);
const pingResponse = await ping.promise.probe(blaster.config.address, {
timeout: 60,
});
if (!pingResponse.alive) {
throw new Error(
`IR Blaster at ${blaster.config.address} is not responding.`
);
}
this.log.debug(
`Blaster at ${blaster.config.address} is alive (ping: ${pingResponse.avg}ms).`
);
}
}
async discoverDevices() {
await this.discoverIrBlasters();
for (const accessory of this.config.accessoires ?? []) {
this.log.debug(
'Accessory',
inspect(accessory, { depth: 0, colors: true })
);
const uuid = this.api.hap.uuid.generate(accessory.uniqueId);
const irBlaster = this.getBlasterByUniqueId(accessory.blaster.uniqueId);
const existingAccessory = this.accessories.find(
(accessory) => accessory.UUID === uuid
);
this.log.debug(
`Existing Accessory "${accessory.displayName}":`,
existingAccessory ? 'found' : 'not found'
);
if (existingAccessory) {
this.log.info(
'Restoring existing accessory from cache:',
existingAccessory.displayName
);
existingAccessory.context.irBlaster = irBlaster;
existingAccessory.context.commands = accessory.commands;
this.api.updatePlatformAccessories([existingAccessory]);
switch (accessory.type) {
case 'Lightbulb': {
new LightbulbAccessory(this, existingAccessory);
break;
}
case 'Fan': {
new FanAccessory(this, existingAccessory);
break;
}
}
} else {
this.log.info(
`Adding new ${accessory.type} accessory:`,
accessory.displayName
);
const newAaccessory = new this.api.platformAccessory<UnknownContext>(
accessory.displayName,
uuid
);
newAaccessory.context.irBlaster = irBlaster;
newAaccessory.context.commands = accessory.commands;
switch (accessory.type) {
case 'Lightbulb': {
new LightbulbAccessory(
this,
newAaccessory as PlatformAccessory<ILightbulbAccessoryContext>
);
break;
}
case 'Fan': {
new FanAccessory(
this,
newAaccessory as PlatformAccessory<IFanAccessoryContext>
);
break;
}
}
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [
newAaccessory,
]);
}
}
}
getBlasterByUniqueId(_uniqueId: string) {
const found = this.blasters.find(
({ config: { uniqueId } }) => uniqueId === _uniqueId
);
if (!found) {
throw new Error(`Could not find blaster with uniqueId "${_uniqueId}"`);
} else {
return found;
}
}
}