homebridge-tuya-plus 4.2.0-dev.85 → 4.3.0-dev.100
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.
- package/Readme.MD +3 -0
- package/config.schema.json +22 -0
- package/index.js +2 -0
- package/lib/OccupancySensorAccessory.js +66 -0
- package/package.json +3 -2
- package/test/OccupancySensorAccessory.test.js +98 -0
- package/test/getCategory.test.js +1 -0
- package/test/support/mocks.js +4 -0
- package/wiki/Occupancy-Sensors.md +30 -0
package/Readme.MD
CHANGED
|
@@ -45,6 +45,7 @@ A community-maintained Homebridge plugin for controlling Tuya devices locally ov
|
|
|
45
45
|
* Color<sup>[12](https://github.com/adrianjagielak/homebridge-tuya-plus/blob/main/wiki/Supported-Device-Types.md#white-and-color-light-bulbs)</sup> (Hue, Saturation, Adaptive Lighting)
|
|
46
46
|
* Oil Diffusers<sup>[13](https://github.com/adrianjagielak/homebridge-tuya-plus/blob/main/wiki/Supported-Device-Types.md)</sup>
|
|
47
47
|
* Outlets<sup>[14](https://github.com/adrianjagielak/homebridge-tuya-plus/blob/main/wiki/Supported-Device-Types.md#outlets)</sup>
|
|
48
|
+
* [Presence Sensor (ZY-M100)](wiki/Occupancy-Sensors.md) (occupancy and ambient light)
|
|
48
49
|
* Switches<sup>[15](https://github.com/adrianjagielak/homebridge-tuya-plus/blob/main/wiki/Supported-Device-Types.md#switch)</sup>
|
|
49
50
|
* Vertical Blinds<sup>[16](https://github.com/adrianjagielak/homebridge-tuya-plus/blob/main/wiki/Supported-Device-Types.md#vertical-blinds-with-tilt)</sup>
|
|
50
51
|
|
|
@@ -102,6 +103,8 @@ If you have new accessory logic for a new device, please add a function defined
|
|
|
102
103
|
|
|
103
104
|
## Credits
|
|
104
105
|
|
|
106
|
+
* [Eliran Sapir](https://github.com/sapireli) - ZY-M100 occupancy and ambient light support ([original implementation](https://github.com/sapireli/homebridge-tuya-local/commit/08e223b2f4ecfe80031ee5a4b16393a88fdd0b5b)), and the contact, smoke, and leak sensor port with battery support and regression tests.
|
|
107
|
+
|
|
105
108
|
* [iRayanKhan](https://github.com/iRayanKhan) - developer of the original [homebridge-tuya](https://github.com/iRayanKhan/homebridge-tuya) plugin.
|
|
106
109
|
* [AMoo-Miki](https://github.com/AMoo-Miki) - developer of the [Tuya-Lan](https://github.com/AMoo-Miki/homebridge-tuya-lan) plugin which this plugin is based off.
|
|
107
110
|
* mxDanger - Plugin branding.
|
package/config.schema.json
CHANGED
|
@@ -171,6 +171,10 @@
|
|
|
171
171
|
"title": "Air Purifier",
|
|
172
172
|
"enum": ["AirPurifier"]
|
|
173
173
|
},
|
|
174
|
+
{
|
|
175
|
+
"title": "Presence Sensor (ZY-M100)",
|
|
176
|
+
"enum": ["OccupancySensor"]
|
|
177
|
+
},
|
|
174
178
|
{
|
|
175
179
|
"title": "Irrigation System (Multi-Valve / Sprinkler)",
|
|
176
180
|
"enum": ["IrrigationSystem"]
|
|
@@ -288,6 +292,24 @@
|
|
|
288
292
|
"functionBody": "return model.devices && model.devices[arrayIndices] && ['Outlet','RGBTWOutlet'].includes(model.devices[arrayIndices].type);"
|
|
289
293
|
}
|
|
290
294
|
},
|
|
295
|
+
"dpOccupancy": {
|
|
296
|
+
"type": "integer",
|
|
297
|
+
"title": "Occupancy Data Point",
|
|
298
|
+
"minimum": 1,
|
|
299
|
+
"placeholder": "1",
|
|
300
|
+
"condition": {
|
|
301
|
+
"functionBody": "return model.devices && model.devices[arrayIndices] && model.devices[arrayIndices].type === 'OccupancySensor';"
|
|
302
|
+
}
|
|
303
|
+
},
|
|
304
|
+
"dpLight": {
|
|
305
|
+
"type": "integer",
|
|
306
|
+
"title": "Illuminance Data Point",
|
|
307
|
+
"minimum": 1,
|
|
308
|
+
"placeholder": "104",
|
|
309
|
+
"condition": {
|
|
310
|
+
"functionBody": "return model.devices && model.devices[arrayIndices] && model.devices[arrayIndices].type === 'OccupancySensor';"
|
|
311
|
+
}
|
|
312
|
+
},
|
|
291
313
|
"dpPower": {
|
|
292
314
|
"type": "integer",
|
|
293
315
|
"placeholder": "1",
|
package/index.js
CHANGED
|
@@ -5,6 +5,7 @@ const TuyaCloudApi = require('./lib/TuyaCloudApi');
|
|
|
5
5
|
const TuyaCloudMessaging = require('./lib/TuyaCloudMessaging');
|
|
6
6
|
|
|
7
7
|
const OutletAccessory = require('./lib/OutletAccessory');
|
|
8
|
+
const OccupancySensorAccessory = require('./lib/OccupancySensorAccessory');
|
|
8
9
|
const SimpleLightAccessory = require('./lib/SimpleLightAccessory');
|
|
9
10
|
const MultiOutletAccessory = require('./lib/MultiOutletAccessory');
|
|
10
11
|
const CustomMultiOutletAccessory = require('./lib/CustomMultiOutletAccessory');
|
|
@@ -41,6 +42,7 @@ const UUID_SEED = 'homebridge-tuya';
|
|
|
41
42
|
const DEFAULT_DISCOVER_TIMEOUT = 60000;
|
|
42
43
|
|
|
43
44
|
const CLASS_DEF = {
|
|
45
|
+
occupancysensor: OccupancySensorAccessory,
|
|
44
46
|
outlet: OutletAccessory,
|
|
45
47
|
simplelight: SimpleLightAccessory,
|
|
46
48
|
rgbtwlight: RGBTWLightAccessory,
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
const BaseAccessory = require('./BaseAccessory');
|
|
2
|
+
|
|
3
|
+
class OccupancySensorAccessory extends BaseAccessory {
|
|
4
|
+
static getCategory(Categories) {
|
|
5
|
+
return Categories.SENSOR;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
_registerPlatformAccessory() {
|
|
9
|
+
const {Service} = this.hap;
|
|
10
|
+
const {name} = this.device.context;
|
|
11
|
+
this.accessory.addService(Service.OccupancySensor, name);
|
|
12
|
+
this.accessory.addService(Service.LightSensor, name + ' Light');
|
|
13
|
+
super._registerPlatformAccessory();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
_registerCharacteristics(dps) {
|
|
17
|
+
const {Service, Characteristic} = this.hap;
|
|
18
|
+
const {name} = this.device.context;
|
|
19
|
+
const occupancyService = this.accessory.getService(Service.OccupancySensor);
|
|
20
|
+
const lightService = this.accessory.getService(Service.LightSensor);
|
|
21
|
+
this._checkServiceName(occupancyService, name);
|
|
22
|
+
this._checkServiceName(lightService, name + ' Light');
|
|
23
|
+
|
|
24
|
+
this.dpOccupancy = this._getCustomDP(this.device.context.dpOccupancy) || '1';
|
|
25
|
+
this.dpLight = this._getCustomDP(this.device.context.dpLight) || '104';
|
|
26
|
+
|
|
27
|
+
const occupancy = occupancyService.getCharacteristic(Characteristic.OccupancyDetected)
|
|
28
|
+
.updateValue(this._occupancyValue(dps[this.dpOccupancy]))
|
|
29
|
+
.onGet(async () => this._occupancyValue(await this.getStateAsync(this.dpOccupancy)));
|
|
30
|
+
const light = lightService.getCharacteristic(Characteristic.CurrentAmbientLightLevel)
|
|
31
|
+
.onGet(async () => {
|
|
32
|
+
const value = this._lightValue(await this.getStateAsync(this.dpLight));
|
|
33
|
+
if (value === undefined) throw this._commError();
|
|
34
|
+
return value;
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const initialLight = this._lightValue(dps[this.dpLight]);
|
|
38
|
+
if (initialLight !== undefined) light.updateValue(initialLight);
|
|
39
|
+
|
|
40
|
+
this.device.on('change', changes => {
|
|
41
|
+
if (Object.prototype.hasOwnProperty.call(changes, this.dpOccupancy)) {
|
|
42
|
+
const value = this._occupancyValue(changes[this.dpOccupancy]);
|
|
43
|
+
if (occupancy.value !== value) occupancy.updateValue(value);
|
|
44
|
+
}
|
|
45
|
+
if (Object.prototype.hasOwnProperty.call(changes, this.dpLight)) {
|
|
46
|
+
const value = this._lightValue(changes[this.dpLight]);
|
|
47
|
+
if (value !== undefined && light.value !== value) light.updateValue(value);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
_occupancyValue(value) {
|
|
53
|
+
const {OccupancyDetected} = this.hap.Characteristic;
|
|
54
|
+
return value === 'presence' ? OccupancyDetected.OCCUPANCY_DETECTED : OccupancyDetected.OCCUPANCY_NOT_DETECTED;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
_lightValue(value) {
|
|
58
|
+
if (typeof value !== 'number' && (typeof value !== 'string' || value.trim() === '')) return undefined;
|
|
59
|
+
const lux = Number(value);
|
|
60
|
+
if (!Number.isFinite(lux) || lux < 0) return undefined;
|
|
61
|
+
// HomeKit's light characteristic excludes zero, which this sensor reports in darkness.
|
|
62
|
+
return Math.min(100000, Math.max(0.0001, lux));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
module.exports = OccupancySensorAccessory;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "homebridge-tuya-plus",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.3.0-dev.100",
|
|
4
4
|
"description": "A community-maintained Homebridge plugin for controlling Tuya devices locally over LAN, with an optional Tuya Cloud fallback for whatever the LAN can't reach. Includes new features, fixes, and updated device support. PRs welcome: if it runs, it ships (almost).",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -38,7 +38,8 @@
|
|
|
38
38
|
"supports-hap",
|
|
39
39
|
"homebridge",
|
|
40
40
|
"homebridge-tuya",
|
|
41
|
-
"tuya"
|
|
41
|
+
"tuya",
|
|
42
|
+
"lan"
|
|
42
43
|
],
|
|
43
44
|
"engines": {
|
|
44
45
|
"homebridge": "^1.8.0 || ^2.0.0-beta.0",
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const OccupancySensorAccessory = require('../lib/OccupancySensorAccessory');
|
|
4
|
+
const {HAP, makeInstance, makeMockService, makeMockCharacteristic} = require('./support/mocks');
|
|
5
|
+
|
|
6
|
+
function makeSensor(state = {'1': 'presence', '104': 25}, context = {}) {
|
|
7
|
+
const fixture = makeInstance(OccupancySensorAccessory, state, {type: 'OccupancySensor', ...context});
|
|
8
|
+
const occupancy = makeMockCharacteristic();
|
|
9
|
+
const light = makeMockCharacteristic();
|
|
10
|
+
const occupancyService = makeMockService(HAP.Service.OccupancySensor.UUID);
|
|
11
|
+
const lightService = makeMockService(HAP.Service.LightSensor.UUID);
|
|
12
|
+
occupancyService.displayName = fixture.device.context.name;
|
|
13
|
+
lightService.displayName = fixture.device.context.name + ' Light';
|
|
14
|
+
occupancyService.getCharacteristic.mockReturnValue(occupancy);
|
|
15
|
+
lightService.getCharacteristic.mockReturnValue(light);
|
|
16
|
+
fixture.accessory.getService.mockImplementation(type =>
|
|
17
|
+
type === HAP.Service.OccupancySensor ? occupancyService : lightService);
|
|
18
|
+
fixture.instance._registerCharacteristics(state);
|
|
19
|
+
const change = fixture.device.on.mock.calls.find(([event]) => event === 'change')[1];
|
|
20
|
+
return {...fixture, occupancy, light, change};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
describe('OccupancySensorAccessory', () => {
|
|
24
|
+
test('registers occupancy and light services with stable names', () => {
|
|
25
|
+
const {instance, accessory, platform} = makeInstance(OccupancySensorAccessory);
|
|
26
|
+
instance._registerPlatformAccessory();
|
|
27
|
+
expect(accessory.addService).toHaveBeenCalledWith(HAP.Service.OccupancySensor, 'Test Device');
|
|
28
|
+
expect(accessory.addService).toHaveBeenCalledWith(HAP.Service.LightSensor, 'Test Device Light');
|
|
29
|
+
expect(platform.registerPlatformAccessories).toHaveBeenCalledWith(accessory);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test.each([['presence', 1], ['none', 0], ['absence', 0]])('maps %s to occupancy %s', (value, expected) => {
|
|
33
|
+
const {occupancy} = makeSensor({'1': value, '104': 25});
|
|
34
|
+
expect(occupancy.value).toBe(expected);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('reads occupancy and illuminance through Homebridge getters', async () => {
|
|
38
|
+
const {occupancy, light} = makeSensor();
|
|
39
|
+
await expect(occupancy.onGet.mock.calls[0][0]()).resolves.toBe(1);
|
|
40
|
+
await expect(light.onGet.mock.calls[0][0]()).resolves.toBe(25);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('supports custom data points supplied as numbers or strings', async () => {
|
|
44
|
+
const {instance, occupancy, light} = makeSensor({'5': 'presence', '8': '120'}, {dpOccupancy: 5, dpLight: '8'});
|
|
45
|
+
expect(instance.dpOccupancy).toBe('5');
|
|
46
|
+
expect(instance.dpLight).toBe('8');
|
|
47
|
+
expect(occupancy.value).toBe(1);
|
|
48
|
+
await expect(light.onGet.mock.calls[0][0]()).resolves.toBe(120);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('updates occupancy without overwriting the last light reading', () => {
|
|
52
|
+
const {occupancy, light, change} = makeSensor();
|
|
53
|
+
light.updateValue.mockClear();
|
|
54
|
+
change({'1': 'none'});
|
|
55
|
+
expect(occupancy.value).toBe(0);
|
|
56
|
+
expect(light.value).toBe(25);
|
|
57
|
+
expect(light.updateValue).not.toHaveBeenCalled();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('updates light without changing occupancy or emitting duplicate updates', () => {
|
|
61
|
+
const {occupancy, light, change} = makeSensor();
|
|
62
|
+
occupancy.updateValue.mockClear();
|
|
63
|
+
light.updateValue.mockClear();
|
|
64
|
+
change({'104': 42});
|
|
65
|
+
change({'104': 42});
|
|
66
|
+
expect(light.value).toBe(42);
|
|
67
|
+
expect(light.updateValue).toHaveBeenCalledTimes(1);
|
|
68
|
+
expect(occupancy.updateValue).not.toHaveBeenCalled();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test.each([[0, 0.0001], [0.00001, 0.0001], [100001, 100000]])('clamps %s lux to %s', (value, expected) => {
|
|
72
|
+
const {light} = makeSensor({'1': 'none', '104': value});
|
|
73
|
+
expect(light.value).toBe(expected);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test.each([undefined, null, '', 'invalid', -1, NaN, Infinity, false])('does not publish invalid light reading %s', async value => {
|
|
77
|
+
const {light, change, device} = makeSensor();
|
|
78
|
+
light.updateValue.mockClear();
|
|
79
|
+
change({'104': value});
|
|
80
|
+
expect(light.updateValue).not.toHaveBeenCalled();
|
|
81
|
+
device.state['104'] = value;
|
|
82
|
+
await expect(light.onGet.mock.calls[0][0]()).rejects.toMatchObject({hapStatus: -70402});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('a missing illuminance DP does not prevent occupancy from working', async () => {
|
|
86
|
+
const {occupancy, light} = makeSensor({'1': 'presence'});
|
|
87
|
+
expect(light.updateValue).not.toHaveBeenCalled();
|
|
88
|
+
await expect(occupancy.onGet.mock.calls[0][0]()).resolves.toBe(1);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test('reports No Response for both characteristics when disconnected', async () => {
|
|
92
|
+
const {device, occupancy, light} = makeSensor();
|
|
93
|
+
device.connected = false;
|
|
94
|
+
await expect(occupancy.onGet.mock.calls[0][0]()).rejects.toMatchObject({hapStatus: -70402});
|
|
95
|
+
await expect(light.onGet.mock.calls[0][0]()).rejects.toMatchObject({hapStatus: -70402});
|
|
96
|
+
expect(device.update).not.toHaveBeenCalled();
|
|
97
|
+
});
|
|
98
|
+
});
|
package/test/getCategory.test.js
CHANGED
|
@@ -9,6 +9,7 @@ const { Categories } = HAP;
|
|
|
9
9
|
// expectation again and the accessory is unregistered & recreated on every
|
|
10
10
|
// Homebridge restart — wiping its name, room and automations (issue #45).
|
|
11
11
|
const CLASS_DEF = {
|
|
12
|
+
occupancysensor: require('../lib/OccupancySensorAccessory'),
|
|
12
13
|
outlet: require('../lib/OutletAccessory'),
|
|
13
14
|
simplelight: require('../lib/SimpleLightAccessory'),
|
|
14
15
|
rgbtwlight: require('../lib/RGBTWLightAccessory'),
|
package/test/support/mocks.js
CHANGED
|
@@ -40,6 +40,8 @@ const HAP = {
|
|
|
40
40
|
StatusLowBattery: { UUID: '79', BATTERY_LEVEL_NORMAL: 0, BATTERY_LEVEL_LOW: 1 },
|
|
41
41
|
ChargingState: { UUID: '8F', NOT_CHARGING: 0, CHARGING: 1, NOT_CHARGEABLE: 2 },
|
|
42
42
|
ContactSensorState: { UUID: '6A', CONTACT_DETECTED: 0, CONTACT_NOT_DETECTED: 1 },
|
|
43
|
+
OccupancyDetected: { UUID: '71', OCCUPANCY_NOT_DETECTED: 0, OCCUPANCY_DETECTED: 1 },
|
|
44
|
+
CurrentAmbientLightLevel: { UUID: '6B' },
|
|
43
45
|
LeakDetected: { UUID: '70', LEAK_NOT_DETECTED: 0, LEAK_DETECTED: 1 },
|
|
44
46
|
HeatingThresholdTemperature: { UUID: '12' },
|
|
45
47
|
CoolingThresholdTemperature: { UUID: '0D' },
|
|
@@ -69,6 +71,8 @@ const HAP = {
|
|
|
69
71
|
ServiceLabel: { UUID: 'CC' },
|
|
70
72
|
Battery: { UUID: '96' },
|
|
71
73
|
ContactSensor: { UUID: '80' },
|
|
74
|
+
OccupancySensor: { UUID: '86' },
|
|
75
|
+
LightSensor: { UUID: '84' },
|
|
72
76
|
LeakSensor: { UUID: '83' },
|
|
73
77
|
Doorbell: { UUID: '41' },
|
|
74
78
|
AccessoryInformation: { UUID: '3E' },
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# Occupancy sensors
|
|
2
|
+
|
|
3
|
+
The `OccupancySensor` device type supports the Wi-Fi Tuya ZY-M100 presence sensor.
|
|
4
|
+
It exposes occupancy and ambient light as two HomeKit services on one accessory.
|
|
5
|
+
|
|
6
|
+
Add the device to the existing `TuyaLan` platform's `devices` list:
|
|
7
|
+
|
|
8
|
+
```json
|
|
9
|
+
{
|
|
10
|
+
"type": "OccupancySensor",
|
|
11
|
+
"name": "Garage Occupancy Sensor",
|
|
12
|
+
"id": "YOUR_DEVICE_ID",
|
|
13
|
+
"key": "YOUR_LOCAL_KEY",
|
|
14
|
+
"manufacturer": "Tuya",
|
|
15
|
+
"model": "ZY-M100"
|
|
16
|
+
}
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The default occupancy data point is `1`: the value `presence` means occupied;
|
|
20
|
+
other values mean unoccupied. The default illuminance data point is `104`, in lux.
|
|
21
|
+
Use the optional `dpOccupancy` and `dpLight` settings to override these IDs for a
|
|
22
|
+
device with different data points. These settings also appear in the Homebridge UI
|
|
23
|
+
when the Presence Sensor device type is selected.
|
|
24
|
+
|
|
25
|
+
HomeKit requires illuminance between 0.0001 and 100000 lux. Readings outside that
|
|
26
|
+
range are clamped; invalid or missing light readings are not published as zero.
|
|
27
|
+
An unreachable device reports No Response when its characteristics are read.
|
|
28
|
+
|
|
29
|
+
The sensor must be connected to the network for LAN discovery and updates to work.
|
|
30
|
+
This device type does not add support for Zigbee sensors behind a gateway.
|