Skip to content

Commit

Permalink
Fix read value on parent type
Browse files Browse the repository at this point in the history
  • Loading branch information
atrovato committed Aug 3, 2022
1 parent fed7014 commit 44c105d
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 8 deletions.
2 changes: 1 addition & 1 deletion server/services/zigbee2mqtt/lib/findMatchingExpose.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const logger = require('../../../utils/logger');
const recursiveSearch = (expose, type, search) => {
const { property, features = [] } = expose;
if (property === search) {
return expose;
return { ...expose, parent_type: type };
}

for (let i = 0; i < features.length; i += 1) {
Expand Down
4 changes: 3 additions & 1 deletion server/services/zigbee2mqtt/lib/readValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ function readValue(deviceName, property, value) {
throw new Error(`Zigbee2mqqt expose not found on device "${deviceName}" with property "${property}".`);
}

const matchingValue = exposesMap[expose.type].readValue(expose, value);
const { parent_type: parentType, type: exposeType } = expose;
const mapper = exposesMap[parentType] || exposesMap[exposeType];
const matchingValue = mapper.readValue(expose, value);

if (matchingValue === undefined) {
throw new Error(`Zigbee2mqqt don't handle value "${value}" for property "${property}".`);
Expand Down
22 changes: 19 additions & 3 deletions server/test/services/zigbee2mqtt/lib/findMatchingExpose.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,42 @@ describe('zigbee2mqtt findMatchingExpose', () => {
zigbee2MqttService.device.discoveredDevices[discoveredDevice.friendly_name] = discoveredDevice;
});

it('no device discovered', () => {
it('no device discovered on unknown device', () => {
const result = zigbee2MqttService.device.findMatchingExpose('unknown', 'property');
assert.equal(result, undefined);
});

it('no expose discovered', () => {
it('no expose discovered on unknown property', () => {
const result = zigbee2MqttService.device.findMatchingExpose('0x00158d00045b2740', 'property');
assert.equal(result, undefined);
});

it('no expose discovered', () => {
it('expose discovered', () => {
const expected = {
type: 'binary',
name: 'state',
property: 'state',
parent_type: 'switch',
access: 3,
value_on: 'ON',
value_off: 'OFF',
};
const result = zigbee2MqttService.device.findMatchingExpose('0x00158d00045b2740', 'state');
assert.deepEqual(result, expected);
});

it('expose dicovered on child features', () => {
const expected = {
access: 7,
description: 'Position of this cover',
name: 'position',
property: 'position',
type: 'numeric',
parent_type: 'cover',
value_max: 100,
value_min: 0,
};
const result = zigbee2MqttService.device.findMatchingExpose('0x00158d00045b2740', 'position');
assert.deepEqual(result, expected);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@
"value_off": "OFF"
}
]
},
{
"features": [
{
"access": 7,
"description": "Position of this cover",
"name": "position",
"property": "position",
"type": "numeric",
"value_max": 100,
"value_min": 0
}
],
"type": "cover"
}
],
"model": "WSDCGQ11LM",
Expand Down
5 changes: 5 additions & 0 deletions server/test/services/zigbee2mqtt/lib/readValue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ describe('zigbee2mqtt readValue', () => {
const result = zigbee2MqttService.device.readValue('0x00158d00045b2740', 'alarm', false);
assert.deepEqual(result, 0);
});

it('should return binary OPEN on parent type', () => {
const result = zigbee2MqttService.device.readValue('0x00158d00045b2740', 'alarm', false);
assert.deepEqual(result, 0);
});
});
19 changes: 18 additions & 1 deletion server/test/services/zigbee2mqtt/utils/payloads/CCT5015.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,22 @@
"name": "CCT5015 device",
"model": "CCT5015",
"should_poll": false
}
},
"values": [
{
"internal": -1,
"external": "CLOSE",
"property": "state"
},
{
"internal": 1,
"external": "OPEN",
"property": "state"
},
{
"internal": 0,
"external": "STOP",
"property": "state"
}
]
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { expect } = require('chai');
const Zigbee2mqttManager = require('../../../../services/zigbee2mqtt/lib');

const { convertDevice } = require('../../../../services/zigbee2mqtt/utils/convertDevice');
const payloads = require('./payloads');
Expand All @@ -7,12 +8,28 @@ const serviceId = 'a4c859f0-32d2-46b7-8f5a-3285960f498a';

describe('Decoding real devices', () => {
payloads.forEach((payload) => {
const { name, mqttDevice, gladysDevice } = payload;
const { name, mqttDevice, gladysDevice, values } = payload;

it(`check Gladys device -> ${name}`, async () => {
let service;
beforeEach(() => {
service = new Zigbee2mqttManager(null, null, null);
service.discoveredDevices = {
[name]: mqttDevice,
};
});

it(`[${name}] check Gladys device`, async () => {
const device = await convertDevice(mqttDevice, serviceId);

expect(device).to.deep.eq(gladysDevice);
});

values.forEach(({ internal, external, property }) => {
it(`[${name}] read value ${external}`, () => {
const result = service.readValue(name, property, external);

expect(result).to.deep.eq(internal);
});
});
});
});

0 comments on commit 44c105d

Please sign in to comment.