From a64d493304e9dc1afd6fefb64c1ea518ad663186 Mon Sep 17 00:00:00 2001 From: atrovato <1839717+atrovato@users.noreply.github.com> Date: Tue, 3 May 2022 08:27:46 +0200 Subject: [PATCH] z2m reversed binary --- .../zigbee2mqtt/exposes/binaryType.js | 171 +++++++++--------- .../zigbee2mqtt/exposes/binaryType.test.js | 26 +++ 2 files changed, 116 insertions(+), 81 deletions(-) diff --git a/server/services/zigbee2mqtt/exposes/binaryType.js b/server/services/zigbee2mqtt/exposes/binaryType.js index b1ca7d113a..ac4b13275d 100644 --- a/server/services/zigbee2mqtt/exposes/binaryType.js +++ b/server/services/zigbee2mqtt/exposes/binaryType.js @@ -1,20 +1,106 @@ const { DEVICE_FEATURE_CATEGORIES, DEVICE_FEATURE_TYPES } = require('../../../utils/constants'); +const names = { + alarm: { + feature: { + category: DEVICE_FEATURE_CATEGORIES.SIREN, + type: DEVICE_FEATURE_TYPES.SIREN.BINARY, + }, + }, + contact: { + feature: { + category: DEVICE_FEATURE_CATEGORIES.OPENING_SENSOR, + type: DEVICE_FEATURE_TYPES.SENSOR.BINARY, + }, + reversedValue: true, + }, + eco_mode: { + feature: { + category: DEVICE_FEATURE_CATEGORIES.SWITCH, + type: DEVICE_FEATURE_TYPES.SENSOR.BINARY, + }, + }, + occupancy: { + feature: { + category: DEVICE_FEATURE_CATEGORIES.MOTION_SENSOR, + type: DEVICE_FEATURE_TYPES.SENSOR.BINARY, + }, + }, + interlock: { + feature: { + category: DEVICE_FEATURE_CATEGORIES.SWITCH, + type: DEVICE_FEATURE_TYPES.SENSOR.BINARY, + }, + }, + carbon_monoxide: { + feature: { + category: DEVICE_FEATURE_CATEGORIES.CO_SENSOR, + type: DEVICE_FEATURE_TYPES.SENSOR.BINARY, + }, + }, + presence: { + feature: { + category: DEVICE_FEATURE_CATEGORIES.MOTION_SENSOR, + type: DEVICE_FEATURE_TYPES.SENSOR.BINARY, + }, + }, + smoke: { + feature: { + category: DEVICE_FEATURE_CATEGORIES.SMOKE_SENSOR, + type: DEVICE_FEATURE_TYPES.SENSOR.BINARY, + }, + }, + state: { + types: { + light: { + category: DEVICE_FEATURE_CATEGORIES.LIGHT, + type: DEVICE_FEATURE_TYPES.LIGHT.BINARY, + }, + lock: { + category: DEVICE_FEATURE_CATEGORIES.ACCESS_CONTROL, + type: DEVICE_FEATURE_TYPES.ACCESS_CONTROL.MODE, + }, + switch: { + category: DEVICE_FEATURE_CATEGORIES.SWITCH, + type: DEVICE_FEATURE_TYPES.SWITCH.BINARY, + }, + }, + }, + vibration: { + feature: { + category: DEVICE_FEATURE_CATEGORIES.VIBRATION_SENSOR, + type: DEVICE_FEATURE_TYPES.VIBRATION_SENSOR.BINARY, + }, + }, + water_leak: { + feature: { + category: DEVICE_FEATURE_CATEGORIES.LEAK_SENSOR, + type: DEVICE_FEATURE_TYPES.SENSOR.BINARY, + }, + }, +}; + module.exports = { type: 'binary', writeValue: (expose, value) => { - if (value === 1) { + const { [expose.name]: mapping = {} } = names; + const reversed = mapping.reversedValue || false; + + if ((value === 1 && !reversed) || (value === 0 && reversed)) { return expose.value_on; } return expose.value_off; }, readValue: (expose, value) => { + const { [expose.name]: mapping = {} } = names; + const reversed = mapping.reversedValue || false; + switch (value) { case expose.value_on: - return 1; + return reversed ? 0 : 1; case expose.value_off: - return 0; + return reversed ? 1 : 0; default: return undefined; } @@ -25,82 +111,5 @@ module.exports = { min: 0, max: 1, }, - names: { - alarm: { - feature: { - category: DEVICE_FEATURE_CATEGORIES.SIREN, - type: DEVICE_FEATURE_TYPES.SIREN.BINARY, - }, - }, - contact: { - feature: { - category: DEVICE_FEATURE_CATEGORIES.OPENING_SENSOR, - type: DEVICE_FEATURE_TYPES.SENSOR.BINARY, - }, - }, - eco_mode: { - feature: { - category: DEVICE_FEATURE_CATEGORIES.SWITCH, - type: DEVICE_FEATURE_TYPES.SENSOR.BINARY, - }, - }, - occupancy: { - feature: { - category: DEVICE_FEATURE_CATEGORIES.MOTION_SENSOR, - type: DEVICE_FEATURE_TYPES.SENSOR.BINARY, - }, - }, - interlock: { - feature: { - category: DEVICE_FEATURE_CATEGORIES.SWITCH, - type: DEVICE_FEATURE_TYPES.SENSOR.BINARY, - }, - }, - carbon_monoxide: { - feature: { - category: DEVICE_FEATURE_CATEGORIES.CO_SENSOR, - type: DEVICE_FEATURE_TYPES.SENSOR.BINARY, - }, - }, - presence: { - feature: { - category: DEVICE_FEATURE_CATEGORIES.MOTION_SENSOR, - type: DEVICE_FEATURE_TYPES.SENSOR.BINARY, - }, - }, - smoke: { - feature: { - category: DEVICE_FEATURE_CATEGORIES.SMOKE_SENSOR, - type: DEVICE_FEATURE_TYPES.SENSOR.BINARY, - }, - }, - state: { - types: { - light: { - category: DEVICE_FEATURE_CATEGORIES.LIGHT, - type: DEVICE_FEATURE_TYPES.LIGHT.BINARY, - }, - lock: { - category: DEVICE_FEATURE_CATEGORIES.ACCESS_CONTROL, - type: DEVICE_FEATURE_TYPES.ACCESS_CONTROL.MODE, - }, - switch: { - category: DEVICE_FEATURE_CATEGORIES.SWITCH, - type: DEVICE_FEATURE_TYPES.SWITCH.BINARY, - }, - }, - }, - vibration: { - feature: { - category: DEVICE_FEATURE_CATEGORIES.VIBRATION_SENSOR, - type: DEVICE_FEATURE_TYPES.VIBRATION_SENSOR.BINARY, - }, - }, - water_leak: { - feature: { - category: DEVICE_FEATURE_CATEGORIES.LEAK_SENSOR, - type: DEVICE_FEATURE_TYPES.SENSOR.BINARY, - }, - }, - }, + names, }; diff --git a/server/test/services/zigbee2mqtt/exposes/binaryType.test.js b/server/test/services/zigbee2mqtt/exposes/binaryType.test.js index 259f340088..55a79f1b11 100644 --- a/server/test/services/zigbee2mqtt/exposes/binaryType.test.js +++ b/server/test/services/zigbee2mqtt/exposes/binaryType.test.js @@ -4,9 +4,15 @@ const binaryType = require('../../../../services/zigbee2mqtt/exposes/binaryType' describe('zigbee2mqtt binaryType', () => { const expose = { + name: 'state', value_on: 'ON', value_off: 7, }; + const reversedExpose = { + name: 'contact', + value_on: 'ON', + value_off: 'OFF', + }; it('should write 1 value', () => { const result = binaryType.writeValue(expose, 1); @@ -28,6 +34,26 @@ describe('zigbee2mqtt binaryType', () => { assert.equal(result, 0); }); + it('should write 1 value on reversed expose', () => { + const result = binaryType.writeValue(reversedExpose, 1); + assert.equal(result, 'OFF'); + }); + + it('should write 0 value on reversed expose', () => { + const result = binaryType.writeValue(reversedExpose, 0); + assert.equal(result, 'ON'); + }); + + it('should read 1 value on reversed expose', () => { + const result = binaryType.readValue(reversedExpose, 'ON'); + assert.equal(result, 0); + }); + + it('should read 0 value on reversed expose', () => { + const result = binaryType.readValue(reversedExpose, 'OFF'); + assert.equal(result, 1); + }); + it('should read unknown value', () => { const result = binaryType.readValue(expose, 'unknown'); assert.equal(result, undefined);