This repository was archived by the owner on Nov 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.js
More file actions
248 lines (248 loc) · 8.65 KB
/
index.js
File metadata and controls
248 lines (248 loc) · 8.65 KB
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
238
239
240
241
242
243
244
245
246
247
248
var Service, Characteristic;
const mqtt = require('mqtt');
const EventEmitter = require('events');
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-dyson", "dyson-coollink", CoolLink);
homebridge.registerAccessory("homebridge-dyson", "dyson-hotcoollink", HotCoolLink);
}
function CoolLink(log, config) {
this.log = log;
this.name = config['name'];
this.ip = config['ip'];
this.username = config["username"];
this.password = config["password"];
this.initConnection();
this.initCommonSensors();
this.initSpecificSensors();
}
CoolLink.prototype.initConnection = function() {
this.url = 'mqtt://' + this.ip;
this.options = {
keepalive: 10,
clientId: 'homebridge-dyson_' + Math.random().toString(16),
protocolId: 'MQTT',
protocolVersion: 4,
clean: true,
reconnectPeriod: 1000,
connectTimeout: 30 * 1000,
username: this.username,
password: this.password,
rejectUnauthorized: false
};
this.json_emitter = new EventEmitter();
var that = this;
this.mqtt_client = mqtt.connect(this.url, this.options);
this.mqtt_client.on('connect', function() {
that.mqtt_client.subscribe(that.getCurrentStatusTopic());
})
this.mqtt_client.on('message', function(topic, message) {
json = JSON.parse(message);
if (json !== null) {
if (json.msg === "ENVIRONMENTAL-CURRENT-SENSOR-DATA") {
that.json_emitter.emit('sensor', json);
}
if (json.msg === "CURRENT-STATE") {
that.json_emitter.emit('state', json);
}
}
});
}
CoolLink.prototype.initCommonSensors = function() {
// Temperature sensor
this.temperature_sensor = new Service.TemperatureSensor(this.name);
this.temperature_sensor
.getCharacteristic(Characteristic.CurrentTemperature)
.setProps({minValue: -50, maxValue: 100})
.on('get', this.getTemperature.bind(this));
// Humidity sensor
this.humidity_sensor = new Service.HumiditySensor(this.name);
this.humidity_sensor
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.setProps({minValue: 0, maxValue: 100})
.on('get', this.getRelativeHumidity.bind(this));
// Air Quality sensor
this.air_quality_sensor = new Service.AirQualitySensor(this.name);
this.air_quality_sensor
.getCharacteristic(Characteristic.AirQuality)
.on('get', this.getAirQuality.bind(this));
// Fan
this.fan = new Service.Fan(this.name);
this.fan
.getCharacteristic(Characteristic.On)
.on('get', this.isFanOn.bind(this))
.on('set', this.setFan.bind(this));
this.fan
.getCharacteristic(Characteristic.On)
.eventEnabled = true;
this.fan
.getCharacteristic(Characteristic.RotationSpeed)
.on('get', this.getFanRotationSpeed.bind(this))
.on('set', this.setFanRotationSpeed.bind(this));
// Rotation switch
this.rotation_switch = new Service.Switch("Rotation - " + this.name, "Rotation");
this.rotation_switch
.getCharacteristic(Characteristic.On)
.on('get', this.isRotationOn.bind(this))
.on('set', this.setRotation.bind(this));
}
CoolLink.prototype.initSpecificSensors = function() {
// Auto switch
this.auto_switch = new Service.Switch("Auto - " + this.name, "Auto");
this.auto_switch
.getCharacteristic(Characteristic.On)
.on('get', this.isAutoOn.bind(this))
.on('set', this.setAuto.bind(this));
this.auto_switch
.getCharacteristic(Characteristic.On)
.eventEnabled = true;
}
CoolLink.prototype.getServices = function() {
return [
this.temperature_sensor,
this.humidity_sensor,
this.air_quality_sensor,
this.fan,
this.auto_switch,
this.rotation_switch,
];
}
CoolLink.prototype.getMQTTPrefix = function() {
return "475";
}
CoolLink.prototype.getCurrentStatusTopic = function() {
return this.getMQTTPrefix() + '/' + this.username + '/status/current';
}
CoolLink.prototype.getCommandTopic = function() {
return this.getMQTTPrefix() + '/' + this.username + '/command';
}
CoolLink.prototype.requestCurrentState = function() {
if ((this.json_emitter.listenerCount('state') + this.json_emitter.listenerCount('sensor')) == 1) {
this.mqtt_client.publish(
this.getCommandTopic(),
'{"msg":"REQUEST-CURRENT-STATE"}'
);
}
}
CoolLink.prototype.getTemperature = function(callback) {
var that = this;
this.json_emitter.once('sensor', (json) => {
var temperature = parseFloat(json.data.tact) / 10 - 273.15;
that.log("Temperature:", temperature.toFixed(2));
callback(null, temperature);
});
this.requestCurrentState();
}
CoolLink.prototype.getRelativeHumidity = function(callback) {
var that = this;
this.json_emitter.once('sensor', (json) => {
var relative_humidity = parseInt(json.data.hact);
that.log("Humidity:", relative_humidity, "%");
callback(null, relative_humidity);
});
this.requestCurrentState();
}
CoolLink.prototype.getAirQuality = function(callback) {
var that = this;
this.json_emitter.once('sensor', (json) => {
var air_quality = Math.min(Math.max(Math.floor((parseInt(json.data.pact) + parseInt(json.data.vact)) / 2), 1), 5);
that.log("Air Quality:", air_quality);
callback(null, air_quality);
});
this.requestCurrentState();
}
CoolLink.prototype.isFanOn = function(callback) {
var that = this;
this.json_emitter.once('state', (json) => {
var fmod = json['product-state']['fmod'];
var on = (fmod === "FAN")
that.log("Fan:", on);
callback(null, on);
});
this.requestCurrentState();
}
CoolLink.prototype.setFan = function(value, callback) {
var that = this;
var now = new Date();
var fmod = value ? "FAN" : "OFF";
var message = '{"msg":"STATE-SET","time":"' + now.toISOString() + '","data":{"fmod":"' + fmod + '"}}';
this.mqtt_client.publish(
this.getCommandTopic(),
message
);
this.auto_switch.getCharacteristic(Characteristic.On).updateValue(false);
this.isFanOn(callback);
}
CoolLink.prototype.getFanRotationSpeed = function(callback) {
var that = this;
this.json_emitter.once('state', (json) => {
var fnsp = parseInt(json['product-state']['fnsp']);
var rotation_speed = fnsp * 10;
that.log("Fan Speed:", rotation_speed, '%');
callback(null, rotation_speed);
});
this.requestCurrentState();
}
CoolLink.prototype.setFanRotationSpeed = function(value, callback) {
var that = this;
var now = new Date();
var fnsp = Math.round(value / 10);
var message = '{"msg":"STATE-SET","time":"' + now.toISOString() + '","data":{"fnsp":"' + fnsp + '"}}'
this.mqtt_client.publish(
this.getCommandTopic(),
message
);
this.getFanRotationSpeed(callback);
}
CoolLink.prototype.isAutoOn = function(callback) {
var that = this;
this.json_emitter.once('state', (json) => {
var fmod = json['product-state']['fmod'];
var on = (fmod === "AUTO")
that.log("Auto:", on);
callback(null, on);
});
this.requestCurrentState();
}
CoolLink.prototype.setAuto = function(value, callback) {
var that = this;
var now = new Date();
var fmod = value ? "AUTO" : "OFF";
var message = '{"msg":"STATE-SET","time":"' + now.toISOString() + '","data":{"fmod":"' + fmod + '"}}';
this.mqtt_client.publish(
this.getCommandTopic(),
message
);
this.fan.getCharacteristic(Characteristic.On).updateValue(false);
this.isAutoOn(callback);
}
CoolLink.prototype.isRotationOn = function(callback) {
var that = this;
this.json_emitter.once('state', (json) => {
var oson = json['product-state']['oson'];
var on = (oson === "ON")
that.log("Rotation:", on);
callback(null, on);
});
this.requestCurrentState();
}
CoolLink.prototype.setRotation = function(value, callback) {
var that = this;
var now = new Date();
var oson = value ? "ON" : "OFF";
var message = '{"msg":"STATE-SET","time":"' + now.toISOString() + '","data":{"oson":"' + oson + '"}}';
this.mqtt_client.publish(
this.getCommandTopic(),
message
);
this.isRotationOn(callback);
}
function HotCoolLink(log, config) {
CoolLink.call(this, log, config);
}
HotCoolLink.prototype = Object.create(CoolLink.prototype);
HotCoolLink.prototype.constructor = HotCoolLink;
HotCoolLink.prototype.getMQTTPrefix = function() {
return "455";
}