Skip to content

Commit

Permalink
Merge pull request #514 from michikrug/fan-percent
Browse files Browse the repository at this point in the history
  • Loading branch information
michikrug authored Oct 10, 2023
2 parents 229f057 + c295e65 commit 7113457
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 8 deletions.
8 changes: 4 additions & 4 deletions docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ Player transportItem (tvGroup) { ga="tvTransport" }
| **Device Type** | [Fan](https://developers.home.google.com/cloud-to-cloud/guides/fan), [Hood](https://developers.home.google.com/cloud-to-cloud/guides/hood), [AirPurifier](https://developers.home.google.com/cloud-to-cloud/guides/airpurifier) |
| **Supported Traits** | [OnOff](https://developers.home.google.com/cloud-to-cloud/traits/OnOff), [FanSpeed](https://developers.home.google.com/cloud-to-cloud/traits/fanspeed) (depending on used item type) |
| **Supported Items** | Switch (no speed control), Dimmer |
| **Configuration** | (optional) `checkState=true/false`<br>(optional) `speeds="0=away:zero,50=default:standard:one,100=high:two"`<br>(optional) `lang="en"`<br>(optional) `ordered=true/false`<br>_Hint: if you are using a Dimmer then `speeds` is required_ |
| **Configuration** | (optional) `checkState=true/false`<br>(optional) `speeds="0=away:zero,50=default:standard:one,100=high:two"`<br>(optional) `lang="en"`<br>(optional) `ordered=true/false` |
Fans (and similar device types, like AirPurifier or Hood) support the `FanSpeed` trait.
If you do not specify the `speeds` option, Google will use and expect percentage values for the fan speed.
Expand All @@ -320,10 +320,10 @@ You are also able to define the language of those aliases.
The option `ordered` will tell the system that your list is ordered and you will then be able to also say "faster" or "slower" and Google will use the next or previous speed.
```shell
Dimmer { ga="Fan" [ speeds="0=away:zero,50=default:standard:one,100=high:two", lang="en", ordered=true ] } # Only percentage values for the speed
Dimmer { ga="Fan" [ speeds="0=away:zero,50=default:standard:one,100=high:two", lang="en", ordered=true ] } # Using specific percentage values for the speed
Switch { ga="Hood" } # No speed control - only on/off
Dimmer { ga="AirPurifier" } # Only percentage values for the speed
Dimmer { ga="AirPurifier" [ speeds="0=away:zero,1=low:one,2=medium:two,3=high:three,4=turbo:four", lang="en", ordered=true ] } # Specific speed modes/values stated, which can differ from percentage
Dimmer { ga="AirPurifier" } # Using percentage values for the speed
Dimmer { ga="AirPurifier" [ speeds="0=away:zero,1=low:one,2=medium:two,3=high:three,4=turbo:four", lang="en", ordered=true ] } # Using specific speed modes/values, which differ from percentage
Switch { ga="AirPurifier" } # No speed control - only on/off
```
Expand Down
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;
14 changes: 11 additions & 3 deletions functions/devices/fan.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class Fan extends DefaultDevice {
static getAttributes(item) {
const config = this.getConfig(item);
if (!config || !config.speeds) {
return {};
return {
supportsFanSpeedPercent: true
};
}
const attributes = {
availableFanSpeeds: {
Expand Down Expand Up @@ -49,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 });
});
});
21 changes: 20 additions & 1 deletion tests/devices/fan.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ describe('Fan Device', () => {
}
}
};
expect(Device.getAttributes(item)).toStrictEqual({});
expect(Device.getAttributes(item)).toStrictEqual({
supportsFanSpeedPercent: true
});
});

test('getAttributes speeds', () => {
Expand Down Expand Up @@ -84,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 7113457

Please sign in to comment.