From 7ef0a092180efc1eadd5c0b2b89d678918fa2530 Mon Sep 17 00:00:00 2001 From: Michael Krug Date: Mon, 28 Mar 2022 00:53:06 +0200 Subject: [PATCH 1/2] Bring back UP & DOWN for open close devices Signed-off-by: Michael Krug --- functions/commands/openclose.js | 5 +++-- tests/commands/openclose.test.js | 20 +++++++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/functions/commands/openclose.js b/functions/commands/openclose.js index 21d3699d..41b010b6 100644 --- a/functions/commands/openclose.js +++ b/functions/commands/openclose.js @@ -19,7 +19,7 @@ class OpenClose extends DefaultCommand { openPercent = 100 - openPercent; } if (itemType === 'Rollershutter') { - return (100 - openPercent).toString(); + return openPercent === 0 ? 'DOWN' : openPercent === 100 ? 'UP' : (100 - openPercent).toString(); } if (itemType === 'Switch') { return openPercent === 0 ? 'OFF' : 'ON'; @@ -34,7 +34,8 @@ class OpenClose extends DefaultCommand { } static checkCurrentState(target, state, params) { - if (target === state) { + const adjustedTarget = target === 'DOWN' ? '100' : target === 'UP' ? '0' : target; + if (adjustedTarget === state) { throw { errorCode: params.openPercent === 0 ? 'alreadyClosed' : 'alreadyOpen' }; } } diff --git a/tests/commands/openclose.test.js b/tests/commands/openclose.test.js index 0d7a42b5..df70ef5b 100644 --- a/tests/commands/openclose.test.js +++ b/tests/commands/openclose.test.js @@ -27,9 +27,9 @@ describe('OpenClose Command', () => { test('convertParamsToValue Rollershutter', () => { const device = { customData: { itemType: 'Rollershutter' } }; - expect(Command.convertParamsToValue({ openPercent: 0 }, {}, device)).toBe('100'); + expect(Command.convertParamsToValue({ openPercent: 0 }, {}, device)).toBe('DOWN'); expect(Command.convertParamsToValue({ openPercent: 20 }, {}, device)).toBe('80'); - expect(Command.convertParamsToValue({ openPercent: 100 }, {}, device)).toBe('0'); + expect(Command.convertParamsToValue({ openPercent: 100 }, {}, device)).toBe('UP'); }); test('convertParamsToValue Switch', () => { @@ -53,7 +53,7 @@ describe('OpenClose Command', () => { describe('checkCurrentState', () => { test('Switch', () => { - expect.assertions(4); + expect.assertions(8); expect(Command.checkCurrentState('ON', 'OFF', { openPercent: 100 })).toBeUndefined(); try { @@ -68,6 +68,20 @@ describe('OpenClose Command', () => { } catch (e) { expect(e.errorCode).toBe('alreadyClosed'); } + + expect(Command.checkCurrentState('UP', '100', { openPercent: 100 })).toBeUndefined(); + try { + Command.checkCurrentState('UP', '0', { openPercent: 100 }); + } catch (e) { + expect(e.errorCode).toBe('alreadyOpen'); + } + + expect(Command.checkCurrentState('DOWN', '0', { openPercent: 0 })).toBeUndefined(); + try { + Command.checkCurrentState('DOWN', '100', { openPercent: 0 }); + } catch (e) { + expect(e.errorCode).toBe('alreadyClosed'); + } }); }); }); From 53c4d16627c28efe1bbef01523d9b6505738f49b Mon Sep 17 00:00:00 2001 From: Michael Krug Date: Mon, 28 Mar 2022 01:06:01 +0200 Subject: [PATCH 2/2] Shorten isInverted checks Signed-off-by: Michael Krug --- functions/commands/charge.js | 2 +- functions/commands/mute.js | 2 +- functions/commands/onoff.js | 2 +- functions/commands/openclose.js | 2 +- tests/commands/default.test.js | 7 +++++++ 5 files changed, 11 insertions(+), 4 deletions(-) diff --git a/functions/commands/charge.js b/functions/commands/charge.js index f2f69f78..12477009 100644 --- a/functions/commands/charge.js +++ b/functions/commands/charge.js @@ -24,7 +24,7 @@ class Charge extends DefaultCommand { static convertParamsToValue(params, _, device) { let charge = params.charge; - if (this.isInverted(device) === true) { + if (this.isInverted(device)) { charge = !charge; } return charge ? 'ON' : 'OFF'; diff --git a/functions/commands/mute.js b/functions/commands/mute.js index ef537cca..0c66b2c2 100644 --- a/functions/commands/mute.js +++ b/functions/commands/mute.js @@ -42,7 +42,7 @@ class Mute extends DefaultCommand { if (itemType !== 'Switch') { return mute ? '0' : undefined; } - if (this.isInverted(device) === true) { + if (this.isInverted(device)) { mute = !mute; } return mute ? 'ON' : 'OFF'; diff --git a/functions/commands/onoff.js b/functions/commands/onoff.js index 06372816..135b6aba 100644 --- a/functions/commands/onoff.js +++ b/functions/commands/onoff.js @@ -39,7 +39,7 @@ class OnOff extends DefaultCommand { static convertParamsToValue(params, _, device) { let on = params.on; - if (this.isInverted(device) === true) { + if (this.isInverted(device)) { on = !on; } return on ? 'ON' : 'OFF'; diff --git a/functions/commands/openclose.js b/functions/commands/openclose.js index 41b010b6..683a4263 100644 --- a/functions/commands/openclose.js +++ b/functions/commands/openclose.js @@ -15,7 +15,7 @@ class OpenClose extends DefaultCommand { throw { statusCode: 400 }; } let openPercent = params.openPercent; - if (this.isInverted(device) === true) { + if (this.isInverted(device)) { openPercent = 100 - openPercent; } if (itemType === 'Rollershutter') { diff --git a/tests/commands/default.test.js b/tests/commands/default.test.js index 39413428..00179baa 100644 --- a/tests/commands/default.test.js +++ b/tests/commands/default.test.js @@ -71,6 +71,13 @@ describe('Default Command', () => { expect(Command.getItemName({ name: 'Item' }, {})).toBe('Item'); }); + test('isInverted', () => { + expect(Command.isInverted({})).toBe(false); + expect(Command.isInverted({ id: 'Item', customData: {} })).toBe(false); + expect(Command.isInverted({ id: 'Item', customData: { inverted: false } })).toBe(false); + expect(Command.isInverted({ id: 'Item', customData: { inverted: true } })).toBe(true); + }); + test('requiresItem', () => { expect(Command.requiresItem({})).toBe(false); });