Skip to content

Commit

Permalink
Add support for actually setting fanSpeedPercent
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Krug <[email protected]>
  • Loading branch information
michikrug committed Oct 10, 2023
1 parent 153c1e6 commit a29f03c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
23 changes: 23 additions & 0 deletions functions/commands/setfanspeedpercent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const DefaultCommand = require('./default.js');

class SetFanSpeedPercent extends DefaultCommand {
static get type() {
return 'action.devices.commands.SetFanSpeed';
}

static validateParams(params) {
return 'fanSpeedPercent' in params && typeof params.fanSpeedPercent === 'number';
}

static convertParamsToValue(params) {
return params.fanSpeedPercent.toString();
}

static getResponseStates(params) {
return {
currentFanSpeedPercent: params.fanSpeedPercent
};
}
}

module.exports = SetFanSpeedPercent;
10 changes: 8 additions & 2 deletions functions/devices/fan.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,16 @@ class Fan extends DefaultDevice {
}

static getState(item) {
return {
currentFanSpeedSetting: item.state.toString(),
const state = {
on: Number(item.state) > 0
};
const config = this.getConfig(item);
if (config && config.speeds) {
state.currentFanSpeedSetting = item.state.toString();
} else {
state.currentFanSpeedPercent = Math.round(Number(item.state));
}
return state;
}
}

Expand Down
18 changes: 18 additions & 0 deletions tests/commands/setfanspeedpercent.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const Command = require('../../functions/commands/setfanspeedpercent.js');

describe('SetFanSpeed Command', () => {
const params = { fanSpeedPercent: 50 };

test('validateParams', () => {
expect(Command.validateParams({})).toBe(false);
expect(Command.validateParams(params)).toBe(true);
});

test('convertParamsToValue', () => {
expect(Command.convertParamsToValue(params)).toBe('50');
});

test('getResponseStates', () => {
expect(Command.getResponseStates(params)).toStrictEqual({ currentFanSpeedPercent: 50 });
});
});
17 changes: 17 additions & 0 deletions tests/devices/fan.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@ describe('Fan Device', () => {

test('getState', () => {
expect(Device.getState({ state: '50' })).toStrictEqual({
currentFanSpeedPercent: 50,
on: true
});
expect(
Device.getState({
state: '50',
metadata: {
ga: {
config: {
ordered: true,
speeds: '0=null:off,50=slow,100=full:fast',
lang: 'en'
}
}
}
})
).toStrictEqual({
currentFanSpeedSetting: '50',
on: true
});
Expand Down

0 comments on commit a29f03c

Please sign in to comment.