homebridge-savantlightingcn 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/README.md +45 -0
  2. package/accessories/alarm_control_panel.js +187 -0
  3. package/accessories/binary_sensor.js +243 -0
  4. package/accessories/climate.js +526 -0
  5. package/accessories/cover.js +214 -0
  6. package/accessories/device_tracker.js +152 -0
  7. package/accessories/fan.js +254 -0
  8. package/accessories/floor_heating.js +266 -0
  9. package/accessories/light.js +519 -0
  10. package/accessories/lock.js +177 -0
  11. package/accessories/media_player.js +276 -0
  12. package/accessories/sensor.js +225 -0
  13. package/accessories/switch.js +217 -0
  14. package/dist/css/app.ddbde395.css +1 -0
  15. package/dist/css/chunk-vendors.f2db5e15.css +1 -0
  16. package/dist/css/test.0ebe5642.css +0 -0
  17. package/dist/favicon.ico +0 -0
  18. package/dist/index.html +1 -0
  19. package/dist/js/about.7c566887.js +2 -0
  20. package/dist/js/about.7c566887.js.map +1 -0
  21. package/dist/js/about.ecf592a0.js +2 -0
  22. package/dist/js/about.ecf592a0.js.map +1 -0
  23. package/dist/js/app.01e8fa44.js +2 -0
  24. package/dist/js/app.01e8fa44.js.map +1 -0
  25. package/dist/js/app.15ed2e3b.js +2 -0
  26. package/dist/js/app.15ed2e3b.js.map +1 -0
  27. package/dist/js/app.25976869.js +2 -0
  28. package/dist/js/app.25976869.js.map +1 -0
  29. package/dist/js/app.3112044b.js +2 -0
  30. package/dist/js/app.3112044b.js.map +1 -0
  31. package/dist/js/app.3a289bce.js +2 -0
  32. package/dist/js/app.3a289bce.js.map +1 -0
  33. package/dist/js/app.734f9d85.js +2 -0
  34. package/dist/js/app.734f9d85.js.map +1 -0
  35. package/dist/js/app.83545550.js +2 -0
  36. package/dist/js/app.83545550.js.map +1 -0
  37. package/dist/js/app.926ce9ff.js +2 -0
  38. package/dist/js/app.926ce9ff.js.map +1 -0
  39. package/dist/js/app.a6ba37fb.js +2 -0
  40. package/dist/js/app.a6ba37fb.js.map +1 -0
  41. package/dist/js/app.aa309e99.js +2 -0
  42. package/dist/js/app.aa309e99.js.map +1 -0
  43. package/dist/js/app.b2b4f482.js +2 -0
  44. package/dist/js/app.b2b4f482.js.map +1 -0
  45. package/dist/js/app.f9643858.js +2 -0
  46. package/dist/js/app.f9643858.js.map +1 -0
  47. package/dist/js/chunk-vendors.372a828c.js +52 -0
  48. package/dist/js/chunk-vendors.372a828c.js.map +1 -0
  49. package/dist/js/chunk-vendors.4b731e62.js +52 -0
  50. package/dist/js/chunk-vendors.4b731e62.js.map +1 -0
  51. package/dist/js/chunk-vendors.941b76e9.js +46 -0
  52. package/dist/js/chunk-vendors.941b76e9.js.map +1 -0
  53. package/dist/js/test.041333f7.js +2 -0
  54. package/dist/js/test.041333f7.js.map +1 -0
  55. package/dist/js/test.70bf0852.js +8 -0
  56. package/dist/js/test.70bf0852.js.map +1 -0
  57. package/dist/js/test.840bc653.js +2 -0
  58. package/dist/js/test.840bc653.js.map +1 -0
  59. package/index.js +1635 -0
  60. package/package.json +44 -0
