forked from vieira/homebridge-yeelight-wifi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform.js
164 lines (141 loc) · 4.8 KB
/
platform.js
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
const dgram = require('dgram');
const YeeBulb = require('./bulbs/bulb');
const Brightness = require('./bulbs/brightness');
const MoonlightMode = require('./bulbs/moonlight');
const Color = require('./bulbs/color');
const Temperature = require('./bulbs/temperature');
const { getDeviceId, getName, blacklist, sleep, pipe } = require('./utils');
const MODELS = {
MONO: 'mono', // Color Temperature Bulb
COLOR: 'color', // RGB Bulb
STRIPE: 'stripe', // LED Stripe
CEILING: 'ceiling', // Ceiling lights
CEILC: 'ceilc', // Ceiling light Yeelight 450c
BSLAMP: 'bslamp', // Bed side lamp
LAMP: 'lamp', // Star Lamp etc.
};
class YeePlatform {
constructor(log, config, api) {
if (!api) return;
log.debug(`starting YeePlatform using homebridge API v${api.version}`);
this.searchMessage = Buffer.from(
['M-SEARCH * HTTP/1.1', 'MAN: "ssdp:discover"', 'ST: wifi_bulb'].join(
global.EOL
)
);
this.addr = '239.255.255.250';
this.port = 1982;
this.log = log;
this.config = config;
this.sock = dgram.createSocket('udp4');
this.devices = {};
this.sock.bind(this.port, () => {
this.sock.setBroadcast(true);
this.sock.setMulticastTTL(128);
this.sock.addMembership(this.addr);
const multicastInterface =
config && config.multicast && config.multicast.interface;
if (multicastInterface) {
this.sock.setMulticastInterface(multicastInterface);
}
});
this.api = api;
this.api.on('didFinishLaunching', async () => {
this.sock.on('message', this.handleMessage.bind(this));
log(`Searching for known devices...`);
do {
this.search();
// eslint-disable-next-line no-await-in-loop
await sleep(15000);
} while (
Object.values(this.devices).some((accessory) => !accessory.initialized)
);
log(`All known devices found. Stopping proactive search.`);
});
}
configureAccessory(accessory) {
this.log(`Loaded accessory ${accessory.displayName}.`);
accessory.initialized = false;
this.devices[accessory.context.did] = accessory;
}
search() {
this.log('Sending search request...');
this.sock.send(
this.searchMessage,
0,
this.searchMessage.length,
this.port,
this.addr
);
}
handleMessage(message) {
const headers = {};
const [method, ...kvs] = message.toString().split(global.EOL);
if (method.startsWith('M-SEARCH')) return;
kvs.forEach((kv) => {
const [k, v] = kv.split(': ');
headers[k] = v;
});
const endpoint = headers.Location.split('//')[1];
this.log(`Received advertisement from ${getDeviceId(headers.id)}.`);
this.buildDevice(endpoint, headers);
}
buildDevice(endpoint, { id, model, support, ...props }) {
const deviceId = getDeviceId(id);
const name = getName(`${model}-${deviceId}`, this.config);
const hidden = blacklist(deviceId, this.config);
let accessory = this.devices[id];
if (hidden === true) {
this.log.debug(`Device ${name} is blacklisted, ignoring...`);
try {
delete this.devices[id];
this.api.unregisterPlatformAccessories(
'homebridge-yeelight',
'yeelight',
[accessory]
);
this.log(`Device ${name} was unregistered`);
// eslint-disable-next-line no-empty
} catch (_) {}
return;
}
const features = support
.split(' ')
.concat(Object.keys(props))
.filter((f) => !hidden.includes(f));
if (!accessory) {
this.log(`Initializing new accessory ${id} with name ${name}...`);
const uuid = global.UUIDGen.generate(id);
accessory = new global.Accessory(name, uuid);
accessory.context.did = id;
accessory.context.model = model;
this.devices[id] = accessory;
this.api.registerPlatformAccessories('homebridge-yeelight', 'yeelight', [
accessory,
]);
}
if (accessory && accessory.initialized) return;
const mixins = [];
const family = Object.values(MODELS).find((fam) => model.startsWith(fam));
// Lamps that support moonlight mode
if ([MODELS.CEILING, MODELS.LAMP, MODELS.CEILC].includes(family)) {
this.log(`Device ${name} supports moonlight mode`);
mixins.push(MoonlightMode);
}
if (features.includes('set_bright')) {
this.log(`Device ${name} supports brightness`);
mixins.push(Brightness);
}
if (features.includes('set_hsv')) {
this.log(`Device ${name} supports color`);
mixins.push(Color);
}
if (features.includes('set_ct_abx')) {
this.log(`Device ${name} supports color temperature`);
mixins.push(Temperature);
}
const Bulb = class extends pipe(...mixins)(YeeBulb) {};
return new Bulb({ id, name, model, endpoint, accessory, ...props }, this);
}
}
module.exports = YeePlatform;