Skip to content

Commit

Permalink
Merge pull request #331 from michikrug/pr-rollershutter
Browse files Browse the repository at this point in the history
  • Loading branch information
michikrug authored Apr 2, 2022
2 parents 25daca0 + 53c4d16 commit 894a1bc
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion functions/commands/charge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion functions/commands/mute.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion functions/commands/onoff.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
7 changes: 4 additions & 3 deletions functions/commands/openclose.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ 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') {
return (100 - openPercent).toString();
return openPercent === 0 ? 'DOWN' : openPercent === 100 ? 'UP' : (100 - openPercent).toString();
}
if (itemType === 'Switch') {
return openPercent === 0 ? 'OFF' : 'ON';
Expand All @@ -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' };
}
}
Expand Down
7 changes: 7 additions & 0 deletions tests/commands/default.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
20 changes: 17 additions & 3 deletions tests/commands/openclose.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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 {
Expand All @@ -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');
}
});
});
});

0 comments on commit 894a1bc

Please sign in to comment.