@@ -0,0 +1,276 @@
1
+ 'use strict';
2
+
3
+ let Service;
4
+ let Characteristic;
5
+ let communicationError;
6
+
7
+ function HomeAssistantMediaPlayer(log, data, client, firmware) {
8
+ /* eslint-disable no-unused-vars */
9
+ const SUPPORT_PAUSE = 1;
10
+ const SUPPORT_SEEK = 2;
11
+ const SUPPORT_VOLUME_SET = 4;
12
+ const SUPPORT_VOLUME_MUTE = 8;
13
+ const SUPPORT_PREVIOUS_TRACK = 16;
14
+ const SUPPORT_NEXT_TRACK = 32;
15
+ const SUPPORT_TURN_ON = 128;
16
+ const SUPPORT_TURN_OFF = 256;
17
+ const SUPPORT_VOLUME_STEP = 1024;
18
+ const SUPPORT_STOP = 4096;
19
+ const SUPPORT_PLAY = 16384;
20
+ /* eslint-enable no-unused-vars */
21
+
22
+ // device info
23
+ this.domain = 'media_player';
24
+ this.data = data;
25
+ this.entity_id = data.entity_id;
26
+ this.uuid_base = data.entity_id;
27
+ this.firmware = firmware;
28
+ this.supportedFeatures = data.attributes.supported_features;
29
+ this.stateLogicCompareWithOn = true;
30
+
31
+ if (data.attributes && data.attributes.friendly_name) {
32
+ this.name = data.attributes.friendly_name;
33
+ } else {
34
+ this.name = data.entity_id.split('.').pop().replace(/_/g, ' ');
35
+ }
36
+
37
+ const supportPause = (this.supportedFeatures | SUPPORT_PAUSE) === this.supportedFeatures;
38
+ const supportStop = (this.supportedFeatures | SUPPORT_STOP) === this.supportedFeatures;
39
+ const supportOnOff = ((this.supportedFeatures | SUPPORT_TURN_ON) === this.supportedFeatures &&
40
+ (this.supportedFeatures | SUPPORT_TURN_OFF) === this.supportedFeatures);
41
+ this.supportMute = (this.supportedFeatures | SUPPORT_VOLUME_MUTE) === this.supportedFeatures;
42
+ this.supportVolume = (this.supportedFeatures | SUPPORT_VOLUME_SET) === this.supportedFeatures;
43
+
44
+ if (this.data && this.data.attributes && this.data.attributes.homebridge_media_player_switch === 'on_off' && supportOnOff) {
45
+ this.onState = 'on';
46
+ this.offState = 'off';
47
+ this.onService = 'turn_on';
48
+ this.offService = 'turn_off';
49
+ this.stateLogicCompareWithOn = false;
50
+ } else if (this.data && this.data.attributes && this.data.attributes.homebridge_media_player_switch === 'play_stop' && supportStop) {
51
+ this.onState = 'playing';
52
+ this.offState = 'idle';
53
+ this.onService = 'media_play';
54
+ this.offService = 'media_stop';
55
+ } else if (supportPause) {
56
+ this.onState = 'playing';
57
+ this.offState = 'paused';
58
+ this.onService = 'media_play';
59
+ this.offService = 'media_pause';
60
+ } else if (supportStop) {
61
+ this.onState = 'playing';
62
+ this.offState = 'idle';
63
+ this.onService = 'media_play';
64
+ this.offService = 'media_stop';
65
+ } else if (supportOnOff) {
66
+ this.onState = 'on';
67
+ this.offState = 'off';
68
+ this.onService = 'turn_on';
69
+ this.offService = 'turn_off';
70
+ }
71
+ if (data.attributes && data.attributes.homebridge_manufacturer) {
72
+ this.manufacturer = String(data.attributes.homebridge_manufacturer);
73
+ } else {
74
+ this.manufacturer = 'Home Assistant';
75
+ }
76
+ if (data.attributes && data.attributes.homebridge_model) {
77
+ this.model = String(data.attributes.homebridge_model);
78
+ } else {
79
+ this.model = 'Media Player';
80
+ }
81
+ if (data.attributes && data.attributes.homebridge_serial) {
82
+ this.serial = String(data.attributes.homebridge_serial);
83
+ } else {
84
+ this.serial = data.entity_id;
85
+ }
86
+ this.client = client;
87
+ this.log = log;
88
+ }
89
+
90
+ HomeAssistantMediaPlayer.prototype = {
91
+ onEvent(oldState, newState) {
92
+ if (newState.state) {
93
+ let powerState;
94
+ if (this.stateLogicCompareWithOn) {
95
+ powerState = newState.state === this.onState;
96
+ } else {
97
+ powerState = newState.state !== this.offState;
98
+ }
99
+ this.switchService.getCharacteristic(Characteristic.On)
100
+ .setValue(powerState, null, 'internal');
101
+ }
102
+ },
103
+ getPowerState(callback) {
104
+ this.log(`fetching power state for: ${this.name}`);
105
+
106
+ this.client.fetchState(this.entity_id, (data) => {
107
+ if (data) {
108
+ let powerState;
109
+ if (this.stateLogicCompareWithOn) {
110
+ powerState = data.state === this.onState;
111
+ } else {
112
+ powerState = data.state !== this.offState;
113
+ }
114
+ callback(null, powerState);
115
+ } else {
116
+ callback(communicationError);
117
+ }
118
+ });
119
+ },
120
+ setPowerState(powerOn, callback, context) {
121
+ if (context === 'internal') {
122
+ callback();
123
+ return;
124
+ }
125
+
126
+ const that = this;
127
+ const serviceData = {};
128
+ serviceData.entity_id = this.entity_id;
129
+
130
+ if (powerOn) {
131
+ this.log(`Setting power state on the '${this.name}' to on`);
132
+
133
+ this.client.callService(this.domain, this.onService, serviceData, (data) => {
134
+ if (data) {
135
+ that.log(`Successfully set power state on the '${that.name}' to on`);
136
+ callback();
137
+ } else {
138
+ callback(communicationError);
139
+ }
140
+ });
141
+ } else {
142
+ this.log(`Setting power state on the '${this.name}' to off`);
143
+
144
+ this.client.callService(this.domain, this.offService, serviceData, (data) => {
145
+ if (data) {
146
+ that.log(`Successfully set power state on the '${that.name}' to off`);
147
+ callback();
148
+ } else {
149
+ callback(communicationError);
150
+ }
151
+ });
152
+ }
153
+ },
154
+ getMuteState(callback) {
155
+ this.log(`fetching mute state for: ${this.name}`);
156
+
157
+ this.client.fetchState(this.entity_id, (data) => {
158
+ if (data) {
159
+ callback(null, data.attributes.is_volume_muted);
160
+ } else {
161
+ callback(communicationError);
162
+ }
163
+ });
164
+ },
165
+ setMuteState(muteOn, callback, context) {
166
+ if (context === 'internal') {
167
+ callback();
168
+ return;
169
+ }
170
+
171
+ const that = this;
172
+ const serviceData = {};
173
+ serviceData.entity_id = this.entity_id;
174
+ serviceData.is_volume_muted = (muteOn) ? 'true' : 'false';
175
+
176
+ this.log(`Setting mute state on the '${this.name}' to ${serviceData.is_volume_muted}`);
177
+
178
+ this.client.callService(this.domain, 'volume_mute', serviceData, (data) => {
179
+ if (data) {
180
+ that.log(`Successfully set mute state on the '${that.name}' to ${serviceData.is_volume_muted}`);
181
+ callback();
182
+ } else {
183
+ callback(communicationError);
184
+ }
185
+ });
186
+ },
187
+ getVolume(callback) {
188
+ this.log(`fetching volume for: ${this.name}`);
189
+
190
+ this.client.fetchState(this.entity_id, (data) => {
191
+ if (data) {
192
+ let volume;
193
+ if (!(data.attributes.volume_level)) {
194
+ volume = 0;
195
+ } else {
196
+ volume = (data.attributes.volume_level * 100);
197
+ }
198
+ callback(null, volume);
199
+ } else {
200
+ callback(communicationError);
201
+ }
202
+ });
203
+ },
204
+ setVolume(volume, callback, context) {
205
+ if (context === 'internal') {
206
+ callback();
207
+ return;
208
+ }
209
+
210
+ const that = this;
211
+ const serviceData = {};
212
+ serviceData.entity_id = this.entity_id;
213
+ serviceData.volume_level = volume / 100;
214
+
215
+ this.log(`Setting volume on the '${this.name}' to ${volume}%`);
216
+
217
+ this.client.callService(this.domain, 'volume_set', serviceData, (data) => {
218
+ if (data) {
219
+ that.log(`Successfully set volume on the '${that.name}' to ${serviceData.volume_level}`);
220
+ callback();
221
+ } else {
222
+ callback(communicationError);
223
+ }
224
+ });
225
+ },
226
+ getServices() {
227
+ this.switchService = new Service.Switch();
228
+ const informationService = new Service.AccessoryInformation();
229
+
230
+ informationService
231
+ .setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
232
+ .setCharacteristic(Characteristic.Model, this.model)
233
+ .setCharacteristic(Characteristic.SerialNumber, this.serial)
234
+ .setCharacteristic(Characteristic.FirmwareRevision, this.firmware);
235
+
236
+ this.switchService
237
+ .getCharacteristic(Characteristic.On)
238
+ .on('get', this.getPowerState.bind(this))
239
+ .on('set', this.setPowerState.bind(this));
240
+
241
+ if (this.supportMute) {
242
+ this.speakerService = new Service.Speaker();
243
+
244
+ this.speakerService
245
+ .setCharacteristic(Characteristic.Name, this.name);
246
+
247
+ this.speakerService
248
+ .getCharacteristic(Characteristic.Mute)
249
+ .on('get', this.getMuteState.bind(this))
250
+ .on('set', this.setMuteState.bind(this));
251
+
252
+ if (this.supportVolume) {
253
+ this.speakerService
254
+ .getCharacteristic(Characteristic.Volume)
255
+ .on('get', this.getVolume.bind(this))
256
+ .on('set', this.setVolume.bind(this));
257
+ }
258
+
259
+ return [informationService, this.switchService, this.speakerService];
260
+ }
261
+
262
+ return [informationService, this.switchService];
263
+ },
264
+
265
+ };
266
+
267
+ function HomeAssistantMediaPlayerPlatform(oService, oCharacteristic, oCommunicationError) {
268
+ Service = oService;
269
+ Characteristic = oCharacteristic;
270
+ communicationError = oCommunicationError;
271
+
272
+ return HomeAssistantMediaPlayer;
273
+ }
274
+
275
+ module.exports = HomeAssistantMediaPlayerPlatform;
276
+ module.exports.HomeAssistantMediaPlayer = HomeAssistantMediaPlayer;
@@ -0,0 +1,225 @@
1
+ 'use strict';
2
+
3
+ let Service;
4
+ let Characteristic;
5
+ let communicationError;
6
+
7
+ class HomeAssistantSensor {
8
+ constructor(log, data, client, service, characteristic, transformData, firmware) {
9
+ // device info
10
+ this.data = data;
11
+ this.entity_id = data.entity_id;
12
+ this.uuid_base = data.entity_id;
13
+ this.firmware = firmware;
14
+ if (data.attributes && data.attributes.friendly_name) {
15
+ this.name = data.attributes.friendly_name;
16
+ } else {
17
+ this.name = data.entity_id.split('.').pop().replace(/_/g, ' ');
18
+ }
19
+ if (data.attributes && data.attributes.homebridge_manufacturer) {
20
+ this.manufacturer = String(data.attributes.homebridge_manufacturer);
21
+ } else {
22
+ this.manufacturer = 'Home Assistant';
23
+ }
24
+ if (data.attributes && data.attributes.homebridge_model) {
25
+ this.model = String(data.attributes.homebridge_model);
26
+ } else {
27
+ this.model = 'Sensor';
28
+ }
29
+ if (data.attributes && data.attributes.homebridge_serial) {
30
+ this.serial = String(data.attributes.homebridge_serial);
31
+ } else {
32
+ this.serial = data.entity_id;
33
+ }
34
+ this.entity_type = data.entity_id.split('.')[0];
35
+ this.service = service;
36
+ this.characteristic = characteristic;
37
+ if (transformData) {
38
+ this.transformData = transformData;
39
+ }
40
+ this.client = client;
41
+ this.log = log;
42
+ this.batterySource = data.attributes.homebridge_battery_source;
43
+ this.chargingSource = data.attributes.homebridge_charging_source;
44
+ }
45
+
46
+ transformData(data) {
47
+ return parseFloat(data.state);
48
+ }
49
+
50
+ onEvent(oldState, newState) {
51
+ if (newState.state) {
52
+ if (this.service === Service.CarbonDioxideSensor) {
53
+ const transformed = this.transformData(newState);
54
+ this.sensorService.getCharacteristic(this.characteristic)
55
+ .setValue(transformed, null, 'internal');
56
+
57
+ const abnormal = Characteristic.CarbonDioxideDetected.CO2_LEVELS_ABNORMAL;
58
+ const normal = Characteristic.CarbonDioxideDetected.CO2_LEVELS_NORMAL;
59
+ const detected = (transformed > 1000 ? abnormal : normal);
60
+ this.sensorService.getCharacteristic(Characteristic.CarbonDioxideDetected)
61
+ .setValue(detected, null, 'internal');
62
+ } else {
63
+ this.sensorService.getCharacteristic(this.characteristic)
64
+ .setValue(this.transformData(newState), null, 'internal');
65
+ }
66
+ }
67
+ }
68
+
69
+ identify(callback) {
70
+ this.log(`identifying: ${this.name}`);
71
+ callback();
72
+ }
73
+
74
+ getState(callback) {
75
+ this.log(`fetching state for: ${this.name}`);
76
+ this.client.fetchState(this.entity_id, (data) => {
77
+ if (data) {
78
+ callback(null, this.transformData(data));
79
+ } else {
80
+ callback(communicationError);
81
+ }
82
+ });
83
+ }
84
+
85
+ getBatteryLevel(callback) {
86
+ this.client.fetchState(this.batterySource, (data) => {
87
+ if (data) {
88
+ callback(null, parseFloat(data.state));
89
+ } else {
90
+ callback(communicationError);
91
+ }
92
+ });
93
+ }
94
+ getChargingState(callback) {
95
+ if (this.batterySource && this.chargingSource) {
96
+ this.client.fetchState(this.chargingSource, (data) => {
97
+ if (data) {
98
+ callback(null, data.state.toLowerCase() === 'charging' ? 1 : 0);
99
+ } else {
100
+ callback(communicationError);
101
+ }
102
+ });
103
+ } else {
104
+ callback(null, 2);
105
+ }
106
+ }
107
+ getLowBatteryStatus(callback) {
108
+ this.client.fetchState(this.batterySource, (data) => {
109
+ if (data) {
110
+ callback(null, parseFloat(data.state) > 20 ? 0 : 1);
111
+ } else {
112
+ callback(communicationError);
113
+ }
114
+ });
115
+ }
116
+ getServices() {
117
+ this.sensorService = new this.service(); // eslint-disable-line new-cap
118
+ const informationService = new Service.AccessoryInformation();
119
+
120
+ informationService
121
+ .setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
122
+ .setCharacteristic(Characteristic.Model, this.model)
123
+ .setCharacteristic(Characteristic.SerialNumber, this.serial)
124
+ .setCharacteristic(Characteristic.FirmwareRevision, this.firmware);
125
+
126
+ this.sensorService
127
+ .getCharacteristic(this.characteristic)
128
+ .setProps({ minValue: -50 })
129
+ .on('get', this.getState.bind(this));
130
+
131
+ if (this.batterySource) {
132
+ this.batteryService = new Service.BatteryService();
133
+ this.batteryService
134
+ .getCharacteristic(Characteristic.BatteryLevel)
135
+ .setProps({ maxValue: 100, minValue: 0, minStep: 1 })
136
+ .on('get', this.getBatteryLevel.bind(this));
137
+ this.batteryService
138
+ .getCharacteristic(Characteristic.ChargingState)
139
+ .setProps({ maxValue: 2 })
140
+ .on('get', this.getChargingState.bind(this));
141
+ this.batteryService
142
+ .getCharacteristic(Characteristic.StatusLowBattery)
143
+ .on('get', this.getLowBatteryStatus.bind(this));
144
+ return [informationService, this.batteryService, this.sensorService];
145
+ }
146
+ return [informationService, this.sensorService];
147
+ }
148
+ }
149
+
150
+ function HomeAssistantSensorFactory(log, data, client, firmware) {
151
+ if (!data.attributes) {
152
+ return null;
153
+ }
154
+ let service;
155
+ let characteristic;
156
+ let transformData;
157
+ if (data.attributes.unit_of_measurement === '°C'
158
+ || data.attributes.unit_of_measurement === '℃'
159
+ || data.attributes.unit_of_measurement === '°F'
160
+ || data.attributes.unit_of_measurement === '℉') {
161
+ service = Service.TemperatureSensor;
162
+ characteristic = Characteristic.CurrentTemperature;
163
+ transformData = function transformData(dataToTransform) { // eslint-disable-line no-shadow
164
+ let value = parseFloat(dataToTransform.state);
165
+ // HomeKit only works with Celsius internally
166
+ if (dataToTransform.attributes.unit_of_measurement === '°F'
167
+ || dataToTransform.attributes.unit_of_measurement === '℉') {
168
+ value = (value - 32) / 1.8;
169
+ }
170
+ return value;
171
+ };
172
+ } else if (data.attributes.unit_of_measurement === '%' && (data.entity_id.includes('humidity') || data.attributes.homebridge_sensor_type === 'humidity')) {
173
+ service = Service.HumiditySensor;
174
+ characteristic = Characteristic.CurrentRelativeHumidity;
175
+ } else if ((typeof data.attributes.unit_of_measurement === 'string' && data.attributes.unit_of_measurement.toLowerCase() === 'lux') || data.attributes.homebridge_sensor_type === 'light') {
176
+ service = Service.LightSensor;
177
+ characteristic = Characteristic.CurrentAmbientLightLevel;
178
+ transformData = function transformData(dataToTransform) { // eslint-disable-line no-shadow
179
+ return Math.max(0.0001, parseFloat(dataToTransform.state));
180
+ };
181
+ } else if (typeof data.attributes.unit_of_measurement === 'string' && data.attributes.unit_of_measurement.toLowerCase() === 'ppm' && (data.entity_id.includes('co2') || data.attributes.homebridge_sensor_type === 'co2')) {
182
+ service = Service.CarbonDioxideSensor;
183
+ characteristic = Characteristic.CarbonDioxideLevel;
184
+ } else if ((typeof data.attributes.unit_of_measurement === 'string' && data.attributes.unit_of_measurement.toLowerCase() === 'aqi') || data.attributes.homebridge_sensor_type === 'air_quality') {
185
+ service = Service.AirQualitySensor;
186
+ characteristic = Characteristic.AirQuality;
187
+ transformData = function transformData(dataToTransform) { // eslint-disable-line no-shadow
188
+ const value = parseFloat(dataToTransform.state);
189
+ if (value <= 75) {
190
+ return 1;
191
+ } else if (value >= 76 && value <= 150) {
192
+ return 2;
193
+ } else if (value >= 151 && value <= 225) {
194
+ return 3;
195
+ } else if (value >= 226 && value <= 300) {
196
+ return 4;
197
+ } else if (value >= 301) {
198
+ return 5;
199
+ }
200
+ return 0;
201
+ };
202
+ } else {
203
+ return null;
204
+ }
205
+
206
+ return new HomeAssistantSensor(
207
+ log, data, client,
208
+ service,
209
+ characteristic,
210
+ transformData,
211
+ firmware
212
+ );
213
+ }
214
+
215
+ function HomeAssistantSensorPlatform(oService, oCharacteristic, oCommunicationError) {
216
+ Service = oService;
217
+ Characteristic = oCharacteristic;
218
+ communicationError = oCommunicationError;
219
+
220
+ return HomeAssistantSensorFactory;
221
+ }
222
+
223
+ module.exports = HomeAssistantSensorPlatform;
224
+
225
+ module.exports.HomeAssistantSensorFactory = HomeAssistantSensorFactory;