Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

z2m reversed binary #1517

Merged
merged 1 commit into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 90 additions & 81 deletions server/services/zigbee2mqtt/exposes/binaryType.js
Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -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,
};
26 changes: 26 additions & 0 deletions server/test/services/zigbee2mqtt/exposes/binaryType.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down