Skip to content

Commit

Permalink
Add fan rotation direction support. (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x5e committed Nov 18, 2022
1 parent 4061089 commit b1d4ba5
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/accessory/FanAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export default class FanAccessory extends BaseAccessory {
this.configureRotationSpeedOn();
}

this.configureRotationDirection();

this.configureLightOn();
this.configureLightBrightness();
}
Expand Down Expand Up @@ -54,6 +56,10 @@ export default class FanAccessory extends BaseAccessory {
return undefined;
}

getFanDirection() {
return this.getSchema('fan_direction');
}

getLightOnSchema() {
return this.getSchema('light')
|| this.getSchema('switch_led');
Expand All @@ -66,16 +72,21 @@ export default class FanAccessory extends BaseAccessory {

configureActive() {
const schema = this.getFanActiveSchema()!;
if (!schema) {
return;
}

const { ACTIVE, INACTIVE } = this.Characteristic.Active;
this.fanService().getCharacteristic(this.Characteristic.Active)
.onGet(() => {
const status = this.getStatus(schema.code)!;
return status.value as boolean;
return status.value as boolean ? ACTIVE : INACTIVE;
})
.onSet(value => {
const status = this.getStatus(schema.code)!;
this.sendCommands([{
code: status.code,
value: (value === this.Characteristic.Active.ACTIVE) ? true : false,
value: (value === ACTIVE) ? true : false,
}], true);
});
}
Expand Down Expand Up @@ -134,6 +145,23 @@ export default class FanAccessory extends BaseAccessory {
.setProps(props);
}

configureRotationDirection() {
const schema = this.getFanDirection();
if (!schema) {
return;
}

const { CLOCKWISE, COUNTER_CLOCKWISE } = this.Characteristic.RotationDirection;
this.fanService().getCharacteristic(this.Characteristic.RotationDirection)
.onGet(() => {
const status = this.getStatus(schema.code)!;
return (status.value !== 'reverse') ? CLOCKWISE : COUNTER_CLOCKWISE;
})
.onSet(value => {
this.sendCommands([{ code: schema.code, value: (value === CLOCKWISE) ? 'forward' : 'reverse' }]);
});
}

configureLightOn() {
const schema = this.getLightOnSchema();
if (!schema) {
Expand Down

0 comments on commit b1d4ba5

Please sign in to comment.